refactor(api): 复用电池业务常量
This commit is contained in:
+24
-27
@@ -4,13 +4,8 @@ import { createColumnHelper, flexRender, getCoreRowModel, useReactTable } from '
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { z } from 'zod'
|
||||
import { orpc } from '@/client/orpc'
|
||||
import type { BatteryInfo } from '@/domain/battery'
|
||||
|
||||
const sortOptions = ['createdAtDesc', 'createdAtAsc', 'powerDesc', 'powerAsc'] as const
|
||||
type BatteryListSort = (typeof sortOptions)[number]
|
||||
|
||||
const powerStatusOptions = [0, 1, 2] as const
|
||||
type PowerStatusFilter = (typeof powerStatusOptions)[number]
|
||||
import type { BatteryInfo, BatteryListSort, PowerStatus } from '@/domain/battery'
|
||||
import { BATTERY_LIST_SORT, BATTERY_LIST_SORT_VALUES, POWER_STATUS, POWER_STATUS_VALUES } from '@/domain/battery'
|
||||
|
||||
const pageSizeOptions = [20, 50, 100] as const
|
||||
type PageSizeOption = (typeof pageSizeOptions)[number]
|
||||
@@ -29,8 +24,10 @@ const cursorSchema = z.preprocess(
|
||||
const searchSchema = z.object({
|
||||
search: searchFilterSchema,
|
||||
lowPower: z.boolean().optional(),
|
||||
powerStatus: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(),
|
||||
sort: z.enum(sortOptions).optional().default('createdAtDesc'),
|
||||
powerStatus: z
|
||||
.union([z.literal(POWER_STATUS.NOT_CHARGING), z.literal(POWER_STATUS.CHARGING), z.literal(POWER_STATUS.FULL)])
|
||||
.optional(),
|
||||
sort: z.enum(BATTERY_LIST_SORT_VALUES).optional().default(BATTERY_LIST_SORT.CREATED_AT_DESC),
|
||||
pageSize: z.coerce
|
||||
.number()
|
||||
.pipe(z.union([z.literal(20), z.literal(50), z.literal(100)]))
|
||||
@@ -53,16 +50,16 @@ export const Route = createFileRoute('/batteries')({
|
||||
),
|
||||
})
|
||||
|
||||
const powerStatusLabel: Record<0 | 1 | 2, string> = {
|
||||
0: '未充电',
|
||||
1: '充电中',
|
||||
2: '已充满',
|
||||
const powerStatusLabel: Record<PowerStatus, string> = {
|
||||
[POWER_STATUS.NOT_CHARGING]: '未充电',
|
||||
[POWER_STATUS.CHARGING]: '充电中',
|
||||
[POWER_STATUS.FULL]: '已充满',
|
||||
}
|
||||
|
||||
const powerStatusColor: Record<0 | 1 | 2, string> = {
|
||||
0: 'text-zinc-400',
|
||||
1: 'text-teal-400',
|
||||
2: 'text-emerald-400',
|
||||
const powerStatusColor: Record<PowerStatus, string> = {
|
||||
[POWER_STATUS.NOT_CHARGING]: 'text-zinc-400',
|
||||
[POWER_STATUS.CHARGING]: 'text-teal-400',
|
||||
[POWER_STATUS.FULL]: 'text-emerald-400',
|
||||
}
|
||||
|
||||
function powerBarColor(power: number, isLowPower: boolean): string {
|
||||
@@ -74,13 +71,13 @@ function powerBarColor(power: number, isLowPower: boolean): string {
|
||||
const columnHelper = createColumnHelper<BatteryInfo>()
|
||||
|
||||
function parseSort(value: string): BatteryListSort {
|
||||
return sortOptions.find((option) => option === value) ?? 'createdAtDesc'
|
||||
return BATTERY_LIST_SORT_VALUES.find((option) => option === value) ?? BATTERY_LIST_SORT.CREATED_AT_DESC
|
||||
}
|
||||
|
||||
function parsePowerStatus(value: string): PowerStatusFilter | undefined {
|
||||
function parsePowerStatus(value: string): PowerStatus | undefined {
|
||||
const parsed = Number(value)
|
||||
|
||||
return powerStatusOptions.find((option) => option === parsed)
|
||||
return POWER_STATUS_VALUES.find((option) => option === parsed)
|
||||
}
|
||||
|
||||
function parsePageSize(value: string): PageSizeOption {
|
||||
@@ -284,9 +281,9 @@ function BatteriesPage() {
|
||||
}}
|
||||
>
|
||||
<option value="">所有充电状态</option>
|
||||
<option value="0">未充电</option>
|
||||
<option value="1">充电中</option>
|
||||
<option value="2">已充满</option>
|
||||
<option value={POWER_STATUS.NOT_CHARGING}>未充电</option>
|
||||
<option value={POWER_STATUS.CHARGING}>充电中</option>
|
||||
<option value={POWER_STATUS.FULL}>已充满</option>
|
||||
</select>
|
||||
|
||||
<label className="flex items-center gap-2 text-sm text-zinc-400 cursor-pointer">
|
||||
@@ -324,10 +321,10 @@ function BatteriesPage() {
|
||||
})
|
||||
}}
|
||||
>
|
||||
<option value="createdAtDesc">最新更新</option>
|
||||
<option value="createdAtAsc">最早更新</option>
|
||||
<option value="powerDesc">电量从高到低</option>
|
||||
<option value="powerAsc">电量从低到高</option>
|
||||
<option value={BATTERY_LIST_SORT.CREATED_AT_DESC}>最新更新</option>
|
||||
<option value={BATTERY_LIST_SORT.CREATED_AT_ASC}>最早更新</option>
|
||||
<option value={BATTERY_LIST_SORT.POWER_DESC}>电量从高到低</option>
|
||||
<option value={BATTERY_LIST_SORT.POWER_ASC}>电量从低到高</option>
|
||||
</select>
|
||||
|
||||
<select
|
||||
|
||||
Reference in New Issue
Block a user