Skip to content

Commit ade2325

Browse files
committed
feat(web): add a shortcut command for new threads on the active branch
1 parent 5691412 commit ade2325

11 files changed

Lines changed: 144 additions & 29 deletions

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ import { isScratchProject } from "@t3tools/client-runtime/state/projects";
111111
import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments";
112112
import { useProjects, useServerConfigs, useThreadShells, waitForProject } from "../state/entities";
113113
import { useThreadSearch } from "../state/queries";
114-
import { resolveThreadActionProjectRef, startNewThreadFromContext } from "../lib/chatThreadActions";
114+
import {
115+
resolveThreadActionProjectRef,
116+
startNewThreadFromContext,
117+
startNewThreadOnBranch,
118+
} from "../lib/chatThreadActions";
115119
import {
116120
appendBrowsePathSegment,
117121
ensureBrowseDirectoryPath,
@@ -1903,6 +1907,21 @@ function OpenCommandPaletteDialog(props: {
19031907
});
19041908
}
19051909

1910+
if (activeThread?.branch) {
1911+
const thread = activeThread;
1912+
actionItems.push({
1913+
kind: "action",
1914+
value: "action:new-thread-on-branch",
1915+
searchTerms: ["new thread on", thread.branch ?? "", "branch", "worktree", "checkout"],
1916+
title: `New thread on ${thread.branch}`,
1917+
icon: <SquarePenIcon className={ITEM_ICON_CLASS} />,
1918+
shortcutCommand: "chat.newOnBranch",
1919+
run: async () => {
1920+
await startNewThreadOnBranch(thread, handleNewThread);
1921+
},
1922+
});
1923+
}
1924+
19061925
if (scratchTargetEnvironmentId !== null) {
19071926
actionItems.push({
19081927
kind: "action",

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { startNewThreadOnBranch } from "../lib/chatThreadActions";
12
import { useSupportsMultiplePullRequests } from "~/hooks/useSupportsMultiplePullRequests";
23
import { resolveThreadCurrentPullRequestLink } from "@t3tools/shared/threadPullRequests";
34
import { Spinner } from "~/components/ui/spinner";
@@ -2275,16 +2276,7 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
22752276
}
22762277

22772278
if (clicked === "new-thread-on-branch") {
2278-
// Explicit branch carry-over: reuse the thread's worktree when it
2279-
// has one, otherwise its branch on the local checkout.
2280-
const result = await settlePromise(() =>
2281-
handleNewThread(scopeProjectRef(thread.environmentId, thread.projectId), {
2282-
branch: thread.branch,
2283-
worktreePath: thread.worktreePath,
2284-
envMode: thread.worktreePath ? "worktree" : "local",
2285-
startFromOrigin: false,
2286-
}),
2287-
);
2279+
const result = await settlePromise(() => startNewThreadOnBranch(thread, handleNewThread));
22882280
if (result._tag === "Failure") {
22892281
const error = squashAtomCommandFailure(result);
22902282
toastManager.add(

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { startNewThreadOnBranch } from "../lib/chatThreadActions";
12
import { ThreadHoverCard, ThreadHoverCardPopup } from "./ThreadHoverCard";
23
import { CollapsibleSectionHeader } from "./ui/collapsible-section-header";
34
import { setThreadChangeRequestSnapshot } from "./ThreadStatusIndicators";
@@ -4359,15 +4360,8 @@ export default function Sidebar() {
43594360
if (threadProjectGroup) openProjectSettings(threadProjectGroup);
43604361
return;
43614362
case "new-thread-on-branch": {
4362-
// Explicit branch carry-over: reuse the thread's worktree when it
4363-
// has one, otherwise its branch on the local checkout.
43644363
const result = await settlePromise(() =>
4365-
handleNewThreadRef.current(scopeProjectRef(thread.environmentId, thread.projectId), {
4366-
branch: thread.branch,
4367-
worktreePath: thread.worktreePath,
4368-
envMode: thread.worktreePath ? "worktree" : "local",
4369-
startFromOrigin: false,
4370-
}),
4364+
startNewThreadOnBranch(thread, handleNewThreadRef.current),
43714365
);
43724366
if (result._tag === "Failure") {
43734367
const error = squashAtomCommandFailure(result);

‎apps/web/src/hooks/useHandleNewThread.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,33 @@ describe.each([
266266
},
267267
);
268268

269+
it.each([null, "/remote/project-feature"])(
270+
"preserves explicit branch and checkout %s over workspace defaults",
271+
async (worktreePath) => {
272+
testState.reset(draft, { envMode: "worktree", startFromOrigin: true });
273+
const projectRef = { environmentId: "environment-ssh", projectId: "project-remote" } as never;
274+
const options = {
275+
branch: "feature",
276+
worktreePath,
277+
envMode: worktreePath ? ("worktree" as const) : ("local" as const),
278+
startFromOrigin: false,
279+
};
280+
const opened = await useNewThreadHandler()(projectRef, options);
281+
expect(testState.draftStore.setLogicalProjectDraftThreadId).toHaveBeenCalledWith(
282+
"remote-project",
283+
projectRef,
284+
opened!.draftId,
285+
expect.objectContaining(options),
286+
);
287+
if (draft) {
288+
expect(testState.draftStore.setDraftThreadContext).toHaveBeenCalledWith(
289+
draft.draftId,
290+
expect.objectContaining(options),
291+
);
292+
}
293+
},
294+
);
295+
269296
it.each([true, false])(
270297
"preserves an explicit start-from-origin choice of %s",
271298
async (startFromOrigin) => {

‎apps/web/src/hooks/useThreadActionMenu.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
1+
import { startNewThreadOnBranch } from "../lib/chatThreadActions";
22
import { requestCustomSnooze } from "../components/CustomSnoozeDialog";
33
import {
44
type AtomCommandResult,
@@ -196,15 +196,8 @@ export function useThreadActionMenu(input: {
196196
return;
197197
}
198198
case "new-thread-on-branch": {
199-
// Explicit branch carry-over: reuse the thread's worktree when it
200-
// has one, otherwise its branch on the local checkout.
201199
const result = await settlePromise(() =>
202-
handleNewThread(scopeProjectRef(threadRef.environmentId, thread.projectId), {
203-
branch: thread.branch,
204-
worktreePath: thread.worktreePath,
205-
envMode: thread.worktreePath ? "worktree" : "local",
206-
startFromOrigin: false,
207-
}),
200+
startNewThreadOnBranch(thread, handleNewThread),
208201
);
209202
if (result._tag === "Failure") {
210203
failureToast("Could not create thread", squashAtomCommandFailure(result));

‎apps/web/src/lib/chatThreadActions.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
resolveNewDraftStartFromOrigin,
1313
resolveNewThreadModelSelectionOverride,
1414
startNewThreadFromContext,
15+
startNewThreadOnBranch,
1516
type ChatThreadActionContext,
1617
} from "./chatThreadActions";
1718

@@ -169,3 +170,46 @@ describe("chatThreadActions", () => {
169170
expect(handleNewThread).not.toHaveBeenCalled();
170171
});
171172
});
173+
174+
describe("startNewThreadOnBranch", () => {
175+
it.each([null, "/remote/project-feature"])(
176+
"reuses the branch with checkout %s",
177+
async (worktreePath) => {
178+
const handleNewThread = vi.fn(async () => ({ draftId: "draft" }));
179+
const thread = {
180+
environmentId: ENVIRONMENT_ID,
181+
projectId: PROJECT_ID,
182+
branch: "feature",
183+
worktreePath,
184+
};
185+
expect(await startNewThreadOnBranch(thread, handleNewThread)).toBe(true);
186+
expect(handleNewThread).toHaveBeenCalledWith(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), {
187+
branch: "feature",
188+
worktreePath,
189+
envMode: worktreePath ? "worktree" : "local",
190+
startFromOrigin: false,
191+
});
192+
},
193+
);
194+
it.each([
195+
null,
196+
{ environmentId: ENVIRONMENT_ID, projectId: PROJECT_ID, branch: null, worktreePath: null },
197+
])("does not create a draft without a saved branch", async (thread) => {
198+
const handleNewThread = vi.fn(async () => ({}));
199+
expect(await startNewThreadOnBranch(thread, handleNewThread)).toBe(false);
200+
expect(handleNewThread).not.toHaveBeenCalled();
201+
});
202+
it("reports when creation does not open a draft", async () => {
203+
expect(
204+
await startNewThreadOnBranch(
205+
{
206+
environmentId: ENVIRONMENT_ID,
207+
projectId: PROJECT_ID,
208+
branch: "feature",
209+
worktreePath: null,
210+
},
211+
async () => null,
212+
),
213+
).toBe(false);
214+
});
215+
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,18 @@ export async function startNewThreadFromContext(
100100
await context.handleNewThread(projectRef);
101101
return true;
102102
}
103+
104+
/** Opens a blank draft on a saved thread's branch, reusing its checkout. */
105+
export async function startNewThreadOnBranch(
106+
thread: (ThreadContextLike & { branch: string | null; worktreePath: string | null }) | null,
107+
handleNewThread: NewThreadHandler,
108+
): Promise<boolean> {
109+
if (!thread?.branch) return false;
110+
const opened = await handleNewThread(scopeProjectRef(thread.environmentId, thread.projectId), {
111+
branch: thread.branch,
112+
worktreePath: thread.worktreePath,
113+
envMode: thread.worktreePath ? "worktree" : "local",
114+
startFromOrigin: false,
115+
});
116+
return opened != null;
117+
}

‎apps/web/src/routes/_chat.tsx‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
12
import { Outlet, createFileRoute, redirect, useParams } from "@tanstack/react-router";
23
import { useAtomValue } from "@effect/atom-react";
34
import { useEffect, useMemo } from "react";
@@ -14,7 +15,7 @@ import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping";
1415
import { dispatchPreviewAction } from "../components/preview/previewActionBus";
1516
import { useHandleNewThread } from "../hooks/useHandleNewThread";
1617
import { useScratchProject } from "../hooks/useScratchProject";
17-
import { startNewThreadFromContext } from "../lib/chatThreadActions";
18+
import { startNewThreadFromContext, startNewThreadOnBranch } from "../lib/chatThreadActions";
1819
import { isPreviewFocused } from "../lib/previewFocus";
1920
import { isTerminalFocused } from "../lib/terminalFocus";
2021
import { isEditableFocused } from "../lib/editableFocus";
@@ -95,6 +96,26 @@ function ChatRouteGlobalShortcuts() {
9596
return;
9697
}
9798

99+
if (command === "chat.newOnBranch") {
100+
event.preventDefault();
101+
event.stopPropagation();
102+
if (event.repeat) return;
103+
void settlePromise(() => startNewThreadOnBranch(activeThread, handleNewThread)).then(
104+
(result) => {
105+
if (result._tag !== "Failure") return;
106+
const error = squashAtomCommandFailure(result);
107+
toastManager.add(
108+
stackedThreadToast({
109+
type: "error",
110+
title: "Could not create thread",
111+
description: error instanceof Error ? error.message : "An error occurred.",
112+
}),
113+
);
114+
},
115+
);
116+
return;
117+
}
118+
98119
if (command === "chat.newLocal") {
99120
event.preventDefault();
100121
event.stopPropagation();

‎docs/user/keybindings.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ through the pages you have visited, like a browser's back and forward buttons.
133133
[new-thread defaults](./thread-sidebar.md#start-a-thread). `chat.newWithoutProject`
134134
(`mod+alt+n`) starts a thread [without a project](./thread-sidebar.md#start-without-a-project).
135135

136+
Assign `chat.newOnBranch` in Settings → Keybindings → Add binding to start a blank
137+
thread on the active thread's branch and reuse its worktree. It has no default shortcut.
138+
136139
## Reserved shortcuts
137140

138141
In the desktop app, `mod+w` closes the focused terminal or the active right-panel

‎packages/contracts/src/keybindings.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ it.effect("parses keybinding rules", () =>
100100
command: "chat.newLocal",
101101
});
102102
assert.strictEqual(parsedLocal.command, "chat.newLocal");
103+
const parsedOnBranch = yield* decode(KeybindingRule, {
104+
key: "mod+alt+b",
105+
command: "chat.newOnBranch",
106+
when: "!terminalFocus",
107+
});
108+
assert.strictEqual(parsedOnBranch.command, "chat.newOnBranch");
103109

104110
const parsedModelPickerToggle = yield* decode(KeybindingRule, {
105111
key: "mod+shift+m",

0 commit comments

Comments
 (0)