Skip to content

Commit 9130d93

Browse files
authored
feat(web): add composer and PR number shortcuts (#11615)
1 parent 33118d9 commit 9130d93

24 files changed

Lines changed: 694 additions & 49 deletions

‎apps/server/src/keybindings.ts‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { writeFileStringAtomically } from "./atomicWrite.ts";
4545
import { fromJsonStringPretty, fromLenientJson } from "@t3tools/shared/schemaJson";
4646
import {
4747
DEFAULT_KEYBINDINGS,
48-
DEFAULT_RESOLVED_KEYBINDINGS,
48+
mergeWithDefaultKeybindings,
4949
compileResolvedKeybindingRule,
5050
compileResolvedKeybindingsConfig,
5151
parseKeybindingShortcut,
@@ -201,25 +201,6 @@ function invalidEntryIssue(index: number, detail: string): ServerConfigIssue {
201201
};
202202
}
203203

204-
function mergeWithDefaultKeybindings(custom: ResolvedKeybindingsConfig): ResolvedKeybindingsConfig {
205-
if (custom.length === 0) {
206-
return [...DEFAULT_RESOLVED_KEYBINDINGS];
207-
}
208-
209-
const overriddenCommands = new Set(custom.map((binding) => binding.command));
210-
const retainedDefaults = DEFAULT_RESOLVED_KEYBINDINGS.filter(
211-
(binding) => !overriddenCommands.has(binding.command),
212-
);
213-
const merged = [...retainedDefaults, ...custom];
214-
215-
if (merged.length <= MAX_KEYBINDINGS_COUNT) {
216-
return merged;
217-
}
218-
219-
// Keep the latest rules when the config exceeds max size; later rules have higher precedence.
220-
return merged.slice(-MAX_KEYBINDINGS_COUNT);
221-
}
222-
223204
/**
224205
* Keybindings - Service tag for keybinding configuration operations.
225206
*/

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ import {
88
HistoryIcon,
99
ScaleIcon,
1010
} from "lucide-react";
11-
import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
11+
import {
12+
type Ref,
13+
memo,
14+
useImperativeHandle,
15+
useCallback,
16+
useEffect,
17+
useLayoutEffect,
18+
useMemo,
19+
useRef,
20+
useState,
21+
} from "react";
1222

1323
import { useComposerDraftStore, type DraftId } from "../composerDraftStore";
1424
import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
@@ -25,7 +35,10 @@ import {
2535
resolvePreviousWorktreeSeed,
2636
shouldShowEnvironmentIndicator,
2737
} from "./BranchToolbar.logic";
28-
import { BranchToolbarBranchSelector } from "./BranchToolbarBranchSelector";
38+
import {
39+
BranchToolbarBranchSelector,
40+
type BranchToolbarBranchSelectorHandle,
41+
} from "./BranchToolbarBranchSelector";
2942
import { BranchToolbarEnvironmentSelector } from "./BranchToolbarEnvironmentSelector";
3043
import { BranchToolbarEnvModeSelector } from "./BranchToolbarEnvModeSelector";
3144
import { Button } from "./ui/button";
@@ -46,7 +59,13 @@ import { measureRestingComposerControls } from "./chat/restingComposerControlsMe
4659
import { resolveRestingComposerControlsNaturalWidth } from "./composerFooterLayout";
4760
import { cn } from "~/lib/utils";
4861

62+
export interface BranchToolbarHandle {
63+
openBranchPicker: () => void;
64+
usePreviousWorktree: () => void;
65+
}
66+
4967
interface BranchToolbarProps {
68+
ref?: Ref<BranchToolbarHandle>;
5069
environmentId: EnvironmentId;
5170
threadId: ThreadId;
5271
showGitControls: boolean;
@@ -169,6 +188,10 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
169188
render={<Button variant="ghost" size="xs" />}
170189
className="min-w-0 max-w-[48%] flex-initial justify-start font-normal text-muted-foreground/70 text-xs! hover:text-foreground/80"
171190
data-composer-context-control
191+
data-composer-shortcut={[
192+
showEnvironmentPicker && !envLocked ? "composer.host" : "",
193+
!envModeLocked ? "composer.workspace" : "",
194+
].join(" ")}
172195
>
173196
{triggerContent}
174197
<ChevronDownIcon className="size-3 shrink-0 opacity-50" />
@@ -438,6 +461,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
438461
}
439462

440463
export const BranchToolbar = memo(function BranchToolbar({
464+
ref,
441465
environmentId,
442466
threadId,
443467
showGitControls,
@@ -458,6 +482,7 @@ export const BranchToolbar = memo(function BranchToolbar({
458482
composerControlsHostRef,
459483
contextStripVisible = true,
460484
}: BranchToolbarProps) {
485+
const branchSelectorRef = useRef<BranchToolbarBranchSelectorHandle>(null);
461486
const threadRef = useMemo(
462487
() => scopeThreadRef(environmentId, threadId),
463488
[environmentId, threadId],
@@ -518,6 +543,25 @@ export const BranchToolbar = memo(function BranchToolbar({
518543
});
519544
}, [activeProjectRef, draftId, previousWorktreeSeed, setDraftThreadContext, threadRef]);
520545

546+
useImperativeHandle(
547+
ref,
548+
() => ({
549+
openBranchPicker: () => branchSelectorRef.current?.open(),
550+
usePreviousWorktree: () => {
551+
if (!showGitControls || !canUsePreviousWorktree || !previousWorktreeSeed) return;
552+
onUsePreviousWorktree();
553+
onComposerFocusRequest?.();
554+
},
555+
}),
556+
[
557+
canUsePreviousWorktree,
558+
onComposerFocusRequest,
559+
onUsePreviousWorktree,
560+
previousWorktreeSeed,
561+
showGitControls,
562+
],
563+
);
564+
521565
const showEnvironmentPicker = Boolean(
522566
availableEnvironments && availableEnvironments.length > 1 && onEnvironmentChange,
523567
);
@@ -618,6 +662,7 @@ export const BranchToolbar = memo(function BranchToolbar({
618662

619663
{showGitControls ? (
620664
<BranchToolbarBranchSelector
665+
ref={branchSelectorRef}
621666
className="min-w-0 flex-initial justify-end @3xl/composer-surface:ml-auto"
622667
environmentId={environmentId}
623668
threadId={threadId}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import {
1515
useDeferredValue,
1616
useEffect,
1717
useId,
18+
useImperativeHandle,
1819
useLayoutEffect,
1920
useMemo,
2021
useOptimistic,
2122
useRef,
2223
useState,
2324
useTransition,
2425
type MouseEvent as ReactMouseEvent,
26+
type Ref,
2527
} from "react";
2628

2729
import { useComposerDraftStore, type DraftId } from "../composerDraftStore";
@@ -72,7 +74,12 @@ import {
7274
import { stackedThreadToast, toastManager } from "./ui/toast";
7375
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
7476

77+
export interface BranchToolbarBranchSelectorHandle {
78+
open: () => void;
79+
}
80+
7581
interface BranchToolbarBranchSelectorProps {
82+
ref?: Ref<BranchToolbarBranchSelectorHandle>;
7683
className?: string;
7784
environmentId: EnvironmentId;
7885
threadId: ThreadId;
@@ -92,6 +99,7 @@ function toBranchActionErrorMessage(error: unknown): string {
9299
}
93100

94101
export function BranchToolbarBranchSelector({
102+
ref,
95103
className,
96104
environmentId,
97105
threadId,
@@ -535,6 +543,17 @@ export function BranchToolbarBranchSelector({
535543
}
536544
}, []);
537545

546+
useImperativeHandle(
547+
ref,
548+
() => ({
549+
open: () => {
550+
if (isInitialBranchesLoadPending || isBranchActionPending) return;
551+
handleOpenChange(true);
552+
},
553+
}),
554+
[handleOpenChange, isBranchActionPending, isInitialBranchesLoadPending],
555+
);
556+
538557
const [showTopBranchScrollFade, setShowTopBranchScrollFade] = useState(false);
539558
const [showBottomBranchScrollFade, setShowBottomBranchScrollFade] = useState(false);
540559
const fetchNextBranchPage = useCallback(() => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const BranchToolbarEnvModeSelector = memo(function BranchToolbarEnvModeSe
9393
size="xs"
9494
className="min-w-0 shrink font-normal text-xs!"
9595
aria-label="Workspace"
96+
data-composer-shortcut="composer.workspace"
9697
data-composer-context-control
9798
>
9899
{effectiveEnvMode === "worktree" ? (

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvir
9595
size="xs"
9696
className="min-w-0 max-w-full font-normal text-xs!"
9797
aria-label="Run on"
98+
data-composer-shortcut="composer.host"
9899
data-composer-context-control
99100
>
100101
{autoEnvironmentLabel ? (

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ import {
219219
deriveAgentPanelModel,
220220
foldSubagentActivities,
221221
} from "@t3tools/client-runtime/state/subagentRuntime";
222-
import { BranchToolbar } from "./BranchToolbar";
222+
import { BranchToolbar, type BranchToolbarHandle } from "./BranchToolbar";
223223
import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings";
224224
import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
225225
import {
@@ -1617,6 +1617,7 @@ export default function ChatView(props: ChatViewProps) {
16171617
const composerTerminalContextsRef = useRef<TerminalContextDraft[]>([]);
16181618
const localComposerRef = useRef<ChatComposerHandle | null>(null);
16191619
const composerRef = useComposerHandleContext() ?? localComposerRef;
1620+
const branchToolbarRef = useRef<BranchToolbarHandle>(null);
16201621
const pasteAsTextShortcutUntilRef = useRef(0);
16211622
const [restingComposerControlsHost, setRestingComposerControlsHost] =
16221623
useState<HTMLDivElement | null>(null);
@@ -6397,6 +6398,17 @@ export default function ChatView(props: ChatViewProps) {
63976398
terminalUiOpenByThreadRef.current[activeThreadKey] = current;
63986399
}, [activeThreadKey, focusComposer, terminalUiState.terminalOpen]);
63996400

6401+
const getShortcutContext = useCallback(
6402+
() => ({
6403+
terminalFocus: getTerminalFocusOwner() !== null,
6404+
terminalOpen: Boolean(terminalUiState.terminalOpen),
6405+
previewFocus: isPreviewFocused(),
6406+
previewOpen: previewPanelOpen,
6407+
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,
6408+
}),
6409+
[composerRef, previewPanelOpen, terminalUiState.terminalOpen],
6410+
);
6411+
64006412
useEffect(() => {
64016413
const handler = (event: globalThis.KeyboardEvent) => {
64026414
if (preventRepeatedTerminalCloseShortcut(event, keybindings)) {
@@ -6417,13 +6429,7 @@ export default function ChatView(props: ChatViewProps) {
64176429
if (event.defaultPrevented && terminalFocusOwner === null) {
64186430
return;
64196431
}
6420-
const shortcutContext = {
6421-
terminalFocus: terminalFocusOwner !== null,
6422-
terminalOpen: Boolean(terminalUiState.terminalOpen),
6423-
previewFocus: isPreviewFocused(),
6424-
previewOpen: previewPanelOpen,
6425-
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,
6426-
};
6432+
const shortcutContext = getShortcutContext();
64276433

64286434
if (
64296435
!shortcutContext.terminalFocus &&
@@ -6588,7 +6594,33 @@ export default function ChatView(props: ChatViewProps) {
65886594
if (command === "modelPicker.toggle") {
65896595
event.preventDefault();
65906596
event.stopPropagation();
6591-
composerRef.current?.toggleModelPicker();
6597+
if (!event.repeat) composerRef.current?.toggleModelPicker();
6598+
return;
6599+
}
6600+
6601+
if (
6602+
command === "composer.host" ||
6603+
command === "composer.effort" ||
6604+
command === "composer.mode" ||
6605+
command === "composer.workspace"
6606+
) {
6607+
event.preventDefault();
6608+
event.stopPropagation();
6609+
if (!event.repeat) composerRef.current?.openControl(command);
6610+
return;
6611+
}
6612+
6613+
if (command === "composer.branch") {
6614+
event.preventDefault();
6615+
event.stopPropagation();
6616+
if (!event.repeat) branchToolbarRef.current?.openBranchPicker();
6617+
return;
6618+
}
6619+
6620+
if (command === "composer.previousWorktree") {
6621+
event.preventDefault();
6622+
event.stopPropagation();
6623+
if (!event.repeat) branchToolbarRef.current?.usePreviousWorktree();
65926624
return;
65936625
}
65946626

@@ -6643,7 +6675,7 @@ export default function ChatView(props: ChatViewProps) {
66436675
supportsSettlement,
66446676
confirmAndUnpinThread,
66456677
copyActiveThreadReference,
6646-
previewPanelOpen,
6678+
getShortcutContext,
66476679
toggleRightPanel,
66486680
toggleRightPanelMaximized,
66496681
toggleTerminalVisibility,
@@ -8708,6 +8740,10 @@ export default function ChatView(props: ChatViewProps) {
87088740
// reader's feet. A link the agent wrote can open any other one here, and that one has to be
87098741
// checkable out like it is anywhere else.
87108742
<PullRequestDetailPanel
8743+
getShortcutContext={getShortcutContext}
8744+
shortcutsEnabled={
8745+
rightPanelOpen && activeRightPanelSurface?.id === renderedRightPanelSurface.id
8746+
}
87118747
key={`${renderedRightPanelSurface.host ?? ""}:${renderedRightPanelSurface.repository}#${renderedRightPanelSurface.number}`}
87128748
environmentId={activeThread.environmentId}
87138749
onSelectPullRequest={(reference) => {
@@ -9231,6 +9267,7 @@ export default function ChatView(props: ChatViewProps) {
92319267
{mountComposerContextStrip && (
92329268
<div className="pointer-events-auto">
92339269
<BranchToolbar
9270+
ref={branchToolbarRef}
92349271
environmentId={activeThread.environmentId}
92359272
threadId={activeThread.id}
92369273
showGitControls={isGitRepo}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "../../questionAttachments";
1616
import type {
1717
ApprovalRequestId,
18+
KeybindingCommand,
1819
AssistantCitation,
1920
ChatFileAttachment,
2021
EnvironmentId,
@@ -62,7 +63,7 @@ import {
6263
useState,
6364
useSyncExternalStore,
6465
} from "react";
65-
import { createPortal } from "react-dom";
66+
import { createPortal, flushSync } from "react-dom";
6667
import {
6768
clampCollapsedComposerCursor,
6869
type ComposerSubmissionIntent,
@@ -1103,6 +1104,7 @@ const ComposerFooterModeControls = memo(function ComposerFooterModeControls(prop
11031104
<TooltipTrigger
11041105
render={
11051106
<ComposerSelectControl
1107+
data-composer-shortcut="composer.mode"
11061108
size={size}
11071109
className={size === "xs" ? undefined : "font-medium"}
11081110
aria-label="Runtime mode"
@@ -1231,6 +1233,7 @@ export interface ChatComposerHandle {
12311233
) => boolean;
12321234
openModelPicker: () => void;
12331235
toggleModelPicker: () => void;
1236+
openControl: (command: KeybindingCommand) => void;
12341237
isModelPickerOpen: () => boolean;
12351238
compactContext: () => void;
12361239
readSnapshot: () => {
@@ -5743,6 +5746,28 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
57435746
openModelPicker();
57445747
}
57455748
},
5749+
openControl: (command) => {
5750+
if (composerBlurFrameRef.current !== null) {
5751+
window.cancelAnimationFrame(composerBlurFrameRef.current);
5752+
composerBlurFrameRef.current = null;
5753+
}
5754+
flushSync(() => {
5755+
setIsComposerScrollCollapsed(false);
5756+
setIsComposerFocused(true);
5757+
});
5758+
const shell = composerFormRef.current?.closest('[data-slot="composer-shell"]');
5759+
const trigger = Array.from(
5760+
shell?.querySelectorAll<HTMLButtonElement>(
5761+
`button[data-composer-shortcut~="${command}"]:not(:disabled)`,
5762+
) ?? [],
5763+
).find(
5764+
(element) =>
5765+
!element.closest("[inert]") && element.checkVisibility({ visibilityProperty: true }),
5766+
);
5767+
if (!trigger) return;
5768+
trigger.focus({ preventScroll: true });
5769+
trigger.click();
5770+
},
57465771
compactContext: compactThreadContext,
57475772
isModelPickerOpen: () => isComposerModelPickerOpen,
57485773
readSnapshot: () => {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export const CompactComposerControlsMenu = memo(function CompactComposerControls
4040
variant="ghost"
4141
className={size === "xs" ? "shrink-0" : "shrink-0 px-2"}
4242
aria-label="More composer controls"
43+
data-composer-shortcut={
44+
props.traitsMenuContent ? "composer.mode composer.effort" : "composer.mode"
45+
}
4346
/>
4447
}
4548
>

0 commit comments

Comments
 (0)