({ messages }: CachePillProps)
| 69 | }; |
| 70 | |
| 71 | function CachePill({ messages }: CachePillProps): React.ReactNode { |
| 72 | const [now, setNow] = useState(() => Date.now()); |
| 73 | const [isFlashOn, setIsFlashOn] = useState(true); |
| 74 | |
| 75 | const usage = getCurrentUsage(messages); |
| 76 | |
| 77 | // Feed new responses into the in-memory singleton |
| 78 | const prevSigRef = useRef<string | null>(null); |
| 79 | if (usage !== null) { |
| 80 | const sig = tokenSignature(usage); |
| 81 | if (sig !== prevSigRef.current) { |
| 82 | prevSigRef.current = sig; |
| 83 | cacheOnResponse(usage); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | const cacheState = getCacheStatsState(); |
| 88 | const { lastResetAt, lastHitRate } = cacheState; |
| 89 | |
| 90 | // Derived timing |
| 91 | const elapsed = lastResetAt !== null ? now - lastResetAt : null; |
| 92 | const remaining = elapsed !== null ? CACHE_TTL_MS - elapsed : null; |
| 93 | const elapsedMin = elapsed !== null ? elapsed / 60_000 : null; |
| 94 | const isExpired = remaining !== null && remaining <= 0; |
| 95 | |
| 96 | // 1-second countdown ticker |
| 97 | useEffect(() => { |
| 98 | const id = setInterval(() => setNow(Date.now()), 1000); |
| 99 | return () => clearInterval(id); |
| 100 | }, []); |
| 101 | |
| 102 | // 500ms flash in last 5 minutes |
| 103 | const inFlashZone = elapsedMin !== null && elapsedMin >= 55 && !isExpired; |
| 104 | useEffect(() => { |
| 105 | if (!inFlashZone) { |
| 106 | setIsFlashOn(true); |
| 107 | return; |
| 108 | } |
| 109 | const id = setInterval(() => setIsFlashOn(v => !v), 500); |
| 110 | return () => clearInterval(id); |
| 111 | }, [inFlashZone]); |
| 112 | |
| 113 | // Load persisted fallback once on mount |
| 114 | const initDoneRef = useRef(false); |
| 115 | useEffect(() => { |
| 116 | if (initDoneRef.current) return; |
| 117 | initDoneRef.current = true; |
| 118 | const sid = getSessionId(); |
| 119 | void initCacheStatsState(sid); |
| 120 | }, []); |
| 121 | |
| 122 | const displayHitRate = usage !== null ? computeHitRate(usage) : lastHitRate; |
| 123 | |
| 124 | // No data yet — show placeholder |
| 125 | if (displayHitRate === null && lastResetAt === null) { |
| 126 | return <Text dimColor>{' Cache --% --:--'}</Text>; |
| 127 | } |
| 128 |
nothing calls this directly
no test coverage detected