Skip to content

Commit 300d3ce

Browse files
refactor(web): chat shortcuts live with the hooks that own their actions
Terminal, right panel, thread lifecycle and composer chords move next to the code they drive; useChatShortcuts reads its input through effect events instead of a layout-effect ref. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4d5d85a commit 300d3ce

6 files changed

Lines changed: 221 additions & 192 deletions

File tree

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

Lines changed: 14 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ import type { UsageLimitSourceSnapshots } from "@t3tools/contracts";
4747
import { feedbackBannerItem } from "./chat/ComposerFeedback";
4848
import { useUsageLimitsPanel } from "./chat/useUsageLimitsPanel";
4949
import { useWorktreeSetup } from "./chat/useWorktreeSetup";
50-
import { useChatShortcuts, type ChatShortcutHandler } from "./chat/useChatShortcuts";
50+
import { useChatShortcuts } from "./chat/useChatShortcuts";
5151
import * as Schema from "effect/Schema";
5252
import {
5353
AuthOrchestrationOperateScope,
5454
AuthSourceControlWriteScope,
5555
AuthPreviewOperateScope,
56-
AuthTerminalReadScope,
5756
AuthTerminalOperateScope,
5857
type AssistantCitation,
5958
type ChatFileAttachment,
@@ -71,7 +70,6 @@ import {
7170
resolveIdleCompaction,
7271
RuntimeMode,
7372
} from "@t3tools/contracts";
74-
import { useThreadActions } from "../hooks/useThreadActions";
7573
import {
7674
deriveProviderSubagentStatus,
7775
deriveReportedModelSelection,
@@ -138,7 +136,6 @@ import { useWorkspaceMutationRefresh } from "../hooks/useWorkspaceMutationRefres
138136
import { DEFAULT_INTERACTION_MODE, type ChatMessage } from "../types";
139137
import { useTheme } from "../hooks/useTheme";
140138
import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
141-
import { settleAndAdvance } from "../settleAndAdvance";
142139
import { subscribeSnapShotComposerFocus } from "../lib/desktopSnapShot";
143140
import { useMediaQuery } from "../hooks/useMediaQuery";
144141
import { FileMetadataThreadProvider } from "../hooks/FileMetadataThreadProvider";
@@ -411,10 +408,8 @@ export default function ChatView(props: ChatViewProps) {
411408
} = props;
412409
const canOperateThread = useEnvironmentScope(environmentId, AuthOrchestrationOperateScope);
413410
const canOperateTerminal = useEnvironmentScope(environmentId, AuthTerminalOperateScope);
414-
const canReadTerminal = useEnvironmentScope(environmentId, AuthTerminalReadScope);
415411
const draftId = routeKind === "draft" ? props.draftId : null;
416412
const handleNewThread = useNewThreadHandler();
417-
const { settleThread, pinThread, confirmAndUnpinThread } = useThreadActions();
418413
const routeThreadRef = scopeThreadRef(environmentId, threadId);
419414
const routeThreadKey = scopedThreadKey(routeThreadRef);
420415
const currentRouteThreadKeyRef = useRef<string | null>(routeThreadKey);
@@ -1847,17 +1842,13 @@ export default function ChatView(props: ChatViewProps) {
18471842
addTerminalContextToDraftStable,
18481843
addTerminalSurfaceStable,
18491844
closePanelTerminalStable,
1850-
createNewTerminalStable,
18511845
lastInvokedScriptByProjectId,
1852-
requestClosePanelTerminalStable,
1853-
requestCloseTerminalStable,
18541846
runProjectScriptStable,
18551847
runShellCommandStable,
1856-
setTerminalOpenStable,
18571848
splitPanelTerminalStable,
18581849
splitPanelTerminalVerticalStable,
1859-
splitTerminalStable,
18601850
toggleTerminalVisibilityStable,
1851+
shortcuts: terminalShortcuts,
18611852
} = useChatTerminalActions({
18621853
environmentId,
18631854
composerRef,
@@ -2382,7 +2373,6 @@ export default function ChatView(props: ChatViewProps) {
23822373
},
23832374
);
23842375
};
2385-
const copyActiveThreadReferenceStable = useStableCallback(copyActiveThreadReference);
23862376
const pullRequestPanelTarget = activeThread
23872377
? threadPullRequestPanelTarget({
23882378
projectId: activeThread.projectId,
@@ -2404,12 +2394,12 @@ export default function ChatView(props: ChatViewProps) {
24042394
createBrowserSurfaceStable,
24052395
moveRightPanelSurfaceStable,
24062396
onOpenTurnDiffStable,
2407-
onToggleDiffStable,
24082397
openChangesFromThreadPanelStable,
24092398
openFileSurfaceStable,
24102399
toggleRightPanelMaximizedStable,
24112400
toggleRightPanelStable,
24122401
toggleThreadPanelStable,
2402+
shortcuts: rightPanelShortcuts,
24132403
} = useRightPanelActions({
24142404
activeThreadRef,
24152405
activeProject,
@@ -2434,29 +2424,26 @@ export default function ChatView(props: ChatViewProps) {
24342424
explicitDiffOpenRef,
24352425
setTerminalFocusRequestId,
24362426
onDiffPanelOpen,
2427+
closeRightPanelSurface,
24372428
});
24382429
const pullRequestSurfaceAvailable = supportsPullRequests && pullRequestPanelTarget !== null;
24392430
const nowMinute = useNowMinute();
24402431
const {
24412432
acknowledgeActiveThreadWoke,
24422433
activeBranchMismatchKey,
2443-
activeThreadPinned,
2444-
activeThreadSettled,
24452434
backgroundWorkBanner,
24462435
childInputBanner,
24472436
goalBanner,
24482437
handleSwitchCheckoutToThread,
24492438
handleSwitchCheckoutToThreadStable,
2450-
handleUnsettleActiveThreadStable,
24512439
isRestoringThreadBranch,
24522440
onOpenRelatedThread,
24532441
onOpenRelatedThreadStable,
24542442
sendStandaloneCommand,
24552443
setBranchMismatchDismissTick,
24562444
showBranchMismatchBanner,
2457-
supportsPinning,
2458-
supportsSettlement,
24592445
threadStatusLine,
2446+
shortcuts: threadLifecycleShortcuts,
24602447
} = useThreadLifecycle({
24612448
environmentId,
24622449
routeThreadKey,
@@ -2705,120 +2692,31 @@ export default function ChatView(props: ChatViewProps) {
27052692
const threadFindControlsRef = useRef<ThreadFindControls | null>(null);
27062693
const [isThreadFindActive, setIsThreadFindActive] = useState(false);
27072694
const openThreadFind = () => threadFindControlsRef.current?.open();
2708-
const openThreadFindStable = useStableCallback(openThreadFind);
27092695
const closeThreadFind = () => threadFindControlsRef.current?.close();
2710-
const closeThreadFindStable = useStableCallback(closeThreadFind);
27112696

2712-
const toastPinFailure = (title: string) => (result: AtomCommandResult<unknown, unknown>) => {
2713-
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
2714-
const error = squashAtomCommandFailure(result);
2715-
toastManager.add(
2716-
stackedThreadToast({
2717-
type: "error",
2718-
title,
2719-
description: error instanceof Error ? error.message : "An error occurred.",
2720-
}),
2721-
);
2722-
};
2723-
const openTerminalIfClosed = () => {
2724-
if (!terminalUiState.terminalOpen) setTerminalOpenStable(true);
2725-
};
2726-
const openComposerControl = (command: Parameters<ChatComposerHandle["openControl"]>[0]) =>
2727-
(({ event }) => {
2728-
if (!event.repeat) composerRef.current?.openControl(command);
2729-
}) satisfies ChatShortcutHandler;
27302697
useChatShortcuts({
27312698
enabled: activeThreadId !== null,
27322699
keybindings,
27332700
scriptKeybindings,
2734-
getShortcutContext: getShortcutContextStable,
2701+
getShortcutContext,
27352702
threadFindActive: isThreadFindActive,
2736-
typeIntoComposer: (key) => composerRef.current?.insertTextAtEnd(key) === true,
2703+
composerRef,
2704+
branchToolbarRef,
27372705
closeThreadFind: () => {
2738-
closeThreadFindStable();
2706+
closeThreadFind();
27392707
focusComposerStable();
27402708
},
27412709
commands: {
2710+
...terminalShortcuts,
2711+
...rightPanelShortcuts,
2712+
...threadLifecycleShortcuts,
27422713
"thread.copyReference": ({ event }) => {
2743-
if (!event.repeat) copyActiveThreadReferenceStable();
2744-
},
2745-
"thread.settle": () => {
2746-
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
2747-
if (!isServerThread || !activeThreadRef || !supportsSettlement) return;
2748-
if (activeThreadSettled) {
2749-
void handleUnsettleActiveThreadStable();
2750-
return;
2751-
}
2752-
if (settleAndAdvance(activeThreadRef)) return;
2753-
void settleThread(activeThreadRef).then(toastPinFailure("Failed to settle thread"));
2754-
},
2755-
"thread.pin": () => {
2756-
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
2757-
if (!isServerThread || !activeThreadRef || !supportsPinning) return;
2758-
void (
2759-
activeThreadPinned ? confirmAndUnpinThread(activeThreadRef) : pinThread(activeThreadRef)
2760-
).then(
2761-
toastPinFailure(activeThreadPinned ? "Failed to unpin thread" : "Failed to pin thread"),
2762-
);
2763-
},
2764-
"terminal.toggle": () => {
2765-
if (!terminalUiState.terminalOpen && !canReadTerminal && !canOperateTerminal) return false;
2766-
toggleTerminalVisibilityStable();
2714+
if (!event.repeat) copyActiveThreadReference();
27672715
},
27682716
"chat.find": () => {
27692717
if (!isServerThread || serverConfig?.threadFind !== true) return false;
2770-
openThreadFindStable();
2771-
},
2772-
"rightPanel.toggle": () => toggleRightPanelStable(),
2773-
"rightPanel.toggleMaximized": ({ event }) => {
2774-
if (!event.repeat) toggleRightPanelMaximizedStable();
2718+
openThreadFind();
27752719
},
2776-
"threadPanel.toggle": () => toggleThreadPanelStable(),
2777-
"rightPanel.close": ({ event }) => {
2778-
if (!activeRightPanelSurface) return false;
2779-
if (!event.repeat) closeRightPanelSurfaceStable(activeRightPanelSurface);
2780-
},
2781-
"terminal.split": ({ terminalFocusOwner }) => {
2782-
if (!canOperateTerminal) return false;
2783-
if (terminalFocusOwner === "right-panel") return void splitPanelTerminalStable();
2784-
openTerminalIfClosed();
2785-
splitTerminalStable();
2786-
},
2787-
"terminal.splitVertical": ({ terminalFocusOwner }) => {
2788-
if (!canOperateTerminal) return false;
2789-
if (terminalFocusOwner === "right-panel") return void splitPanelTerminalStable("vertical");
2790-
openTerminalIfClosed();
2791-
splitTerminalStable("vertical");
2792-
},
2793-
"terminal.close": ({ terminalFocusOwner }) => {
2794-
if (!canOperateTerminal) return false;
2795-
if (terminalFocusOwner === "right-panel" && activeRightPanelSurface?.kind === "terminal") {
2796-
requestClosePanelTerminalStable(activeRightPanelSurface.activeTerminalId);
2797-
return;
2798-
}
2799-
if (terminalUiState.terminalOpen)
2800-
requestCloseTerminalStable(terminalUiState.activeTerminalId);
2801-
},
2802-
"terminal.new": ({ terminalFocusOwner }) => {
2803-
if (!canOperateTerminal) return false;
2804-
if (terminalFocusOwner === "right-panel") return void addTerminalSurfaceStable();
2805-
openTerminalIfClosed();
2806-
createNewTerminalStable();
2807-
},
2808-
"diff.toggle": () => onToggleDiffStable(),
2809-
"modelPicker.toggle": ({ event }) => {
2810-
if (!event.repeat) composerRef.current?.toggleModelPicker();
2811-
},
2812-
"composer.increaseEffort": ({ event }) => {
2813-
if (!event.repeat) composerRef.current?.stepReasoningEffort(1);
2814-
},
2815-
"composer.decreaseEffort": ({ event }) => {
2816-
if (!event.repeat) composerRef.current?.stepReasoningEffort(-1);
2817-
},
2818-
"composer.host": openComposerControl("composer.host"),
2819-
"composer.effort": openComposerControl("composer.effort"),
2820-
"composer.mode": openComposerControl("composer.mode"),
2821-
"composer.workspace": openComposerControl("composer.workspace"),
28222720
"composer.cycleHost": ({ event }) => {
28232721
if (!canChangeEnvironment) return false;
28242722
if (event.repeat) return;
@@ -2829,12 +2727,6 @@ export default function ChatView(props: ChatViewProps) {
28292727
const next = logicalProjectEnvironments[(index + 1) % logicalProjectEnvironments.length];
28302728
if (next) onEnvironmentChangeStable(next.environmentId);
28312729
},
2832-
"composer.branch": ({ event }) => {
2833-
if (!event.repeat) branchToolbarRef.current?.openBranchPicker();
2834-
},
2835-
"composer.previousWorktree": ({ event }) => {
2836-
if (!event.repeat) branchToolbarRef.current?.usePreviousWorktree();
2837-
},
28382730
"thread.steerQueuedMessage": ({ event }) =>
28392731
routeKind !== "draft" && queuedRunsControlRef.current?.steerNext(event.repeat) === true,
28402732
"thread.editQueuedMessage": ({ event }) =>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { ComposerHandleRef } from "../../composerHandleContext";
2+
import type { BranchToolbarHandle } from "../BranchToolbar";
3+
import type { ChatComposerHandle } from "./ChatComposer";
4+
import type { ChatShortcutCommands } from "./useChatShortcuts";
5+
6+
const openControl =
7+
(composerRef: ComposerHandleRef, control: Parameters<ChatComposerHandle["openControl"]>[0]) =>
8+
({ event }: { readonly event: KeyboardEvent }) => {
9+
if (!event.repeat) composerRef.current?.openControl(control);
10+
};
11+
12+
/** Chords that open or step the composer's controls and its branch toolbar. Held keys do not repeat them. */
13+
export function composerShortcutCommands(
14+
composerRef: ComposerHandleRef,
15+
branchToolbarRef: { readonly current: BranchToolbarHandle | null },
16+
): ChatShortcutCommands {
17+
return {
18+
"modelPicker.toggle": ({ event }) => {
19+
if (!event.repeat) composerRef.current?.toggleModelPicker();
20+
},
21+
"composer.increaseEffort": ({ event }) => {
22+
if (!event.repeat) composerRef.current?.stepReasoningEffort(1);
23+
},
24+
"composer.decreaseEffort": ({ event }) => {
25+
if (!event.repeat) composerRef.current?.stepReasoningEffort(-1);
26+
},
27+
"composer.host": openControl(composerRef, "composer.host"),
28+
"composer.effort": openControl(composerRef, "composer.effort"),
29+
"composer.mode": openControl(composerRef, "composer.mode"),
30+
"composer.workspace": openControl(composerRef, "composer.workspace"),
31+
"composer.branch": ({ event }) => {
32+
if (!event.repeat) branchToolbarRef.current?.openBranchPicker();
33+
},
34+
"composer.previousWorktree": ({ event }) => {
35+
if (!event.repeat) branchToolbarRef.current?.usePreviousWorktree();
36+
},
37+
} satisfies ChatShortcutCommands;
38+
}

0 commit comments

Comments
 (0)