Skip to content

Commit 8946ff8

Browse files
KyrylRclaude
andcommitted
fix(web): numbered thread shortcuts reach unsent drafts
Unsent drafts are listed at the top of the sidebar but never got a Cmd+1..9 badge, and Cmd+1 skipped past them to the first thread. Drafts now take the first numbers and threads continue after them, with the badges, the jump targets and the draft rows driven by one shared list. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4c36119 commit 8946ff8

5 files changed

Lines changed: 254 additions & 70 deletions

File tree

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

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { DEFAULT_CLIENT_SETTINGS, EnvironmentId, ProjectId } from "@t3tools/contracts";
1+
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2+
import { DEFAULT_CLIENT_SETTINGS, EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
23
import { describe, expect, it, vi } from "vite-plus/test";
34
import { userEvent } from "vite-plus/test/browser";
45

5-
import { render } from "../../test/render";
6+
import { act, render } from "../../test/render";
67
import { openCommandPalette } from "../commandPaletteBus";
8+
import { DraftId, useComposerDraftStore } from "../composerDraftStore";
79
import { useHandleNewThread } from "../hooks/useHandleNewThread";
10+
import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
811
import { __setClientSettingsForTests, getClientSettings } from "../hooks/useSettings";
912
import { useProjects } from "../state/entities";
1013
import type { Project } from "../types";
@@ -13,10 +16,21 @@ import { SidebarProvider } from "./ui/sidebar";
1316

1417
vi.mock("../state/entities", { spy: true });
1518
vi.mock("../hooks/useHandleNewThread", { spy: true });
19+
vi.mock("../hooks/useThreadRouteTarget", { spy: true });
1620
vi.mock("../commandPaletteBus", () => ({
1721
openCommandPalette: vi.fn(),
1822
isCommandPaletteOpen: () => false,
1923
}));
24+
vi.mock(import("../state/server"), async (original) => {
25+
const { Atom } = await import("effect/reactivity");
26+
const { compileResolvedKeybindingsConfig } = await import("@t3tools/shared/keybindings");
27+
// The default jumps are desktop-only; the test runs in a browser.
28+
const keybindings = compileResolvedKeybindingsConfig([
29+
{ key: "mod+1", command: "thread.jump.1" },
30+
{ key: "mod+2", command: "thread.jump.2" },
31+
]);
32+
return { ...(await original()), primaryServerKeybindingsAtom: Atom.make(keybindings) };
33+
});
2034
vi.mock("./sidebar/SidebarChrome", () => ({
2135
SidebarChromeHeader: () => null,
2236
SidebarChromeFooter: () => null,
@@ -86,3 +100,73 @@ describe("sidebar header new thread", () => {
86100
}
87101
});
88102
});
103+
104+
// regression: unsent drafts were listed above threads but skipped by mod+1..9.
105+
describe("sidebar numbered jumps", () => {
106+
it("number unsent drafts from the top, keep the open draft's number, and open them", async () => {
107+
const initial = useComposerDraftStore.getState();
108+
using stack = new DisposableStack();
109+
stack.defer(() => useComposerDraftStore.setState(initial, true));
110+
const previousSettings = getClientSettings();
111+
stack.defer(() => __setClientSettingsForTests(previousSettings));
112+
__setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
113+
using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
114+
const web = project("web");
115+
vi.mocked(useProjects).mockReturnValue([web]);
116+
vi.mocked(useHandleNewThread).mockReturnValue({
117+
activeDraftThread: null,
118+
activeThread: null,
119+
defaultProjectRef: null,
120+
handleNewThread: vi.fn(),
121+
routeDraftId: null,
122+
routeThreadRef: null,
123+
});
124+
const projectRef = scopeProjectRef(web.environmentId, web.id);
125+
const older = DraftId.make("older-draft");
126+
const newer = DraftId.make("newer-draft");
127+
// The newer draft is open; the test router has no draft route to read it from.
128+
vi.mocked(useThreadRouteTarget).mockReturnValue({ kind: "draft", draftId: newer });
129+
stack.defer(() => vi.mocked(useThreadRouteTarget).mockRestore());
130+
useComposerDraftStore.setState({
131+
draftsByThreadKey: {},
132+
draftThreadsByThreadKey: {},
133+
logicalProjectDraftThreadKeyByLogicalProjectKey: {},
134+
});
135+
for (const [draftId, createdAt] of [
136+
[older, "2026-01-01T00:00:00.000Z"],
137+
[newer, "2026-01-02T00:00:00.000Z"],
138+
] as const) {
139+
initial.setProjectDraftThreadId(projectRef, draftId, {
140+
threadId: ThreadId.make(`${draftId}-thread`),
141+
createdAt,
142+
});
143+
initial.setPrompt(draftId, `${draftId} prompt`);
144+
}
145+
146+
await using screen = await render(
147+
<SidebarProvider>
148+
<Sidebar routeReveal={{ observedKey: null, pendingKey: null }} />
149+
</SidebarProvider>,
150+
);
151+
const rows = screen.getByTestId("sidebar-draft-row");
152+
await expect.element(rows.nth(1)).toHaveAccessibleName("older-draft prompt, Unsent draft, web");
153+
await userEvent.keyboard("{Control>}");
154+
await expect.element(rows.nth(0).getByText("Ctrl+1")).toBeVisible();
155+
await expect.element(rows.nth(1).getByText("Ctrl+2")).toBeVisible();
156+
await userEvent.keyboard("{/Control}");
157+
158+
// Clearing the open draft keeps its row and number, even after search hides the list.
159+
await act(() => initial.setPrompt(newer, ""));
160+
const search = screen.getByRole("combobox", { name: "Search threads" });
161+
await userEvent.fill(search, "older");
162+
await expect.element(rows.first()).not.toBeInTheDocument();
163+
await userEvent.clear(search);
164+
await expect.element(rows.nth(0)).toHaveAccessibleName("newer-draft prompt, Unsent draft, web");
165+
await userEvent.keyboard("{Control>}");
166+
await expect.element(rows.nth(0).getByText("Ctrl+1")).toBeVisible();
167+
await expect.element(rows.nth(1).getByText("Ctrl+2")).toBeVisible();
168+
await userEvent.keyboard("2{/Control}");
169+
170+
await vi.waitFor(() => expect(screen.router!.state.location.pathname).toBe(`/draft/${older}`));
171+
});
172+
});

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ import {
263263
type SidebarProviderEntry,
264264
type SidebarSweepAction,
265265
} from "./sidebar/SidebarThreadRow";
266-
import { SidebarDraftBlock } from "./sidebar/SidebarDraftBlock";
266+
import { SidebarDraftBlock, useSidebarDrafts } from "./sidebar/SidebarDraftBlock";
267267
import {
268268
SidebarProjectFolderHeader,
269269
sidebarFolderRowId,
@@ -324,6 +324,7 @@ const sidebarProviderEntriesAtom = Atom.make((get) => {
324324
}).pipe(Atom.withLabel("sidebar-provider-entries"));
325325

326326
const EMPTY_PROVIDER_ENTRIES: ReadonlyMap<string, SidebarProviderEntry> = new Map();
327+
const EMPTY_DRAFT_IDS: readonly DraftId[] = [];
327328
const EMPTY_THREADS: readonly EnvironmentThreadShell[] = [];
328329

329330
const SETTLED_TAIL_INITIAL_COUNT = 10;
@@ -1427,10 +1428,14 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
14271428
const settledThreadKeysRef = useRef(settledThreadKeys);
14281429
const snoozedThreadKeysRef = useRef(snoozedThreadKeys);
14291430

1431+
const { draftIds, routeDraftComposer } = useSidebarDrafts(scopedProjectKeys, routeDraftIdForRows);
1432+
// Numbered jumps follow the rows on screen: drafts first, then threads.
1433+
// Search hides the draft rows, so they take no numbers while it is open.
1434+
const jumpDraftIds = isSearchingThreads ? EMPTY_DRAFT_IDS : draftIds;
14301435
const jumpLabelByKey = (() => {
14311436
const mapping = new Map<string, string>();
14321437
for (const [index, threadKey] of orderedThreadKeys.entries()) {
1433-
const jumpCommand = threadJumpCommandForIndex(index);
1438+
const jumpCommand = threadJumpCommandForIndex(jumpDraftIds.length + index);
14341439
if (!jumpCommand) break;
14351440
const label = shortcutLabelForCommand(keybindings, jumpCommand);
14361441
if (label) mapping.set(threadKey, label);
@@ -2928,12 +2933,21 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
29282933
}
29292934
const jumpIndex = threadJumpIndexFromCommand(command ?? "");
29302935
if (jumpIndex === null) return;
2931-
navigateToThreadKey(orderedThreadKeys[jumpIndex] ?? null);
2936+
const jumpDraftId = jumpDraftIds[jumpIndex];
2937+
if (jumpDraftId !== undefined) {
2938+
event.preventDefault();
2939+
event.stopPropagation();
2940+
navigateToDraft(jumpDraftId);
2941+
return;
2942+
}
2943+
navigateToThreadKey(orderedThreadKeys[jumpIndex - jumpDraftIds.length] ?? null);
29322944
};
29332945
window.addEventListener("keydown", onWindowKeyDown);
29342946
return () => window.removeEventListener("keydown", onWindowKeyDown);
29352947
}, [
2948+
jumpDraftIds,
29362949
keybindings,
2950+
navigateToDraft,
29372951
navigateToThread,
29382952
orderedThreadKeys,
29392953
routePreviewOpen,
@@ -2959,6 +2973,12 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
29592973
},
29602974
);
29612975
const showJumpHints = useThreadJumpHintsVisible(shouldShowJumpHintsNow);
2976+
const draftJumpLabels = showJumpHints
2977+
? jumpDraftIds.map((_, index) => {
2978+
const jumpCommand = threadJumpCommandForIndex(index);
2979+
return jumpCommand ? shortcutLabelForCommand(keybindings, jumpCommand) : null;
2980+
})
2981+
: null;
29622982

29632983
const handleNewThreadClick = (event?: ReactMouseEvent) => {
29642984
if (
@@ -3464,9 +3484,11 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
34643484
const items: ReactNode[] = [
34653485
<SidebarDraftBlock
34663486
key="draft-sessions"
3487+
draftIds={draftIds}
3488+
routeDraftComposer={routeDraftComposer}
3489+
jumpLabels={draftJumpLabels}
34673490
projectByKey={projectByKey}
34683491
projectDisplayNameByKey={projectDisplayNameByKey}
3469-
scopedProjectKeys={scopedProjectKeys}
34703492
routeDraftId={routeDraftIdForRows}
34713493
onNavigateToDraft={navigateToDraft}
34723494
onDraftContextMenu={handleDraftContextMenu}

‎apps/web/src/components/sidebar/SidebarDraftBlock.browser.test.tsx‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ import { expect, it, vi } from "vite-plus/test";
55

66
import { act, render } from "../../../test/render";
77
import { DraftId, useComposerDraftStore } from "../../composerDraftStore";
8-
import { SidebarDraftBlock } from "./SidebarDraftBlock";
8+
import { SidebarDraftBlock, useSidebarDrafts } from "./SidebarDraftBlock";
9+
10+
function DraftBlock(props: { routeDraftId: string | null }) {
11+
const { draftIds, routeDraftComposer } = useSidebarDrafts(null, props.routeDraftId);
12+
return (
13+
<SidebarDraftBlock
14+
draftIds={draftIds}
15+
routeDraftComposer={routeDraftComposer}
16+
jumpLabels={null}
17+
projectByKey={new Map()}
18+
projectDisplayNameByKey={new Map()}
19+
routeDraftId={props.routeDraftId}
20+
onNavigateToDraft={vi.fn()}
21+
onDraftContextMenu={vi.fn()}
22+
/>
23+
);
24+
}
925

1026
it("keeps draft previews current through edits, promotion and removal", async () => {
1127
const initial = useComposerDraftStore.getState();
@@ -25,14 +41,7 @@ it("keeps draft previews current through edits, promotion and removal", async ()
2541
await using screen = await render(
2642
<Profiler id="draft-block" onRender={onRender}>
2743
<ul>
28-
<SidebarDraftBlock
29-
projectByKey={new Map()}
30-
projectDisplayNameByKey={new Map()}
31-
scopedProjectKeys={null}
32-
routeDraftId={null}
33-
onNavigateToDraft={vi.fn()}
34-
onDraftContextMenu={vi.fn()}
35-
/>
44+
<DraftBlock routeDraftId={null} />
3645
</ul>
3746
</Profiler>,
3847
);
@@ -65,4 +74,28 @@ it("keeps draft previews current through edits, promotion and removal", async ()
6574
await expect.element(row).toHaveAccessibleName("Next draft preview, Unsent draft");
6675
await act(() => initial.clearDraftThread(nextDraftId));
6776
await expect.element(row).not.toBeInTheDocument();
77+
78+
// The open draft keeps the row it had on arrival, so numbered jumps stay put.
79+
const routeDraftId = DraftId.make("route-draft-preview");
80+
await act(() => {
81+
initial.setProjectDraftThreadId(projectRef, routeDraftId, { threadId: ThreadId.make("route") });
82+
initial.setPrompt(routeDraftId, "Route draft preview");
83+
});
84+
await screen.rerender(
85+
<Profiler id="draft-block" onRender={onRender}>
86+
<ul>
87+
<DraftBlock routeDraftId={routeDraftId} />
88+
</ul>
89+
</Profiler>,
90+
);
91+
await act(() => initial.setPrompt(routeDraftId, ""));
92+
await expect.element(row).toHaveAccessibleName("Route draft preview, Unsent draft");
93+
await screen.rerender(
94+
<Profiler id="draft-block" onRender={onRender}>
95+
<ul>
96+
<DraftBlock routeDraftId={null} />
97+
</ul>
98+
</Profiler>,
99+
);
100+
await expect.element(row).not.toBeInTheDocument();
68101
});

0 commit comments

Comments
 (0)