Skip to content

Commit edb01da

Browse files
committed
fix(web): claim settle shortcut without active thread
1 parent d8d84f6 commit edb01da

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
3+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
4+
5+
import { claimSettleThreadShortcut } from "./settleThreadShortcut";
6+
7+
const routeThreadRef = {
8+
environmentId: EnvironmentId.make("environment-id"),
9+
threadId: ThreadId.make("thread-id"),
10+
};
11+
12+
function shortcutEvent(repeat = false) {
13+
return {
14+
repeat,
15+
preventDefault: vi.fn(),
16+
stopPropagation: vi.fn(),
17+
};
18+
}
19+
20+
describe("settle thread shortcut", () => {
21+
it("claims the shortcut when there is no route thread", () => {
22+
const event = shortcutEvent();
23+
24+
expect(claimSettleThreadShortcut(event, null)).toBeNull();
25+
expect(event.preventDefault).toHaveBeenCalledOnce();
26+
expect(event.stopPropagation).toHaveBeenCalledOnce();
27+
});
28+
29+
it("returns the route thread for a non-repeated shortcut", () => {
30+
const event = shortcutEvent();
31+
32+
expect(claimSettleThreadShortcut(event, routeThreadRef)).toBe(routeThreadRef);
33+
expect(event.preventDefault).toHaveBeenCalledOnce();
34+
expect(event.stopPropagation).toHaveBeenCalledOnce();
35+
});
36+
37+
it("claims repeated shortcuts without returning the route thread", () => {
38+
const event = shortcutEvent(true);
39+
40+
expect(claimSettleThreadShortcut(event, routeThreadRef)).toBeNull();
41+
expect(event.preventDefault).toHaveBeenCalledOnce();
42+
expect(event.stopPropagation).toHaveBeenCalledOnce();
43+
});
44+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { ScopedThreadRef } from "@t3tools/contracts";
2+
3+
export interface SettleThreadShortcutEvent {
4+
readonly repeat: boolean;
5+
readonly preventDefault: () => void;
6+
readonly stopPropagation: () => void;
7+
}
8+
9+
export function claimSettleThreadShortcut(
10+
event: SettleThreadShortcutEvent,
11+
routeThreadRef: ScopedThreadRef | null,
12+
): ScopedThreadRef | null {
13+
event.preventDefault();
14+
event.stopPropagation();
15+
if (event.repeat) return null;
16+
return routeThreadRef;
17+
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { useHandleNewThread } from "../hooks/useHandleNewThread";
1919
import { useThreadActions } from "../hooks/useThreadActions";
2020
import { startNewThreadFromContext } from "../lib/chatThreadActions";
2121
import { isPreviewFocused } from "../lib/previewFocus";
22+
import { claimSettleThreadShortcut } from "../lib/settleThreadShortcut";
2223
import { isTerminalFocused } from "../lib/terminalFocus";
2324
import { resolveShortcutCommand } from "../keybindings";
2425
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
@@ -117,17 +118,15 @@ function ChatRouteGlobalShortcuts() {
117118
}
118119

119120
if (command === "thread.settle") {
120-
if (routeThreadRef === null) return;
121-
event.preventDefault();
122-
event.stopPropagation();
123-
if (event.repeat) return;
121+
const threadRef = claimSettleThreadShortcut(event, routeThreadRef);
122+
if (threadRef === null) return;
124123

125-
const threadKey = scopedThreadKey(routeThreadRef);
124+
const threadKey = scopedThreadKey(threadRef);
126125
if (settlingThreadKeysRef.current.has(threadKey)) return;
127126
settlingThreadKeysRef.current.add(threadKey);
128127
void (async () => {
129128
try {
130-
const result = await settleThread(routeThreadRef);
129+
const result = await settleThread(threadRef);
131130
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
132131
const error = squashAtomCommandFailure(result);
133132
toastManager.add(

0 commit comments

Comments
 (0)