Skip to content

Commit 2a890f0

Browse files
committed
fix(web): honor PR shortcut conditions
1 parent 05f387c commit 2a890f0

4 files changed

Lines changed: 54 additions & 12 deletions

File tree

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6268,6 +6268,17 @@ export default function ChatView(props: ChatViewProps) {
62686268
terminalUiOpenByThreadRef.current[activeThreadKey] = current;
62696269
}, [activeThreadKey, focusComposer, terminalUiState.terminalOpen]);
62706270

6271+
const getShortcutContext = useCallback(
6272+
() => ({
6273+
terminalFocus: getTerminalFocusOwner() !== null,
6274+
terminalOpen: Boolean(terminalUiState.terminalOpen),
6275+
previewFocus: isPreviewFocused(),
6276+
previewOpen: previewPanelOpen,
6277+
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,
6278+
}),
6279+
[composerRef, previewPanelOpen, terminalUiState.terminalOpen],
6280+
);
6281+
62716282
useEffect(() => {
62726283
const handler = (event: globalThis.KeyboardEvent) => {
62736284
if (preventRepeatedTerminalCloseShortcut(event, keybindings)) {
@@ -6288,13 +6299,7 @@ export default function ChatView(props: ChatViewProps) {
62886299
if (event.defaultPrevented && terminalFocusOwner === null) {
62896300
return;
62906301
}
6291-
const shortcutContext = {
6292-
terminalFocus: terminalFocusOwner !== null,
6293-
terminalOpen: Boolean(terminalUiState.terminalOpen),
6294-
previewFocus: isPreviewFocused(),
6295-
previewOpen: previewPanelOpen,
6296-
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,
6297-
};
6302+
const shortcutContext = getShortcutContext();
62986303

62996304
if (
63006305
!shortcutContext.terminalFocus &&
@@ -6540,7 +6545,7 @@ export default function ChatView(props: ChatViewProps) {
65406545
supportsSettlement,
65416546
confirmAndUnpinThread,
65426547
copyActiveThreadReference,
6543-
previewPanelOpen,
6548+
getShortcutContext,
65446549
toggleRightPanel,
65456550
toggleRightPanelMaximized,
65466551
toggleTerminalVisibility,
@@ -8536,6 +8541,7 @@ export default function ChatView(props: ChatViewProps) {
85368541
// reader's feet. A link the agent wrote can open any other one here, and that one has to be
85378542
// checkable out like it is anywhere else.
85388543
<PullRequestDetailPanel
8544+
getShortcutContext={getShortcutContext}
85398545
shortcutsEnabled={
85408546
rightPanelOpen && activeRightPanelSurface?.id === renderedRightPanelSurface.id
85418547
}

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
6161
import { useNewThreadHandler } from "~/hooks/useHandleNewThread";
6262
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
6363
import { isCommandPaletteOpen } from "~/commandPaletteBus";
64-
import { resolveShortcutCommand, shortcutLabelForCommand } from "~/keybindings";
65-
import { isTerminalFocused } from "~/lib/terminalFocus";
64+
import {
65+
resolveShortcutCommand,
66+
shortcutLabelForCommand,
67+
type ShortcutMatchContext,
68+
} from "~/keybindings";
6669
import { primaryServerKeybindingsAtom } from "~/state/server";
6770
import { useClientSettings } from "~/hooks/useSettings";
6871
import {
@@ -465,6 +468,7 @@ function PullRequestBaseFreshnessWarning({
465468
export function PullRequestDetailPanel({
466469
environmentId,
467470
shortcutsEnabled,
471+
getShortcutContext,
468472
threadRef = null,
469473
reference: requestedReference,
470474
listEntry = null,
@@ -478,6 +482,7 @@ export function PullRequestDetailPanel({
478482
}: {
479483
environmentId: EnvironmentId;
480484
shortcutsEnabled: boolean;
485+
getShortcutContext: () => ShortcutMatchContext;
481486
onSelectPullRequest?: ((reference: PullRequestRef) => void) | undefined;
482487
/**
483488
* The thread this panel sits beside, if any. Links that are not the pull
@@ -730,7 +735,7 @@ export function PullRequestDetailPanel({
730735
const copyFromShortcut = useEffectEvent((event: KeyboardEvent) => {
731736
if (!shortcutsEnabled || event.defaultPrevented || isCommandPaletteOpen()) return;
732737
const command = resolveShortcutCommand(event, keybindings, {
733-
context: { terminalFocus: isTerminalFocused() },
738+
context: getShortcutContext(),
734739
});
735740
const value =
736741
command === "pullRequest.copyUrl"

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,26 @@ describe("plus key parsing", () => {
10631063
});
10641064

10651065
describe("composer and pull request shortcuts", () => {
1066+
it.each(["terminalOpen", "previewFocus", "previewOpen", "modelPickerOpen"])(
1067+
"honors custom PR shortcut conditions for %s",
1068+
(condition) => {
1069+
const bindings = compileResolvedKeybindingsConfig([
1070+
{ key: "mod+shift+k", command: "pullRequest.copyUrl", when: condition },
1071+
{ key: "mod+shift+k", command: "pullRequest.copyNumber", when: `!${condition}` },
1072+
]);
1073+
const input = event({ key: "k", ctrlKey: true, shiftKey: true });
1074+
for (const enabled of [false, true]) {
1075+
assert.strictEqual(
1076+
resolveShortcutCommand(input, bindings, {
1077+
platform: "Linux",
1078+
context: { [condition]: enabled },
1079+
}),
1080+
enabled ? "pullRequest.copyUrl" : "pullRequest.copyNumber",
1081+
);
1082+
}
1083+
},
1084+
);
1085+
10661086
const shortcuts = [
10671087
["h", "composer.host"],
10681088
["e", "composer.effort"],

‎apps/web/src/routes/_chat.pull-requests.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ import { cn } from "~/lib/utils";
151151
import { primaryServerKeybindingsAtom } from "~/state/server";
152152
import { getSourceControlPresentationForKind } from "~/sourceControlPresentation";
153153

154+
function getShortcutContext() {
155+
return {
156+
terminalFocus: isTerminalFocused(),
157+
terminalOpen: false,
158+
previewFocus: false,
159+
previewOpen: false,
160+
modelPickerOpen: false,
161+
};
162+
}
163+
154164
export interface PullRequestsSearch extends PullRequestListPreferences {
155165
/**
156166
* Narrows the list to one server. Absent means every connected one, which is the default the
@@ -1925,7 +1935,7 @@ function PullRequestsRouteView() {
19251935
const onKeyDown = (event: KeyboardEvent) => {
19261936
if (event.defaultPrevented || isCommandPaletteOpen()) return;
19271937
const command = resolveShortcutCommand(event, keybindings, {
1928-
context: { terminalFocus: isTerminalFocused() },
1938+
context: getShortcutContext(),
19291939
});
19301940
if (command === "rightPanel.close") closeActiveSurfaceFromShortcut(event);
19311941
if (command === "rightPanel.toggle") toggleRightPanelFromShortcut(event);
@@ -1992,6 +2002,7 @@ function PullRequestsRouteView() {
19922002
pullRequestStatusSeeds={listedPullRequestTabStatuses}
19932003
>
19942004
<PullRequestDetailPanel
2005+
getShortcutContext={getShortcutContext}
19952006
shortcutsEnabled={activePullRequestSurface?.id === renderedPullRequestSurface.id}
19962007
key={renderedPullRequestSurface.id}
19972008
environmentId={panelEnvironmentId}

0 commit comments

Comments
 (0)