Skip to content

Commit c246f0d

Browse files
committed
2 parents 0dede17 + bc55a4c commit c246f0d

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

src/components/ParentSize.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ export function ParentSize({ children, className }: ParentSizeProps) {
3333
}, [])
3434

3535
return (
36-
<div ref={containerRef} className={className} style={{ width: '100%', height: '100%' }}>
36+
<div
37+
ref={containerRef}
38+
className={className}
39+
style={{ width: '100%', height: '100%' }}
40+
>
3741
{size.width > 0 && children(size)}
3842
</div>
3943
)

src/hooks/useNpmDownloadCounter.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import type { NpmStats } from '~/utils/stats.server'
55
* Hook to animate NPM download count using direct DOM updates.
66
* Uses setInterval to minimize overhead - updates only when count changes.
77
*/
8-
export function useNpmDownloadCounter(npmData: NpmStats): React.RefCallback<HTMLElement> {
8+
export function useNpmDownloadCounter(
9+
npmData: NpmStats,
10+
): React.RefCallback<HTMLElement> {
911
const baseCount = npmData.totalDownloads ?? 0
1012
const ratePerDay = npmData.ratePerDay ?? 0
1113
const updatedAt = npmData.updatedAt ?? Date.now()
1214

13-
const intervalRef = useRef<ReturnType<typeof setInterval> | undefined>(undefined)
15+
const intervalRef = useRef<ReturnType<typeof setInterval> | undefined>(
16+
undefined,
17+
)
1418
const elementRef = useRef<HTMLElement | null>(null)
1519
const lastCountRef = useRef<number | null>(null)
1620

@@ -48,17 +52,20 @@ export function useNpmDownloadCounter(npmData: NpmStats): React.RefCallback<HTML
4852
}
4953
}, [baseCount, ratePerDay, updatedAt])
5054

51-
const refCallback = useCallback((node: HTMLElement | null) => {
52-
elementRef.current = node
53-
if (node) {
54-
const msPerDay = 24 * 60 * 60 * 1000
55-
const ratePerMs = ratePerDay / msPerDay
56-
const elapsedMs = Date.now() - updatedAt
57-
const count = Math.round(baseCount + ratePerMs * elapsedMs)
58-
lastCountRef.current = count
59-
node.textContent = count.toLocaleString()
60-
}
61-
}, [baseCount, ratePerDay, updatedAt])
55+
const refCallback = useCallback(
56+
(node: HTMLElement | null) => {
57+
elementRef.current = node
58+
if (node) {
59+
const msPerDay = 24 * 60 * 60 * 1000
60+
const ratePerMs = ratePerDay / msPerDay
61+
const elapsedMs = Date.now() - updatedAt
62+
const count = Math.round(baseCount + ratePerMs * elapsedMs)
63+
lastCountRef.current = count
64+
node.textContent = count.toLocaleString()
65+
}
66+
},
67+
[baseCount, ratePerDay, updatedAt],
68+
)
6269

6370
return refCallback
6471
}

0 commit comments

Comments
 (0)