refactor(game): 重构项目结构,优化链路, 移动端适配
- 移除 useGameBoardVm 数据层实施说明文档 - 移除核心玩法与前端规则摘要文档 - 移除游戏模块数据与界面分层第一阶段实施稿文档 - 清理与数据层重构相关的技术方案说明 - 删除关于 PC 和 Mobile 界面分离的设计规划 - 移除 view-model hooks 架构设计相关内容
This commit is contained in:
61
src/hooks/use-period-history-vm.ts
Normal file
61
src/hooks/use-period-history-vm.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useMemo } from 'react'
|
||||
|
||||
import { type GamePeriodHistoryItemDto, getGamePeriodHistory } from '@/api'
|
||||
import { FLOWER_IMAGE_BY_ID } from '@/features/game/shared'
|
||||
import type { PeriodHistoryDisplayItem } from '@/type'
|
||||
|
||||
export const DEFAULT_PERIOD_HISTORY_LIMIT = 36
|
||||
|
||||
function formatPeriodNo(periodNo: string) {
|
||||
const [, timeSegment] = periodNo.split('-')
|
||||
|
||||
return timeSegment && timeSegment.length <= 8 ? timeSegment : periodNo
|
||||
}
|
||||
|
||||
function formatResultNumber(number: number) {
|
||||
return String(number).padStart(2, '0')
|
||||
}
|
||||
|
||||
export function toPeriodHistoryDisplayItem(
|
||||
item: GamePeriodHistoryItemDto,
|
||||
): PeriodHistoryDisplayItem {
|
||||
return {
|
||||
displayPeriodNo: formatPeriodNo(item.period_no),
|
||||
displayResultNumber: formatResultNumber(item.result_number),
|
||||
image: FLOWER_IMAGE_BY_ID[item.result_number]?.animalUrl ?? '',
|
||||
isOdd: item.result_number % 2 === 1,
|
||||
openTime: item.open_time,
|
||||
periodNo: item.period_no,
|
||||
resultNumber: item.result_number,
|
||||
}
|
||||
}
|
||||
|
||||
export function usePeriodHistoryVm({
|
||||
enabled,
|
||||
limit = DEFAULT_PERIOD_HISTORY_LIMIT,
|
||||
}: {
|
||||
enabled: boolean
|
||||
limit?: number
|
||||
}) {
|
||||
const query = useQuery({
|
||||
queryKey: ['game', 'period-history', limit],
|
||||
enabled,
|
||||
queryFn: () => getGamePeriodHistory({ limit }),
|
||||
refetchOnMount: 'always',
|
||||
staleTime: 0,
|
||||
})
|
||||
|
||||
const items = useMemo(
|
||||
() =>
|
||||
(query.data?.list ?? []).map((item) => toPeriodHistoryDisplayItem(item)),
|
||||
[query.data?.list],
|
||||
)
|
||||
|
||||
return {
|
||||
isError: query.isError,
|
||||
isLoading: query.isLoading,
|
||||
items,
|
||||
refetch: query.refetch,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user