-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
|
||
interface NumberTickerProps { | ||
value: number | null | ||
duration?: number // duration in milliseconds | ||
className?: string | ||
} | ||
|
||
export function NumberTicker({ | ||
value, | ||
duration = 1000, // default animation duration: 1 second | ||
className = '', | ||
}: NumberTickerProps) { | ||
const [displayValue, setDisplayValue] = useState(value) | ||
const prevValueRef = useRef(value ?? 0) // Store the previous value | ||
const animationFrame = useRef<number>() | ||
|
||
useEffect(() => { | ||
if (typeof value !== 'number') { | ||
return | ||
} | ||
const startValue = prevValueRef.current // Previous value | ||
const targetValue = value // New value | ||
const startTime = performance.now() | ||
|
||
const animate = (currentTime: number) => { | ||
const elapsed = currentTime - startTime | ||
const progress = Math.min(elapsed / duration, 1) // Progress between 0 and 1 | ||
|
||
// Calculate interpolated value | ||
const interpolatedValue = Math.floor(startValue + (targetValue - startValue) * progress) | ||
setDisplayValue(interpolatedValue) | ||
|
||
if (progress < 1) { | ||
animationFrame.current = requestAnimationFrame(animate) | ||
} | ||
} | ||
|
||
// Start animation | ||
if (animationFrame.current) cancelAnimationFrame(animationFrame.current) | ||
animationFrame.current = requestAnimationFrame(animate) | ||
|
||
// Update the previous value reference | ||
prevValueRef.current = value | ||
|
||
return () => { | ||
if (animationFrame.current) cancelAnimationFrame(animationFrame.current) | ||
} | ||
}, [value, duration]) | ||
|
||
return <span className={className}>{displayValue?.toLocaleString('en') ?? '--,---,---'}</span> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,41 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import config from '../../../config' | ||
import styles from './index.module.scss' | ||
import { getKnowledgeSize } from './utils' | ||
import { NumberTicker } from '../../../components/ui/NumberTicker' | ||
|
||
export default () => <div className={styles.root} /> | ||
const { BACKUP_NODES: backupNodes } = config | ||
|
||
export default () => { | ||
const { data: size } = useQuery( | ||
['backup_nodes'], | ||
async () => { | ||
try { | ||
if (backupNodes.length === 0) return null | ||
|
||
const [size1, size2] = await Promise.race(backupNodes.map(getKnowledgeSize)) | ||
return size1 ?? size2 | ||
} catch { | ||
return null | ||
} | ||
}, | ||
{ refetchInterval: 12 * 1000 }, | ||
) | ||
return ( | ||
<div className={styles.root}> | ||
<a | ||
className={styles.knowledgeBase} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
href="https://talk.nervos.org/t/how-to-get-the-average-occupied-bytes-per-live-cell-in-ckb/7138/2?u=keith" | ||
> | ||
<span>Knowledge Size</span> | ||
<br /> | ||
<div className={styles.ticker}> | ||
<NumberTicker value={size ? +size : null} /> | ||
<span>CKBytes</span> | ||
</div> | ||
</a> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import BigNumber from 'bignumber.js' | ||
|
||
const EXCLUDE = BigNumber('504000000000000000') | ||
|
||
export const getKnowledgeSize = async (nodeUrl: string) => { | ||
const header = await fetch(nodeUrl, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
id: 1, | ||
jsonrpc: '2.0', | ||
method: 'get_tip_header', | ||
params: [], | ||
}), | ||
}) | ||
.then(res => res.json()) | ||
.then(res => res.result) | ||
const { dao } = header | ||
|
||
const [, , , u] = dao | ||
.slice(2) | ||
.match(/\w{16}/g) | ||
.map((i: string) => i.match(/\w{2}/g)?.reverse().join('') ?? '') | ||
const total = BigNumber(`0x${u}`).minus(EXCLUDE).toFormat() | ||
return total | ||
} |