Skip to content

Commit f235bce

Browse files
fix(web): undo notice announces without a DOM and active-row reveal is tested
announce() returns early when no document exists, so the undo store can announce from unit tests. Both sidebars share useActiveRowReveal, covered by a browser test for route changes, late-mounting rows and remounts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bdba53f commit f235bce

5 files changed

Lines changed: 109 additions & 29 deletions

File tree

‎apps/web/src/components/LegacySidebar.tsx‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,7 @@ import {
236236
} from "../sidebarProjectGrouping";
237237
import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
238238
import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
239-
import {
240-
completeSidebarRouteReveal,
241-
pendingSidebarRouteReveal,
242-
type SidebarRouteReveal,
243-
} from "./sidebar/routeReveal";
239+
import { type SidebarRouteReveal, useActiveRowReveal } from "./sidebar/routeReveal";
244240
const SIDEBAR_SORT_LABELS: Record<SidebarProjectSortOrder, string> = {
245241
updated_at: "Last user message",
246242
created_at: "Created at",
@@ -3339,19 +3335,7 @@ export default function LegacySidebar({ routeReveal }: { routeReveal: SidebarRou
33393335
sidebarThreadPreviewCount,
33403336
expandedThreadListsByProject,
33413337
});
3342-
// History and the command palette can open a thread scrolled out of view,
3343-
// and a remount starts the list at the top, so each route change and each
3344-
// mount scroll to the open thread once. Its row can mount after the route
3345-
// does, once its environment's shells arrive, so the reveal waits for it.
3346-
const mountRevealOwed = useRef(true);
3347-
const revealActiveRow = (row: HTMLElement | null, threadKey: string) => {
3348-
if (row === null) return;
3349-
const pendingKey = pendingSidebarRouteReveal(routeReveal, threadKey);
3350-
if (pendingKey === null && !mountRevealOwed.current) return;
3351-
completeSidebarRouteReveal(routeReveal);
3352-
mountRevealOwed.current = false;
3353-
row.scrollIntoView({ block: "nearest" });
3354-
};
3338+
const revealActiveRow = useActiveRowReveal(routeReveal);
33553339
const newThreadShortcutLabelOptions = {
33563340
platform,
33573341
context: {

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,9 @@ import { ProjectFavicon } from "./ProjectFavicon";
201201
import { deriveProviderEntriesByEnvironment } from "../providerInstances";
202202
import { useStableCallback } from "../hooks/useStableCallback";
203203
import {
204-
completeSidebarRouteReveal,
205-
pendingSidebarRouteReveal,
206204
resolveSidebarRouteReveal,
207205
type SidebarRouteReveal,
206+
useActiveRowReveal,
208207
} from "./sidebar/routeReveal";
209208
import { stackedThreadToast, toastManager } from "./ui/toast";
210209
import { Button } from "./ui/button";
@@ -1009,14 +1008,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
10091008
clearSelection();
10101009
setPersistedProjectScopeKey(null);
10111010
}, [clearSelection, revealWidensScope, setPersistedProjectScopeKey]);
1012-
const mountRevealOwed = useRef(true);
1013-
const revealActiveRow = (row: HTMLElement, rowKey: string) => {
1014-
const pendingKey = pendingSidebarRouteReveal(routeReveal, rowKey);
1015-
if (pendingKey === null && !mountRevealOwed.current) return;
1016-
completeSidebarRouteReveal(routeReveal);
1017-
mountRevealOwed.current = false;
1018-
row.scrollIntoView({ block: "nearest" });
1019-
};
1011+
const revealActiveRow = useActiveRowReveal(routeReveal);
10201012
const showProjectEnvironments = projectGroupsSpanEnvironments(projectGroups);
10211013
const scratchMachineLabelFor = (
10221014
thread: Pick<SidebarThreadSummary, "environmentId" | "projectId">,
@@ -1491,7 +1483,8 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
14911483
motion.update(false);
14921484
return () => {
14931485
// Search replaces this list while the Sidebar stays mounted, and a drag
1494-
// whose rows are gone can never drop.
1486+
// whose rows are gone can never drop. The compiler memoizes this callback
1487+
// with no deps (it reads only refs), so the cleanup runs only on unmount.
14951488
dragSensorRef.current?.cancel();
14961489
motion.dispose();
14971490
if (listMotionRef.current === motion) listMotionRef.current = null;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { render } from "../../../test/render";
4+
import { type SidebarRouteReveal, useActiveRowReveal } from "./routeReveal";
5+
6+
const ROW_HEIGHT_PX = 24;
7+
8+
function RevealList(props: {
9+
readonly reveal: SidebarRouteReveal;
10+
readonly rowKeys: ReadonlyArray<string>;
11+
readonly activeKey: string | null;
12+
}) {
13+
const revealActiveRow = useActiveRowReveal(props.reveal);
14+
return (
15+
<ul aria-label="Threads" style={{ height: ROW_HEIGHT_PX * 4, overflowY: "auto", margin: 0 }}>
16+
{props.rowKeys.map((key) => (
17+
<li
18+
key={key}
19+
ref={key === props.activeKey ? (row) => revealActiveRow(row, key) : undefined}
20+
style={{ height: ROW_HEIGHT_PX }}
21+
>
22+
{key}
23+
</li>
24+
))}
25+
</ul>
26+
);
27+
}
28+
29+
const rowKeys = (count: number) => Array.from({ length: count }, (_, index) => `thread-${index}`);
30+
const newReveal = (): SidebarRouteReveal => ({ observedKey: null, pendingKey: null });
31+
32+
describe("useActiveRowReveal", () => {
33+
it("scrolls an off-screen row into view when the route opens its thread", async () => {
34+
const reveal = newReveal();
35+
const keys = rowKeys(50);
36+
// A bare list; the route arrives as the active key.
37+
await using screen = await render(
38+
<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
39+
{ router: false },
40+
);
41+
const target = screen.getByText("thread-40", { exact: true });
42+
await expect.element(target).not.toBeInViewport();
43+
44+
await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />);
45+
46+
await expect.element(target).toBeInViewport();
47+
});
48+
49+
it("reveals the active row when it mounts after the shells arrive", async () => {
50+
const reveal = newReveal();
51+
// The route already names the thread, but its environment's shells have not
52+
// listed it yet.
53+
await using screen = await render(
54+
<RevealList reveal={reveal} rowKeys={rowKeys(5)} activeKey="thread-40" />,
55+
{ router: false },
56+
);
57+
await expect.element(screen.getByText("thread-4", { exact: true })).toBeInTheDocument();
58+
59+
await screen.rerender(
60+
<RevealList reveal={reveal} rowKeys={rowKeys(50)} activeKey="thread-40" />,
61+
);
62+
63+
await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
64+
});
65+
66+
it("reveals the open thread again when the sidebar remounts on the same route", async () => {
67+
const reveal = newReveal();
68+
const keys = rowKeys(50);
69+
{
70+
await using screen = await render(
71+
<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />,
72+
{ router: false },
73+
);
74+
await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
75+
}
76+
77+
// A remount starts the list at the top; the record outlives it with no reveal pending.
78+
await using screen = await render(
79+
<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />,
80+
{ router: false },
81+
);
82+
await expect.element(screen.getByText("thread-40", { exact: true })).toBeInViewport();
83+
});
84+
});

‎apps/web/src/components/sidebar/routeReveal.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useRef } from "react";
2+
13
import { filterSidebarV2VisibleThreads } from "../Sidebar.logic";
24

35
// AppSidebarLayout owns this so drawer and Settings remounts preserve the chosen project scope.
@@ -25,6 +27,21 @@ export function completeSidebarRouteReveal(reveal: SidebarRouteReveal): void {
2527
reveal.pendingKey = null;
2628
}
2729

30+
// Each route change and each mount scroll to the open thread once: a remount
31+
// starts the list at the top. The row can mount after the route, once its
32+
// environment's shells arrive, so the active row's ref callback does the scroll.
33+
export function useActiveRowReveal(reveal: SidebarRouteReveal) {
34+
const mountRevealOwed = useRef(true);
35+
return (row: HTMLElement | null, rowKey: string) => {
36+
if (row === null) return;
37+
const pendingKey = pendingSidebarRouteReveal(reveal, rowKey);
38+
if (pendingKey === null && !mountRevealOwed.current) return;
39+
completeSidebarRouteReveal(reveal);
40+
mountRevealOwed.current = false;
41+
row.scrollIntoView({ block: "nearest" });
42+
};
43+
}
44+
2845
type RevealableThread = Parameters<typeof filterSidebarV2VisibleThreads>[0][number];
2946

3047
// `reveal` is the render-time copy of the record. A route key it has not seen

‎apps/web/src/lib/announce.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ function createRegion() {
3737
* replaces one still waiting from the same `source`, so a superseded status isn't read out.
3838
*/
3939
export function announce(message: string, source?: string) {
40+
// Stores announce without a mounted owner, so they also run where no DOM exists (unit tests).
41+
if (typeof document === "undefined") return;
4042
if (!regions) {
4143
regions = [createRegion(), createRegion()];
4244
document.addEventListener("focusin", () => {

0 commit comments

Comments
 (0)