From 9ecdfb23fe3df6a5df86782385002c562dfc1198 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Fri, 19 Sep 2025 07:51:27 +0000 Subject: (대표님) rfq 히스토리, swp 등 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hooks/use-document-polling.ts | 76 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 hooks/use-document-polling.ts (limited to 'hooks') 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(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 -- cgit v1.2.3