Skip to content

Commit 0c85ed7

Browse files
committed
fix(web): account for send preferences in shortcut badges
1 parent 87cd9e7 commit 0c85ed7

5 files changed

Lines changed: 61 additions & 19 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,26 @@ it("re-enables Save when a When editor closes on an expression it could not pars
129129
it("filters actions and allows additional shortcuts for assigned commands", async () => {
130130
await using screen = await render(<KeybindingsSettingsPanel />);
131131
await screen.getByRole("button", { name: "Add keybinding" }).click();
132-
const search = screen.getByRole("combobox", { name: "Search actions" });
132+
const search = page.getByRole("combobox", { name: "Search actions" });
133133
await expect.element(search).toHaveFocus();
134134
await userEvent.keyboard("sidebar.toggle");
135-
const assigned = screen.getByRole("option", { name: /Sidebar: Toggle/ });
135+
const assigned = page.getByRole("option", { name: /Sidebar: Toggle/ });
136136
await expect.element(assigned).not.toHaveAttribute("aria-disabled", "true");
137137
await expect.element(assigned.element().querySelector("kbd")!).toBeVisible();
138-
expect(screen.getByRole("option").elements()).toHaveLength(1);
138+
await vi.waitFor(() => expect(page.getByRole("option").elements()).toHaveLength(1));
139139
await assigned.click();
140140
const command = screen.getByRole("combobox", { name: "Command", exact: true });
141141
await expect.element(command).toHaveTextContent("Sidebar: Toggle");
142142
await command.click();
143143
await search.fill("Opposite Queue");
144144
await expect
145-
.element(screen.getByRole("option", { name: /Composer: Opposite Queue or Steer Action/ }))
145+
.element(page.getByRole("option", { name: /Composer: Opposite Queue or Steer Action/ }))
146146
.toBeVisible();
147-
expect(screen.getByRole("option").elements()).toHaveLength(1);
147+
await vi.waitFor(() => expect(page.getByRole("option").elements()).toHaveLength(1));
148148
await search.fill("no-such-action");
149-
await expect.element(screen.getByText("No actions found.")).toBeVisible();
149+
await expect.element(page.getByText("No actions found.")).toBeVisible();
150150
await search.fill("composer.cycleHost");
151-
const available = screen.getByRole("option", { name: "Composer: Cycle Host" });
151+
const available = page.getByRole("option", { name: "Composer: Cycle Host" });
152152
await expect.element(available).not.toHaveAttribute("aria-disabled", "true");
153153
await available.click();
154154
await expect

‎apps/web/src/components/settings/KeybindingsSettings.logic.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ describe("KeybindingsSettings.logic", () => {
7979
bindings[0],
8080
]);
8181
expect(rows.filter((row) => !row.isShadowed)).toHaveLength(7);
82+
const sendBindings = compileResolvedKeybindingsConfig([
83+
{ command: "composer.sendAlternate", key: "mod+enter" },
84+
{ command: "chat.new", key: "mod+enter" },
85+
]);
86+
for (const sendShortcut of ["enter", "mod-enter", "mod-enter-multiline"] as const) {
87+
expect(
88+
buildKeybindingRows(sendBindings, "MacIntel", sendShortcut).find(
89+
(row) => row.command === "composer.sendAlternate",
90+
)?.isShadowed,
91+
).toBe(sendShortcut === "enter");
92+
}
93+
const shiftedBindings = compileResolvedKeybindingsConfig([
94+
{ command: "chat.new", key: "mod+shift+enter" },
95+
{ command: "composer.sendAlternate", key: "mod+enter" },
96+
]);
97+
for (const sendShortcut of ["enter", "mod-enter", "mod-enter-multiline"] as const) {
98+
expect(
99+
buildKeybindingRows(shiftedBindings, "MacIntel", sendShortcut).find(
100+
(row) => row.command === "chat.new",
101+
)?.isShadowed,
102+
).toBe(sendShortcut === "mod-enter");
103+
}
82104
expect(
83105
buildKeybindingRows(bindings, "Win32").find((row) => row.command === "sidebar.toggle")
84106
?.isShadowed,

‎apps/web/src/components/settings/KeybindingsSettings.logic.ts‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
STATIC_KEYBINDING_COMMANDS,
3+
type ClientSettings,
34
type KeybindingCommand,
45
type KeybindingShortcut,
56
type KeybindingWhenNode,
@@ -13,7 +14,7 @@ import {
1314
US_SHIFTED_TO_UNSHIFTED_KEYS,
1415
} from "@t3tools/shared/keybindings";
1516

16-
import { shortcutConflictKey, shortcutKeyFromEvent } from "../../keybindings";
17+
import { shortcutConflictKey, shortcutForContext, shortcutKeyFromEvent } from "../../keybindings";
1718
import { isMacPlatform } from "../../lib/utils";
1819
import { METRIC_OPTIONS, WINDOW_OPTIONS } from "../usage/usageShortcuts";
1920

@@ -250,23 +251,37 @@ export function groupKeybindingRows(
250251
export function buildKeybindingRows(
251252
keybindings: ResolvedKeybindingsConfig,
252253
platform = navigator.platform,
254+
sendShortcut: ClientSettings["sendShortcut"] = "enter",
253255
): ReadonlyArray<KeybindingRow> {
254-
const lastBindingIndex = new Map<string, number>();
255-
for (const [index, binding] of keybindings.entries()) {
256-
const key = shortcutConflictKey(binding.shortcut, platform);
257-
lastBindingIndex.set(`${key}:${whenAstToExpression(binding.whenAst)}`, index);
258-
}
256+
// Multiline sending can use either shortcut; settings have no active composer prompt.
257+
const modifierModes =
258+
sendShortcut === "mod-enter-multiline" ? [false, true] : [sendShortcut === "mod-enter"];
259+
const conflictKeys = modifierModes.map((composerRequiresModifier) =>
260+
keybindings.map((binding) =>
261+
shortcutConflictKey(shortcutForContext(binding, { composerRequiresModifier }), platform),
262+
),
263+
);
264+
const lastBindingIndices = conflictKeys.map((keys) => {
265+
const indices = new Map<string, number>();
266+
for (const [index, binding] of keybindings.entries()) {
267+
indices.set(`${keys[index]}:${whenAstToExpression(binding.whenAst)}`, index);
268+
}
269+
return indices;
270+
});
259271
const rowsById = new Map<string, KeybindingRow>();
260272
for (const [index, binding] of keybindings.entries()) {
261273
const defaultBinding = defaultBindingForBinding(binding);
262274
const key = shortcutToKeybindingInput(binding.shortcut);
263275
const when = whenAstToExpression(binding.whenAst);
264276
const id = keybindingRowId(binding.command, key, when);
265-
const conflictKey = shortcutConflictKey(binding.shortcut, platform);
266277
// Only discard proven shadows; distinct conditions may apply in other focus contexts.
267-
const isShadowed =
268-
(lastBindingIndex.get(`${conflictKey}:`) ?? -1) > index ||
269-
(lastBindingIndex.get(`${conflictKey}:${when}`) ?? -1) > index;
278+
const isShadowed = lastBindingIndices.every((indices, mode) => {
279+
const conflictKey = conflictKeys[mode]?.[index];
280+
return (
281+
(indices.get(`${conflictKey}:`) ?? -1) > index ||
282+
(indices.get(`${conflictKey}:${when}`) ?? -1) > index
283+
);
284+
});
270285
rowsById.set(id, {
271286
id,
272287
command: binding.command,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { formatShortcutLabel } from "../../keybindings";
4141
import { cn } from "../../lib/utils";
4242
import { serverEnvironment } from "../../state/server";
4343
import { useSettingsScope } from "./SettingsScopeContext";
44+
import { useClientSettings } from "../../hooks/useSettings";
4445
import { Badge } from "../ui/badge";
4546
import { Button } from "../ui/button";
4647
import { Input } from "../ui/input";
@@ -1352,11 +1353,12 @@ function keybindingAnchorIds(rows: ReturnType<typeof buildKeybindingRows>) {
13521353

13531354
// Cache rows in a separate hook so the compiler does not rebuild them per search keystroke.
13541355
function useKeybindingCatalog() {
1356+
const sendShortcut = useClientSettings((settings) => settings.sendShortcut);
13551357
const serverKeybindings = useSettingsScope().environment?.serverConfig?.keybindings;
13561358
const merged = mergeWithDefaultKeybindings(serverKeybindings ?? []);
13571359
return {
13581360
commandOptions: buildKeybindingCommandOptions(merged),
1359-
allRows: buildKeybindingRows(merged),
1361+
allRows: buildKeybindingRows(merged, navigator.platform, sendShortcut),
13601362
};
13611363
}
13621364

‎apps/web/src/keybindings.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ export function shortcutConflictKey(
105105
].join("|");
106106
}
107107

108-
function shortcutForContext(binding: ResolvedKeybindingRule, context: ShortcutMatchContext) {
108+
export function shortcutForContext(
109+
binding: ResolvedKeybindingRule,
110+
context: Partial<ShortcutMatchContext>,
111+
) {
109112
const shortcut = binding.shortcut;
110113
// Resolve persisted Mod+Enter against send preference without rewriting saved shortcuts.
111114
if (

0 commit comments

Comments
 (0)