MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / useLiveStats

Function useLiveStats

freebuff/web/src/app/live/live-stats-client.ts:25–87  ·  view source on GitHub ↗
(
  initialStats: FreebuffLiveStats,
  options: {
    enabled?: boolean
    pauseWhenHidden?: boolean
    refreshOnMount?: boolean
  } = {},
)

Source from the content-addressed store, hash-verified

23}
24
25export function useLiveStats(
26 initialStats: FreebuffLiveStats,
27 options: {
28 enabled?: boolean
29 pauseWhenHidden?: boolean
30 refreshOnMount?: boolean
31 } = {},
32) {
33 const {
34 enabled = true,
35 pauseWhenHidden = false,
36 refreshOnMount = false,
37 } = options
38 const [stats, setStats] = useState(initialStats)
39
40 useEffect(() => {
41 if (!enabled) {
42 return
43 }
44
45 let isMounted = true
46
47 async function refresh() {
48 if (pauseWhenHidden && document.visibilityState === 'hidden') {
49 return
50 }
51
52 try {
53 const response = await fetch('/api/live', { cache: 'no-store' })
54 if (response.ok && isMounted) {
55 setStats((await response.json()) as FreebuffLiveStats)
56 }
57 } catch {
58 // Keep the previous snapshot if a transient refresh fails.
59 }
60 }
61
62 if (refreshOnMount) {
63 void refresh()
64 }
65
66 const interval = window.setInterval(refresh, POLL_MS)
67 const refreshWhenVisible = () => {
68 if (document.visibilityState === 'visible') {
69 void refresh()
70 }
71 }
72
73 if (pauseWhenHidden) {
74 document.addEventListener('visibilitychange', refreshWhenVisible)
75 }
76
77 return () => {
78 isMounted = false
79 window.clearInterval(interval)
80 if (pauseWhenHidden) {
81 document.removeEventListener('visibilitychange', refreshWhenVisible)
82 }

Callers 3

useHomepageLiveStatsFunction · 0.90
CompactLiveStatsFunction · 0.90
LiveClientFunction · 0.90

Calls 1

refreshFunction · 0.85

Tested by

no test coverage detected