Skip to content

Commit e834f9e

Browse files
jdonelsonclaude
andcommitted
console: Keep the threshold handle still when a drag settles
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c7d58f4 commit e834f9e

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

‎console/src/components/ThresholdLineGraph/useThresholdControl.test.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
// by the Apache License, Version 2.0.
99

1010
import { act, renderHook, waitFor } from "@testing-library/react";
11-
import { describe, expect, it, vi } from "vitest";
11+
import React from "react";
12+
import { afterEach, describe, expect, it, vi } from "vitest";
1213

14+
import { THRESHOLD_DRAG_SETTLE_MS } from "./constants";
1315
import { useThresholdControl } from "./useThresholdControl";
1416

1517
describe("useThresholdControl", () => {
@@ -90,6 +92,52 @@ describe("useThresholdControl", () => {
9092
expect(result.current.inputProps.valueMs).toBe(9_000);
9193
});
9294

95+
describe("with a commit that lands in a transition", () => {
96+
// `BrowserRouter` navigates inside `startTransition`, so this is how a
97+
// search-param `commit` behaves on the page.
98+
const renderWithTransitionCommit = () => {
99+
const shown: number[] = [];
100+
const hook = renderHook(() => {
101+
const [committed, setCommitted] = React.useState(2_000);
102+
const control = useThresholdControl(committed, (value) =>
103+
React.startTransition(() => setCommitted(value)),
104+
);
105+
const threshold = control.graphProps.threshold;
106+
React.useLayoutEffect(() => {
107+
if (shown.at(-1) !== threshold) shown.push(threshold);
108+
});
109+
return control;
110+
});
111+
return { ...hook, shown };
112+
};
113+
114+
afterEach(() => vi.useRealTimers());
115+
116+
it("never shows the old value when a drag settles", () => {
117+
vi.useFakeTimers();
118+
const { result, shown } = renderWithTransitionCommit();
119+
120+
act(() => result.current.graphProps.onThresholdChange(5_000));
121+
act(() => vi.advanceTimersByTime(THRESHOLD_DRAG_SETTLE_MS));
122+
123+
expect(shown).toEqual([2_000, 5_000]);
124+
});
125+
126+
it("keeps a new drag that starts before the last one lands", () => {
127+
vi.useFakeTimers();
128+
const { result, shown } = renderWithTransitionCommit();
129+
130+
act(() => result.current.graphProps.onThresholdChange(5_000));
131+
act(() => {
132+
vi.advanceTimersByTime(THRESHOLD_DRAG_SETTLE_MS);
133+
result.current.graphProps.onThresholdChange(7_000);
134+
});
135+
136+
expect(shown.at(-1)).toBe(7_000);
137+
expect(shown.slice(1)).not.toContain(2_000);
138+
});
139+
});
140+
93141
it("keeps each prop object stable while nothing changes", () => {
94142
const commit = vi.fn();
95143
const { result, rerender } = renderHook(() =>

‎console/src/components/ThresholdLineGraph/useThresholdControl.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ export function useThresholdControl(
7676
setPending(value);
7777
clearTimeout(timer.current);
7878
timer.current = setTimeout(() => {
79-
// Both updates come from one callback, so React batches them into a
80-
// single render. Clearing `pending` later would show `committed`
81-
// before it caught up, which reads as the control springing back.
79+
// `commit` usually navigates, and `BrowserRouter` navigates inside a
80+
// transition. Clearing `pending` outside one renders first and shows
81+
// the old `committed` for a frame, which reads as the control
82+
// springing back.
8283
commitRef.current(value);
83-
setPending(undefined);
84+
React.startTransition(() => setPending(undefined));
8485
}, settleMs);
8586
},
8687
[commitRef],

0 commit comments

Comments
 (0)