File size: 886 Bytes
747e1c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { useState, useEffect, useCallback } from 'react'
import { getSlipQueueCounts } from '@/api/slip-queue'
import type { SlipStatus } from '@/api/slip-queue'

// Polled independently of whichever tab is open — the badge numbers should
// stay live even while looking at a different tab's page.
const POLL_INTERVAL_MS = 5000

export function useSlipQueueCounts() {
  const [counts, setCounts] = useState<Record<SlipStatus, number> | null>(null)

  const refresh = useCallback(async () => {
    try {
      const data = await getSlipQueueCounts()
      setCounts(data)
    } catch {
      // Best-effort — badges just keep their last known values on failure.
    }
  }, [])

  useEffect(() => {
    void refresh()
    const interval = setInterval(() => { void refresh() }, POLL_INTERVAL_MS)
    return () => clearInterval(interval)
  }, [refresh])

  return { counts, refresh }
}