261019_01_ENH: 리스트 비동기 작업 동시 실행 제한 및 진행 상태 UI 개선, 미사용 참조 정리
This commit is contained in:
37
src/hooks/useListJobPageMonitor.ts
Normal file
37
src/hooks/useListJobPageMonitor.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { useQueryClient } from '@tanstack/react-query'
|
||||
import { useAuth } from '@/hooks/useAuth'
|
||||
import { useListJobGate } from '@/components/list/ListJobGate'
|
||||
import { listMutationBlockerIds } from '@/utils/listJobAvailability'
|
||||
import type { ListJobType } from '@/types/list-job'
|
||||
|
||||
/** 두 목록 화면의 공용 감시. 타인의 변경 작업도 종료 시 현재 프로젝트 데이터를 갱신한다. */
|
||||
export function useListJobPageMonitor(prjctIdx: number, jobType: ListJobType, active: boolean) {
|
||||
const gate = useListJobGate({ prjctIdx, jobType, active, pollWhileIdle: true, requestLabel: jobType === 'LIST_ASSIGN' ? '배정' : '업로드·초기화·일괄 수정' })
|
||||
const qc = useQueryClient()
|
||||
const { auth } = useAuth()
|
||||
const userId = auth.user?.userId
|
||||
const previous = useRef<{ scope: string; ids: string[] } | null>(null)
|
||||
const [dataRevision, setDataRevision] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
const scope = JSON.stringify([userId, prjctIdx])
|
||||
if (!active || !userId) { previous.current = null; return }
|
||||
if (previous.current?.scope !== scope) previous.current = null
|
||||
const availability = gate.availability
|
||||
// 실패한 조회와 최대 20건에서 잘린 응답으로 작업 종료를 단정하지 않는다.
|
||||
if (!gate.availabilitySucceeded || !availability || availability.blockerCount > availability.blockers.length) return
|
||||
const ids = listMutationBlockerIds(availability)
|
||||
const ended = previous.current?.ids.some(id => !ids.includes(id)) ?? false
|
||||
previous.current = { scope, ids }
|
||||
if (!ended) return
|
||||
// 실패·중단이어도 이미 반영된 데이터가 있을 수 있다.
|
||||
void qc.invalidateQueries({ predicate: query =>
|
||||
(query.queryKey[0] === 'list' || query.queryKey[0] === 'assign') && query.queryKey[2] === prjctIdx })
|
||||
setDataRevision(revision => revision + 1)
|
||||
}, [active, userId, prjctIdx, gate.availability, gate.availabilitySucceeded, gate.availabilityUpdatedAt, qc])
|
||||
|
||||
return { gate, dataRevision }
|
||||
}
|
||||
Reference in New Issue
Block a user