diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-09-19 07:51:27 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-09-19 07:51:27 +0000 |
| commit | 9ecdfb23fe3df6a5df86782385002c562dfc1198 (patch) | |
| tree | 4188cb7e6bf2c862d9c86a59d79946bd41217227 /hooks | |
| parent | b67861fbb424c7ad47ad1538f75e2945bd8890c5 (diff) | |
(대표님) rfq 히스토리, swp 등
Diffstat (limited to 'hooks')
| -rw-r--r-- | hooks/use-document-polling.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/hooks/use-document-polling.ts b/hooks/use-document-polling.ts new file mode 100644 index 00000000..21162491 --- /dev/null +++ b/hooks/use-document-polling.ts @@ -0,0 +1,76 @@ +// app/hooks/use-document-polling.ts +"use client" + +import { pullDocumentStatusFromSHI } from "@/lib/vendor-document-list/plant/document-stages-service" +import { useEffect, useState } from "react" +import { toast } from "sonner" + +interface UseDocumentPollingProps { + contractId: number + autoStart?: boolean + onUpdate?: () => void +} + +export function useDocumentPolling({ + contractId, + autoStart = true, + onUpdate +}: UseDocumentPollingProps) { + const [isPolling, setIsPolling] = useState(false) + const [lastPolledAt, setLastPolledAt] = useState<Date | null>(null) + const [pollingStatus, setPollingStatus] = useState<'idle' | 'polling' | 'success' | 'error'>('idle') + + // 폴링 함수 + const pollDocuments = async (showToast = true) => { + setIsPolling(true) + setPollingStatus('polling') + + try { + const result = await pullDocumentStatusFromSHI(contractId) + + if (result.success) { + setPollingStatus('success') + setLastPolledAt(new Date()) + + if (showToast) { + if (result.updatedCount > 0) { + toast.success(result.message) + } else { + toast.info("모든 문서가 최신 상태입니다.") + } + } + + // 업데이트 콜백 실행 + if (result.updatedCount > 0 && onUpdate) { + onUpdate() + } + } else { + setPollingStatus('error') + if (showToast) { + toast.error(result.message) + } + } + } catch (error) { + setPollingStatus('error') + if (showToast) { + toast.error("동기화 중 오류가 발생했습니다.") + } + } finally { + setIsPolling(false) + } + } + + // 페이지 로드 시 자동 폴링 + useEffect(() => { + if (autoStart && contractId) { + pollDocuments(false) // 초기 로드 시에는 토스트 메시지 표시 안 함 + } + }, [contractId]) + + return { + isPolling, + lastPolledAt, + pollingStatus, + pollDocuments + } +}
\ No newline at end of file |
