Skip to content

Commit b051f62

Browse files
committed
refactor(landing/QueryLanding): keep the gauge sweep state in one ref
1 parent fba219c commit b051f62

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

src/components/landing/QueryLanding.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ function QueryCachePanel() {
221221
const bumpAttemptRef = React.useRef(0)
222222
// Where each gauge stood when its entry was last written, so the sweep after
223223
// a write starts from the bar's current position instead of from empty.
224-
const sweepFromRef = React.useRef<Record<string, number>>({})
225-
const lastWriteRef = React.useRef<Record<string, number>>({})
226-
const shownRef = React.useRef<Record<string, number>>({})
224+
const gaugeRef = React.useRef<
225+
Record<string, { from: number; shown: number; writtenAt: number }>
226+
>({})
227227
// Starts paused so the server render matches the first client render; the
228228
// effect below turns it on unless the visitor asked for reduced motion.
229229
const [isLive, setIsLive] = React.useState(false)
@@ -312,15 +312,18 @@ function QueryCachePanel() {
312312
// A cache write sweeps the bar from wherever it stood up to the freshness
313313
// the entry actually has. Anchoring the sweep to the last painted value
314314
// keeps a mid-drain write from collapsing the bar before it climbs again.
315-
const writtenAt = query.dataUpdatedAt
316-
if (lastWriteRef.current[row.id] !== writtenAt) {
317-
sweepFromRef.current[row.id] = shownRef.current[row.id] ?? 0
318-
lastWriteRef.current[row.id] = writtenAt
315+
const gauge = (gaugeRef.current[row.id] ??= {
316+
from: 0,
317+
shown: 0,
318+
writtenAt: query.dataUpdatedAt,
319+
})
320+
if (gauge.writtenAt !== query.dataUpdatedAt) {
321+
gauge.from = gauge.shown
322+
gauge.writtenAt = query.dataUpdatedAt
319323
}
320324
const sweep = Math.min(1, elapsed / queryHeroRefillMs)
321-
const from = sweepFromRef.current[row.id] ?? 0
322-
const freshness = from + (drained - from) * sweep
323-
shownRef.current[row.id] = freshness
325+
const freshness = gauge.from + (drained - gauge.from) * sweep
326+
gauge.shown = freshness
324327
return {
325328
...row,
326329
query,

0 commit comments

Comments
 (0)