Skip to content

Commit 84f7454

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 337c585 commit 84f7454

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,
@@ -87,3 +101,73 @@ describe("sidebar header new thread", () => {
87101
}
88102
});
89103
});
104+
105+
// regression: unsent drafts were listed above threads but skipped by mod+1..9.
106+
describe("sidebar numbered jumps", () => {
107+
it("number unsent drafts from the top, keep the open draft's number, and open them", async () => {
108+
const initial = useComposerDraftStore.getState();
109+
using stack = new DisposableStack();
110+
stack.defer(() => useComposerDraftStore.setState(initial, true));
111+
const previousSettings = getClientSettings();
112+
stack.defer(() => __setClientSettingsForTests(previousSettings));
113+
__setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
114+
using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
115+
const web = project("web");
116+
vi.mocked(useProjects).mockReturnValue([web]);
117+
vi.mocked(useHandleNewThread).mockReturnValue({
118+
activeDraftThread: null,
119+
activeThread: null,
120+
defaultProjectRef: null,
121+
handleNewThread: vi.fn(),
122+
routeDraftId: null,
123+
routeThreadRef: null,
124+
});
125+
const projectRef = scopeProjectRef(web.environmentId, web.id);
126+
const older = DraftId.make("older-draft");
127+
const newer = DraftId.make("newer-draft");
128+
// The newer draft is open; the test router has no draft route to read it from.
129+
vi.mocked(useThreadRouteTarget).mockReturnValue({ kind: "draft", draftId: newer });
130+
stack.defer(() => vi.mocked(useThreadRouteTarget).mockRestore());
131+
useComposerDraftStore.setState({
132+
draftsByThreadKey: {},
133+
draftThreadsByThreadKey: {},
134+
logicalProjectDraftThreadKeyByLogicalProjectKey: {},
135+
});
136+
for (const [draftId, createdAt] of [
137+
[older, "2026-01-01T00:00:00.000Z"],
138+
[newer, "2026-01-02T00:00:00.000Z"],
139+
] as const) {
140+
initial.setProjectDraftThreadId(projectRef, draftId, {
141+
threadId: ThreadId.make(`${draftId}-thread`),
142+
createdAt,
143+
});
144+
initial.setPrompt(draftId, `${draftId} prompt`);
145+
}
146+
147+
await using screen = await render(
148+
<SidebarProvider>
149+
<Sidebar routeReveal={{ observedKey: null, pendingKey: null }} />
150+
</SidebarProvider>,
151+
);
152+
const rows = screen.getByTestId("sidebar-draft-row");
153+
await expect.element(rows.nth(1)).toHaveAccessibleName("older-draft prompt, Unsent draft, web");
154+
await userEvent.keyboard("{Control>}");
155+
await expect.element(rows.nth(0).getByText("Ctrl+1")).toBeVisible();
156+
await expect.element(rows.nth(1).getByText("Ctrl+2")).toBeVisible();
157+
await userEvent.keyboard("{/Control}");
158+
159+
// Clearing the open draft keeps its row and number, even after search hides the list.
160+
await act(() => initial.setPrompt(newer, ""));
161+
const search = screen.getByRole("combobox", { name: "Search threads" });
162+
await userEvent.fill(search, "older");
163+
await expect.element(rows.first()).not.toBeInTheDocument();
164+
await userEvent.clear(search);
165+
await expect.element(rows.nth(0)).toHaveAccessibleName("newer-draft prompt, Unsent draft, web");
166+
await userEvent.keyboard("{Control>}");
167+
await expect.element(rows.nth(0).getByText("Ctrl+1")).toBeVisible();
168+
await expect.element(rows.nth(1).getByText("Ctrl+2")).toBeVisible();
169+
await userEvent.keyboard("2{/Control}");
170+
171+
await vi.waitFor(() => expect(screen.router!.state.location.pathname).toBe(`/draft/${older}`));
172+
});
173+
});

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ import {
260260
type SidebarProviderEntry,
261261
type SidebarSweepAction,
262262
} from "./sidebar/SidebarThreadRow";
263-
import { SidebarDraftBlock } from "./sidebar/SidebarDraftBlock";
263+
import { SidebarDraftBlock, useSidebarDrafts } from "./sidebar/SidebarDraftBlock";
264264
import {
265265
SidebarProjectFolderHeader,
266266
sidebarFolderRowId,
@@ -321,6 +321,7 @@ const sidebarProviderEntriesAtom = Atom.make((get) => {
321321
}).pipe(Atom.withLabel("sidebar-provider-entries"));
322322

323323
const EMPTY_PROVIDER_ENTRIES: ReadonlyMap<string, SidebarProviderEntry> = new Map();
324+
const EMPTY_DRAFT_IDS: readonly DraftId[] = [];
324325
const EMPTY_THREADS: readonly EnvironmentThreadShell[] = [];
325326

326327
const SETTLED_TAIL_INITIAL_COUNT = 10;
@@ -1420,10 +1421,14 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
14201421
const settledThreadKeysRef = useRef(settledThreadKeys);
14211422
const snoozedThreadKeysRef = useRef(snoozedThreadKeys);
14221423

1424+
const { draftIds, routeDraftComposer } = useSidebarDrafts(scopedProjectKeys, routeDraftIdForRows);
1425+
// Numbered jumps follow the rows on screen: drafts first, then threads.
1426+
// Search hides the draft rows, so they take no numbers while it is open.
1427+
const jumpDraftIds = isSearchingThreads ? EMPTY_DRAFT_IDS : draftIds;
14231428
const jumpLabelByKey = (() => {
14241429
const mapping = new Map<string, string>();
14251430
for (const [index, threadKey] of orderedThreadKeys.entries()) {
1426-
const jumpCommand = threadJumpCommandForIndex(index);
1431+
const jumpCommand = threadJumpCommandForIndex(jumpDraftIds.length + index);
14271432
if (!jumpCommand) break;
14281433
const label = shortcutLabelForCommand(keybindings, jumpCommand);
14291434
if (label) mapping.set(threadKey, label);
@@ -2901,12 +2906,21 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
29012906
}
29022907
const jumpIndex = threadJumpIndexFromCommand(command ?? "");
29032908
if (jumpIndex === null) return;
2904-
navigateToThreadKey(orderedThreadKeys[jumpIndex] ?? null);
2909+
const jumpDraftId = jumpDraftIds[jumpIndex];
2910+
if (jumpDraftId !== undefined) {
2911+
event.preventDefault();
2912+
event.stopPropagation();
2913+
navigateToDraft(jumpDraftId);
2914+
return;
2915+
}
2916+
navigateToThreadKey(orderedThreadKeys[jumpIndex - jumpDraftIds.length] ?? null);
29052917
};
29062918
window.addEventListener("keydown", onWindowKeyDown);
29072919
return () => window.removeEventListener("keydown", onWindowKeyDown);
29082920
}, [
2921+
jumpDraftIds,
29092922
keybindings,
2923+
navigateToDraft,
29102924
navigateToThread,
29112925
orderedThreadKeys,
29122926
routeRowThreadKey,
@@ -2929,6 +2943,12 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
29292943
},
29302944
);
29312945
const showJumpHints = useThreadJumpHintsVisible(shouldShowJumpHintsNow);
2946+
const draftJumpLabels = showJumpHints
2947+
? jumpDraftIds.map((_, index) => {
2948+
const jumpCommand = threadJumpCommandForIndex(index);
2949+
return jumpCommand ? shortcutLabelForCommand(keybindings, jumpCommand) : null;
2950+
})
2951+
: null;
29322952

29332953
const handleNewThreadClick = (event?: ReactMouseEvent) => {
29342954
if (
@@ -3424,9 +3444,11 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
34243444
const items: ReactNode[] = [
34253445
<SidebarDraftBlock
34263446
key="draft-sessions"
3447+
draftIds={draftIds}
3448+
routeDraftComposer={routeDraftComposer}
3449+
jumpLabels={draftJumpLabels}
34273450
projectByKey={projectByKey}
34283451
projectDisplayNameByKey={projectDisplayNameByKey}
3429-
scopedProjectKeys={scopedProjectKeys}
34303452
routeDraftId={routeDraftIdForRows}
34313453
onNavigateToDraft={navigateToDraft}
34323454
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)