-
Notifications
You must be signed in to change notification settings - Fork 1
feat(frontend): smooth TPS chart transitions on new datapoints #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,107 @@ | ||||||||||
| 'use client' | ||||||||||
|
|
||||||||||
| import { useEffect, useRef, useState } from 'react' | ||||||||||
| import type { TpsDataPoint } from '@/hooks/use-tps' | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Matches the backend's TPS cadence (~one update per block at ~400ms). | ||||||||||
| * Picking the inter-arrival time lets the tween finish right as the next | ||||||||||
| * datapoint arrives, producing continuous motion instead of a snap. | ||||||||||
| */ | ||||||||||
| const ANIMATION_DURATION_MS = 400 | ||||||||||
|
|
||||||||||
| function easeOutCubic(t: number): number { | ||||||||||
| return 1 - (1 - t) ** 3 | ||||||||||
| } | ||||||||||
|
|
||||||||||
| interface AnimationState { | ||||||||||
| startTime: number | ||||||||||
| startValue: number | ||||||||||
| startTimestamp: number | ||||||||||
| targetValue: number | ||||||||||
| targetTimestamp: number | ||||||||||
| } | ||||||||||
|
|
||||||||||
| function pointAt(anim: AnimationState, now: number): TpsDataPoint { | ||||||||||
| const t = Math.min(1, (now - anim.startTime) / ANIMATION_DURATION_MS) | ||||||||||
| const eased = easeOutCubic(t) | ||||||||||
| return { | ||||||||||
| tps: anim.startValue + (anim.targetValue - anim.startValue) * eased, | ||||||||||
| timestamp: | ||||||||||
| anim.startTimestamp + | ||||||||||
| (anim.targetTimestamp - anim.startTimestamp) * eased, | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Tweens the trailing tip of a TPS history so the chart extends smoothly to | ||||||||||
| * each new datapoint rather than snapping. When a new point arrives mid-tween, | ||||||||||
| * the tween restarts from the current interpolated position so the line never | ||||||||||
| * jumps. | ||||||||||
| */ | ||||||||||
| export function useSmoothedTpsHistory(history: TpsDataPoint[]): TpsDataPoint[] { | ||||||||||
| const [rendered, setRendered] = useState<TpsDataPoint[]>(history) | ||||||||||
| const animRef = useRef<AnimationState | null>(null) | ||||||||||
| const rafRef = useRef<number | null>(null) | ||||||||||
|
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Rule Used: When using useRef with a default value in TypeScri... (source) Learned From Prompt To Fix With AIThis is a comment left during a code review.
Path: frontend/hooks/use-smoothed-tps-history.ts
Line: 44-45
Comment:
Both refs use explicit union types (`| null`) which violates the team convention of letting React infer the nullability. The rule calls for `useRef<AnimationState>(null)` and `useRef<number>(null)` — React's overload resolution already makes `.current` nullable when `null` is passed as the initial value.
```suggestion
const animRef = useRef<AnimationState>(null)
const rafRef = useRef<number>(null)
```
**Rule Used:** When using useRef with a default value in TypeScri... ([source](https://app.greptile.com/monad-foudnation/-/custom-context?memory=e5b39c00-8ef7-4612-a56a-d956ea833db7))
**Learned From**
[monad-developers/monapp#178](https://github.com/monad-developers/monapp/pull/178)
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||
|
|
||||||||||
| useEffect(() => { | ||||||||||
| if (history.length === 0) { | ||||||||||
| animRef.current = null | ||||||||||
| setRendered([]) | ||||||||||
| return | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const latest = history[history.length - 1] | ||||||||||
|
|
||||||||||
| if ( | ||||||||||
| animRef.current !== null && | ||||||||||
| animRef.current.targetTimestamp === latest.timestamp | ||||||||||
| ) { | ||||||||||
| return | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const prev = history[history.length - 2] | ||||||||||
| const now = performance.now() | ||||||||||
| const tip: TpsDataPoint = | ||||||||||
| animRef.current !== null | ||||||||||
| ? pointAt(animRef.current, now) | ||||||||||
| : { | ||||||||||
| tps: prev?.tps ?? latest.tps, | ||||||||||
| timestamp: prev?.timestamp ?? latest.timestamp, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| animRef.current = { | ||||||||||
| startTime: now, | ||||||||||
| startValue: tip.tps, | ||||||||||
| startTimestamp: tip.timestamp, | ||||||||||
| targetValue: latest.tps, | ||||||||||
| targetTimestamp: latest.timestamp, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const head = history.slice(0, -1) | ||||||||||
|
|
||||||||||
| const tick = () => { | ||||||||||
| const anim = animRef.current | ||||||||||
| if (anim === null) return | ||||||||||
| const current = pointAt(anim, performance.now()) | ||||||||||
| setRendered([...head, current]) | ||||||||||
| if (performance.now() - anim.startTime < ANIMATION_DURATION_MS) { | ||||||||||
| rafRef.current = requestAnimationFrame(tick) | ||||||||||
| } else { | ||||||||||
| rafRef.current = null | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
Camillebzd marked this conversation as resolved.
|
||||||||||
|
|
||||||||||
| if (rafRef.current !== null) cancelAnimationFrame(rafRef.current) | ||||||||||
| rafRef.current = requestAnimationFrame(tick) | ||||||||||
| }, [history]) | ||||||||||
|
|
||||||||||
| useEffect( | ||||||||||
| () => () => { | ||||||||||
| if (rafRef.current !== null) cancelAnimationFrame(rafRef.current) | ||||||||||
| }, | ||||||||||
| [], | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| return rendered | ||||||||||
| } | ||||||||||
Uh oh!
There was an error while loading. Please reload this page.