Files
o2o-negosium-original/frontend/src/features/list/hooks/useSessionCounts.ts
T

64 lines
2.4 KiB
TypeScript

import { useQueries } from '@tanstack/react-query'
import { negotiationApi, negotiationKeys, SessionStatus, SessionResult } from '@/apis'
// KPI 콕핏 집계. 진행(상태축)·결과(결과축) 두 묶음. 각 항목을 page_size=1 로 조회해 total 만 읽는다(목록과 캐시 공유).
// 클릭 시 걸 필터가 상태냐 결과냐가 다르므로 kind 로 구분해 컨테이너가 올바른 setter 로 보낸다.
export interface KpiCount {
key: string
label: string
kind: 'status' | 'result'
statusLabel?: string // kind=status: 필터에 넣을 원본 상태 라벨
resultCode?: number // kind=result: 필터에 넣을 SessionResult 코드
count: number
isLoading: boolean
}
// 진행 묶음(내가 할 일) — 세션 상태축.
const PROGRESS = [
{ key: 'created', label: '협상 대기', statusLabel: '협상생성', code: SessionStatus.CREATED },
{ key: 'progress', label: '협상 중', statusLabel: '협상중', code: SessionStatus.IN_PROGRESS },
] as const
// 결과 묶음(마감 후) — 결과축. 낙찰=주강조, 결렬=재협상 가능(액션).
const RESULT = [
{ key: 'won', label: '낙찰', code: SessionResult.WON },
{ key: 'open', label: '결렬', code: SessionResult.OPEN },
{ key: 'lost', label: '미낙찰', code: SessionResult.LOST },
] as const
export function useSessionCounts(): { progress: KpiCount[]; result: KpiCount[] } {
const statusQ = useQueries({
queries: PROGRESS.map((k) => ({
queryKey: negotiationKeys.sessionList({ status: k.code, page: 1, page_size: 1 }),
queryFn: () => negotiationApi.getSessions({ status: k.code, page: 1, page_size: 1 }),
staleTime: 60 * 1000,
})),
})
const resultQ = useQueries({
queries: RESULT.map((k) => ({
queryKey: negotiationKeys.sessionList({ result: k.code, page: 1, page_size: 1 }),
queryFn: () => negotiationApi.getSessions({ result: k.code, page: 1, page_size: 1 }),
staleTime: 60 * 1000,
})),
})
return {
progress: PROGRESS.map((k, i) => ({
key: k.key,
label: k.label,
kind: 'status',
statusLabel: k.statusLabel,
count: statusQ[i].data?.total ?? 0,
isLoading: statusQ[i].isLoading,
})),
result: RESULT.map((k, i) => ({
key: k.key,
label: k.label,
kind: 'result',
resultCode: k.code,
count: resultQ[i].data?.total ?? 0,
isLoading: resultQ[i].isLoading,
})),
}
}