Skip to content

Commit 1342d56

Browse files
feat(web): allow shortcuts for archive and delete
1 parent 7f6f47b commit 1342d56

9 files changed

Lines changed: 121 additions & 28 deletions

File tree

‎apps/web/src/components/chat/useThreadLifecycle.tsx‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,14 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
174174
} = input;
175175
const navigate = useNavigate();
176176
const router = useRouter();
177-
const { settleThread, pinThread, confirmAndUnpinThread } = useThreadActions();
177+
const {
178+
settleThread,
179+
pinThread,
180+
confirmAndUnpinThread,
181+
confirmAndArchiveThread,
182+
confirmAndDeleteThread,
183+
} = useThreadActions();
184+
const destructiveShortcutPending = useRef(false);
178185
const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
179186
const switchGitRef = useAtomCommand(vcsEnvironment.switchRef, { reportFailure: false });
180187
const updateThreadMetadata = useOrchestrationCommand(threadEnvironment.updateMetadata, {
@@ -648,6 +655,41 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
648655
);
649656
};
650657
const shortcuts = {
658+
"thread.archive": ({ event }) => {
659+
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
660+
if (!activeThreadShell || !activeThreadRef || !serverConfigLoaded) return;
661+
if (event.repeat || destructiveShortcutPending.current) return;
662+
destructiveShortcutPending.current = true;
663+
let didArchive = false;
664+
void confirmAndArchiveThread(activeThreadRef, {
665+
onArchived: () => {
666+
didArchive = true;
667+
},
668+
})
669+
.then((result) =>
670+
toastThreadActionFailure(
671+
didArchive ? "Thread archived, but navigation failed" : "Failed to archive thread",
672+
)(result),
673+
)
674+
.finally(() => {
675+
destructiveShortcutPending.current = false;
676+
});
677+
},
678+
"thread.delete": ({ event }) => {
679+
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
680+
if (!activeThreadShell || !activeThreadRef || !serverConfigLoaded) return;
681+
if (event.repeat || destructiveShortcutPending.current) return;
682+
destructiveShortcutPending.current = true;
683+
const target = activeThreadRef;
684+
void confirmAndDeleteThread(target)
685+
.then((result) => {
686+
if (readThreadShell(target) !== null)
687+
toastThreadActionFailure("Failed to delete thread")(result);
688+
})
689+
.finally(() => {
690+
destructiveShortcutPending.current = false;
691+
});
692+
},
651693
"thread.settle": () => {
652694
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
653695
if (!activeThreadShell || !activeThreadRef || !serverConfigLoaded) return;

‎apps/web/src/components/settings/KeybindingsSettings.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,14 @@ function useNewKeybindingDraft({
10891089

10901090
return {
10911091
commandDraft,
1092-
setCommandDraft,
1092+
setCommandDraft: (command: KeybindingCommand) => {
1093+
setCommandDraft(command);
1094+
if ((command === "thread.archive" || command === "thread.delete") && !whenDraft) {
1095+
setDraft({
1096+
whenDraft: { type: "not", node: { type: "identifier", name: "terminalFocus" } },
1097+
});
1098+
}
1099+
},
10931100
keyDraft,
10941101
whenDraft,
10951102
whenDraftExpression,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ vi.mock("./useThreadActions", () => ({
130130
"unsnoozeThread",
131131
"pinThread",
132132
"confirmAndUnpinThread",
133-
"archiveThread",
134-
"deleteThread",
133+
"confirmAndArchiveThread",
134+
"confirmAndDeleteThread",
135135
].map((action) => [
136136
action,
137137
async () => {

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,15 @@ export function useThreadActionMenu(input: {
8383
pinThread,
8484
confirmAndUnpinThread,
8585
setThreadAutoSettle,
86-
archiveThread,
87-
deleteThread,
86+
confirmAndArchiveThread,
87+
confirmAndDeleteThread,
8888
markThreadUnread,
8989
turnThreadIntoProject,
9090
} = useThreadActions();
9191
const updateThreadMetadata = useOrchestrationCommand(threadEnvironment.updateMetadata, {
9292
reportFailure: false,
9393
});
9494
const handleNewThread = useNewThreadHandler();
95-
const confirmThreadDelete = useClientSettings((s) => s.confirmThreadDelete);
96-
const confirmThreadArchive = useClientSettings((s) => s.confirmThreadArchive);
9795
const timestampFormat = useClientSettings((s) => s.timestampFormat);
9896
const { copyToClipboard: copyPathToClipboard } = useCopyToClipboard<{ path: string }>({
9997
onCopy: ({ path }) => {
@@ -304,14 +302,8 @@ export function useThreadActionMenu(input: {
304302
copyThreadIdToClipboard(thread.id, { threadId: thread.id });
305303
return;
306304
case "archive": {
307-
if (confirmThreadArchive) {
308-
const confirmed = await settlePromise(() =>
309-
api.dialogs.confirm(`Archive thread "${thread.title}"?`),
310-
);
311-
if (confirmed._tag === "Failure" || !confirmed.value) return;
312-
}
313305
let didArchive = false;
314-
const result = await archiveThread(threadRef, {
306+
const result = await confirmAndArchiveThread(threadRef, {
315307
onArchived: () => {
316308
didArchive = true;
317309
},
@@ -325,19 +317,7 @@ export function useThreadActionMenu(input: {
325317
return;
326318
}
327319
case "delete": {
328-
if (confirmThreadDelete) {
329-
const confirmed = await settlePromise(() =>
330-
api.dialogs.confirm(
331-
[
332-
`Delete thread "${thread.title}"?`,
333-
"This permanently clears conversation history for this thread.",
334-
].join("\n"),
335-
{ variant: "destructive" },
336-
),
337-
);
338-
if (confirmed._tag === "Failure" || !confirmed.value) return;
339-
}
340-
const deleted = await deleteThread(threadRef);
320+
const deleted = await confirmAndDeleteThread(threadRef);
341321
if (
342322
deleted._tag === "Failure" &&
343323
!isAtomCommandInterrupted(deleted) &&

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export function useThreadActions() {
330330
});
331331
const sidebarThreadSortOrder = useClientSettings((settings) => settings.sidebarThreadSortOrder);
332332
const confirmThreadDelete = useClientSettings((settings) => settings.confirmThreadDelete);
333+
const confirmThreadArchive = useClientSettings((settings) => settings.confirmThreadArchive);
333334
const confirmThreadUnpin = useClientSettings((settings) => settings.confirmThreadUnpin);
334335
const clearComposerDraftForThread = useComposerDraftStore((store) => store.clearDraftThread);
335336
const clearProjectDraftThreadById = useComposerDraftStore(
@@ -921,6 +922,25 @@ export function useThreadActions() {
921922
(result) => result._tag === "Success",
922923
);
923924

925+
const confirmAndArchiveThread = async (
926+
target: ScopedThreadRef,
927+
opts: { onArchived?: () => void } = {},
928+
) => {
929+
const permissionFailure = threadOperationFailure(target);
930+
if (permissionFailure) return permissionFailure;
931+
const localApi = readLocalApi();
932+
const resolved = resolveThreadTarget(target);
933+
if (!resolved) return AsyncResult.success(undefined);
934+
if (confirmThreadArchive && localApi) {
935+
const confirmation = await settlePromise(() =>
936+
localApi.dialogs.confirm(`Archive thread "${resolved.thread.title}"?`),
937+
);
938+
if (confirmation._tag === "Failure") return confirmation;
939+
if (!confirmation.value) return AsyncResult.success(undefined);
940+
}
941+
return archiveThread(target, opts);
942+
};
943+
924944
const confirmAndDeleteThread = async (target: ScopedThreadRef) => {
925945
const permissionFailure = threadOperationFailure(target);
926946
if (permissionFailure) return permissionFailure;
@@ -951,6 +971,7 @@ export function useThreadActions() {
951971

952972
return {
953973
archiveThread,
974+
confirmAndArchiveThread,
954975
unarchiveThread,
955976
deleteThread,
956977
confirmAndDeleteThread,

‎apps/web/src/keybindings.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,29 @@ describe("settle thread shortcut", () => {
248248
});
249249
});
250250

251+
describe.each(["thread.archive", "thread.delete"] as const)("%s shortcut", (command) => {
252+
it("is unbound by default and leaves terminal input alone when bound", () => {
253+
assert.isFalse(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === command));
254+
const bindings = compileResolvedKeybindingsConfig([
255+
{ key: "mod+shift+s", command, when: "!terminalFocus" },
256+
]);
257+
const shortcut = event({ key: "s", ctrlKey: true, shiftKey: true });
258+
assert.isNull(
259+
resolveShortcutCommand(shortcut, bindings, {
260+
platform: "Win32",
261+
context: { terminalFocus: true },
262+
}),
263+
);
264+
assert.equal(
265+
resolveShortcutCommand(shortcut, bindings, {
266+
platform: "Win32",
267+
context: { terminalFocus: false },
268+
}),
269+
command,
270+
);
271+
});
272+
});
273+
251274
describe("thread undo shortcut", () => {
252275
it("leaves native undo alone inside text fields and terminals", () => {
253276
assert.isNull(

‎docs/user/keybindings.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ a shortcut.
153153
`thread.stop` interrupts the running turn in the focused thread. It has no default
154154
shortcut; assign one in **Settings → Keybindings**.
155155

156+
`thread.archive` and `thread.delete` act on the current saved thread. Assign a
157+
shortcut in **Settings → Keybindings**; neither has one by default. They respect
158+
the thread confirmation settings, and deletion permanently clears the conversation.
159+
New bindings default to `!terminalFocus`, which you can change in Settings.
160+
156161
`thread.undo` (`mod+z` by default) reverses the actions shown in the notice at the
157162
bottom of the sidebar, such as unpin, settle, snooze, archive, or discarding a
158163
draft. Consecutive

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ it.effect("drops resolved rules with commands this build does not know", () =>
3737
}),
3838
);
3939

40+
it.effect("accepts custom archive and delete thread bindings", () =>
41+
Effect.gen(function* () {
42+
const parsed = yield* decode(ResolvedKeybindingsConfig, [
43+
{ command: "thread.archive", shortcut },
44+
{ command: "thread.delete", shortcut },
45+
]);
46+
assert.deepEqual(
47+
parsed.map((rule) => rule.command),
48+
["thread.archive", "thread.delete"],
49+
);
50+
}),
51+
);
52+
4053
it.effect("drops malformed resolved rule entries", () =>
4154
Effect.gen(function* () {
4255
const parsed = yield* decode(ResolvedKeybindingsConfig, [

‎packages/contracts/src/keybindings.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const THREAD_KEYBINDING_COMMANDS = [
4242
"thread.next",
4343
"thread.copyReference",
4444
"thread.settle",
45+
"thread.archive",
46+
"thread.delete",
4547
"thread.pin",
4648
"thread.undo",
4749
...THREAD_JUMP_KEYBINDING_COMMANDS,

0 commit comments

Comments
 (0)