Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web_app/src/hooks/useCountUp.ts from CommunityOne/open-navigator: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/spaces/CommunityOne/open-navigator/resolve/main/web_app/src/hooks/useCountUp.ts
- Command line
-
hf download hf://spaces/CommunityOne/open-navigator/web_app/src/hooks/useCountUp.ts
-
curl -L -o useCountUp.ts https://huggingface.co/spaces/CommunityOne/open-navigator/resolve/main/web_app/src/hooks/useCountUp.ts
1.2 kB
| import { useEffect, useState } from 'react' | |
| import { countUpValue } from '../utils/folioMotion' | |
| /** | |
| * Text for a KPI numeral. Counts up from 0 to `target` (~900 ms, ease-out) whenever the target changes and always ends on | |
| * `format(target)` exactly. A missing target shows "—" and never animates; reduced motion shows the final value at once. | |
| */ | |
| export function useCountUp(target: number | null, format: (v: number) => string, reduced: boolean, durationMs = 900): string { | |
| const [anim, setAnim] = useState<{ target: number | null; t: number }>({ target: null, t: 0 }) | |
| useEffect(() => { | |
| if (target == null || reduced) return | |
| let raf = 0 | |
| const start = performance.now() | |
| const tick = (now: number) => { | |
| const t = Math.min(1, (now - start) / durationMs) | |
| setAnim({ target, t }) | |
| if (t < 1) raf = requestAnimationFrame(tick) | |
| } | |
| raf = requestAnimationFrame(tick) | |
| return () => cancelAnimationFrame(raf) | |
| }, [target, reduced, durationMs]) | |
| if (target == null || !Number.isFinite(target)) return '—' | |
| if (reduced) return format(target) | |
| const t = anim.target === target ? anim.t : 0 | |
| return t >= 1 ? format(target) : format(countUpValue(target, t)) | |
| } | |