"use client"; import { useEffect, useRef, useState } from "react"; // Split-tender gateway poll loop (split-payment gateway, Task 13). When a split sale has a pending // gateway leg the backend returns a redirect URL (rendered as a QR the customer scans) and a sale // `uuid`; this hook polls that uuid's settlement state until it terminates. `poll`/`cancel` are // injected so the SAME hook drives BOTH the Branch Counter (BO_COUNTER) and Event POS (BO_POS) // surfaces — each passes its own channel client fns. // // Terminal handling: // - PAID → onPaid(); stop. // - FAILED / EXPIRED → onFailed(state); stop. // - timeoutMs elapsed → cancel(uuid) (release the reserved stock), then onFailed("EXPIRED"); stop. // A transient poll error (network blip) is swallowed — the next tick retries. The interval and the // timeout are cleared on unmount and whenever `uuid` clears, so a closed panel never keeps polling. export type SplitPollFailure = "FAILED" | "EXPIRED"; interface UseSplitPollArgs { /** The pending sale's uuid, or null/"" when there is nothing to poll. */ uuid: string | null; poll: (uuid: string) => Promise<{ status: "PENDING" | "PAID" | "FAILED" | "EXPIRED" }>; cancel: (uuid: string) => Promise; onPaid: () => void; onFailed: (state: SplitPollFailure) => void; /** Total wait before auto-cancel. Defaults to 5 minutes. */ timeoutMs?: number; /** Poll cadence. Defaults to 2.5s. */ intervalMs?: number; } export function useSplitPoll({ uuid, poll, cancel, onPaid, onFailed, timeoutMs = 5 * 60_000, intervalMs = 2500, }: UseSplitPollArgs): { polling: boolean; elapsed: number } { const [polling, setPolling] = useState(false); const [elapsed, setElapsed] = useState(0); // Latest callbacks/fns held in refs so the effect depends only on `uuid` (and the timings) and // never re-subscribes when the parent re-renders with new closures. const pollRef = useRef(poll); const cancelRef = useRef(cancel); const onPaidRef = useRef(onPaid); const onFailedRef = useRef(onFailed); // Keep the injected callbacks fresh without re-subscribing the poll loop. The refs are read only // inside the effect below (the interval/timeout callbacks), never during render. /* eslint-disable react-hooks/refs -- latest-callback refs; read only in the effect, not render. */ pollRef.current = poll; cancelRef.current = cancel; onPaidRef.current = onPaid; onFailedRef.current = onFailed; /* eslint-enable react-hooks/refs */ // This effect owns the poll lifecycle: it subscribes to an external timer system and drives the // pending sale to a terminal state. The setState calls synchronise React with that timer, which is // the intended use of an effect (subscribe to an external system), not a cascading-render smell. /* eslint-disable react-hooks/set-state-in-effect -- poll lifecycle synced to an external timer. */ useEffect(() => { if (!uuid) { setPolling(false); setElapsed(0); return; } setPolling(true); setElapsed(0); const start = Date.now(); let done = false; let intervalId: ReturnType; let timeoutId: ReturnType; const stop = () => { done = true; clearInterval(intervalId); clearTimeout(timeoutId); setPolling(false); }; intervalId = setInterval(() => { if (done) return; setElapsed(Date.now() - start); void pollRef .current(uuid) .then((res) => { if (done) return; if (res.status === "PAID") { stop(); onPaidRef.current(); } else if (res.status === "FAILED" || res.status === "EXPIRED") { stop(); onFailedRef.current(res.status); } // PENDING → keep polling. }) .catch(() => { // Transient poll error — swallow and let the next tick retry. }); }, intervalMs); timeoutId = setTimeout(() => { if (done) return; done = true; clearInterval(intervalId); setPolling(false); void Promise.resolve(cancelRef.current(uuid)).finally(() => onFailedRef.current("EXPIRED")); }, timeoutMs); return () => { done = true; clearInterval(intervalId); clearTimeout(timeoutId); }; }, [uuid, timeoutMs, intervalMs]); /* eslint-enable react-hooks/set-state-in-effect */ return { polling, elapsed }; }