import { useState, useEffect, useCallback, useRef } from 'react' import { listSlipQueuePage } from '@/api/slip-queue' import type { SlipQueueItem, SlipStatus } from '@/api/slip-queue' const DEFAULT_PAGE_SIZE = 30 // pending/processing can change without any user action at all (the cron // sweep does it) — re-fetch the currently-loaded window on an interval so // items visibly move on without a manual refresh. Every other status only // changes via an explicit owner action, which the caller reflects locally // via removeItem/updateItem instead. const LIVE_STATUSES = new Set(['pending', 'processing']) const POLL_INTERVAL_MS = 5000 export function useSlipQueueTab(status: SlipStatus, pageSize = DEFAULT_PAGE_SIZE) { const [items, setItems] = useState([]) const [hasMore, setHasMore] = useState(false) const [loading, setLoading] = useState(true) const [loadingMore, setLoadingMore] = useState(false) const [error, setError] = useState(null) // Re-entrancy guard for loadMore, tracked outside React state on purpose: // setting state (setLoadingMore) only affects what gets *rendered* on the // next pass — it has no bearing on whether a concurrently-invoked call to // loadMore keeps executing. An IntersectionObserver can fire loadMore // again while the previous call's fetch is still in flight (e.g. the // sentinel stays intersecting through a slow request), and without a // synchronous gate that second call would fire another request with the // same `after` cursor, appending a duplicate page. loadingRef/hasMoreRef // are read-and-set synchronously at the top of loadMore, before any async // work starts, so a second call bails out immediately instead of racing. const loadingRef = useRef(false) const hasMoreRef = useRef(hasMore) useEffect(() => { hasMoreRef.current = hasMore }, [hasMore]) // Mirrors `items` for loadMore's synchronous cursor read below. A // setItems(prev => { ...; return prev }) "peek" looked equivalent but // isn't reliable: it depends on React's eager-state bailout invoking the // updater inline, which only happens when the fiber has no other pending // update queued — e.g. NOT guaranteed if the poll-driven `load()` below // has a setLoading/setError update in flight on the same fiber when // loadMore fires. Keeping a plain ref in sync at every place `items` is // set (mirroring the hasMoreRef pattern above) makes the read correct // regardless of React's internal scheduling. const itemsRef = useRef(items) // `silent: true` refreshes the loaded window without flipping the `loading` // flag. The consumer (SlipQueueTabPanel) renders a full-panel "Loading…" // placeholder whenever `loading` is true, so a non-silent refresh from the // 5s live-status poll would blank the whole card grid every tick. Only the // loading toggle is suppressed — errors from a silent refresh still surface. const load = useCallback(async (opts: { silent?: boolean } = {}) => { if (!opts.silent) setLoading(true) setError(null) try { const page = await listSlipQueuePage(status, { limit: pageSize }) setItems(page.items) itemsRef.current = page.items setHasMore(page.hasMore) hasMoreRef.current = page.hasMore } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load queue') } finally { if (!opts.silent) setLoading(false) } }, [status, pageSize]) const loadMore = useCallback(async () => { if (loadingRef.current || !hasMoreRef.current) return // A one-time read of "what's the last item right now" to build the // request's cursor — fine to take before the await starts, since the // cursor itself doesn't need to reflect anything that happens later. const currentItems = itemsRef.current if (currentItems.length === 0) return loadingRef.current = true setLoadingMore(true) setError(null) const last = currentItems[currentItems.length - 1]! try { const page = await listSlipQueuePage(status, { limit: pageSize, after: { createdAt: last.created_at, id: last.id } }) // The WRITE, unlike the cursor read above, must not reuse that // pre-await snapshot: a concurrent load() (the live-status poll) or // removeItem/updateItem (an owner action mid-scroll) can land while // this request is in flight, and appending onto the stale snapshot // would silently discard that concurrent change. A functional // updater reads the true state at commit time instead. setItems(prev => { const next = [...prev, ...page.items] itemsRef.current = next return next }) setHasMore(page.hasMore) hasMoreRef.current = page.hasMore } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load more') } finally { loadingRef.current = false setLoadingMore(false) } }, [status, pageSize]) useEffect(() => { void load() }, [load]) useEffect(() => { if (!LIVE_STATUSES.has(status)) return const interval = setInterval(() => { void load({ silent: true }) }, POLL_INTERVAL_MS) return () => clearInterval(interval) }, [status, load]) const removeItem = useCallback((id: string) => { setItems(prev => { const next = prev.filter(i => i.id !== id) itemsRef.current = next return next }) }, []) const updateItem = useCallback((id: string, updated: SlipQueueItem) => { setItems(prev => { const next = prev.map(i => (i.id === id ? updated : i)) itemsRef.current = next return next }) }, []) return { items, hasMore, loading, loadingMore, error, loadMore, reload: load, removeItem, updateItem } }