Skip to content

Commit b1b630a

Browse files
perf(web): searching keybindings only renders rows entering or leaving the filter
The keybinding rows were rebuilt on every search keystroke and every row got the filtered list for conflict checks, so all visible rows re-rendered. Rows are now built once per server keybindings in their own hook and filtering keeps the same row objects. Conflict checks use the full list, which is also what conflicts actually mean. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent efdaa4b commit b1b630a

3 files changed

Lines changed: 114 additions & 92 deletions

File tree

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

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
buildKeybindingCommandOptions,
88
buildWhenVariableOptions,
99
commandLabel,
10+
filterKeybindingRows,
1011
groupKeybindingRows,
1112
keybindingConflictLabels,
1213
keybindingFromKeyboardEvent,
@@ -19,7 +20,7 @@ import {
1920

2021
describe("KeybindingsSettings.logic", () => {
2122
it("lists composer, provider, and pull request commands with editable defaults", () => {
22-
const rows = buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS, "");
23+
const rows = buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS);
2324
for (const command of [
2425
"composer.sendAlternate",
2526
"composer.sendBackground",
@@ -43,24 +44,34 @@ describe("KeybindingsSettings.logic", () => {
4344
}
4445
});
4546
it("finds the editable shortcut for sending the first queued message", () => {
46-
expect(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS, "first queued")).toContainEqual(
47+
expect(
48+
filterKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS), "first queued"),
49+
).toContainEqual(
4750
expect.objectContaining({
4851
command: "thread.steerQueuedMessage",
4952
key: "mod+shift+enter",
5053
}),
5154
);
5255
});
56+
it("filtering keeps the row objects so unchanged rows skip rendering", () => {
57+
const rows = buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS);
58+
const filtered = filterKeybindingRows(rows, "terminal");
59+
expect(filtered.length).toBeGreaterThan(0);
60+
expect(filtered.length).toBeLessThan(rows.length);
61+
for (const row of filtered) expect(rows).toContain(row);
62+
expect(filterKeybindingRows(rows, " ")).toBe(rows);
63+
});
5364
it.each(["pu", "pull request", "copy link", "thread id"])(
5465
"finds the copy link shortcut with %s",
5566
(query) => {
56-
const rows = buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS, query);
67+
const rows = filterKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS), query);
5768
expect(rows).toContainEqual(
5869
expect.objectContaining({ command: "thread.copyReference", key: "mod+shift+c" }),
5970
);
6071
},
6172
);
6273
it("groups rows by command area in page order and drops empty groups", () => {
63-
const groups = groupKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS, ""));
74+
const groups = groupKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS));
6475
expect(groups.map((group) => group.title)).toEqual([
6576
"Navigation",
6677
"Threads",
@@ -74,9 +85,11 @@ describe("KeybindingsSettings.logic", () => {
7485
expect(composer?.rows.map((row) => row.command)).toEqual(
7586
expect.arrayContaining(["composer.host", "modelPicker.toggle"]),
7687
);
77-
expect(groupKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS, "split"))).toEqual(
78-
[expect.objectContaining({ id: "terminal" })],
79-
);
88+
expect(
89+
groupKeybindingRows(
90+
filterKeybindingRows(buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS), "split"),
91+
),
92+
).toEqual([expect.objectContaining({ id: "terminal" })]);
8093
});
8194
it("orders Usage bindings and command choices like the page", () => {
8295
const expected = [
@@ -93,16 +106,18 @@ describe("KeybindingsSettings.logic", () => {
93106
DEFAULT_RESOLVED_KEYBINDINGS,
94107
DEFAULT_RESOLVED_KEYBINDINGS.toReversed(),
95108
]) {
96-
expect(buildKeybindingRows(bindings, "usage").map((row) => row.command)).toEqual(expected);
109+
expect(
110+
filterKeybindingRows(buildKeybindingRows(bindings), "usage").map((row) => row.command),
111+
).toEqual(expected);
97112
expect(
98113
buildKeybindingCommandOptions(bindings).filter((command) => command.startsWith("usage.")),
99114
).toEqual(expected);
100115
}
101116
});
102117

103118
it("builds searchable rows with readable key and when values", () => {
104-
const rows = buildKeybindingRows(
105-
[
119+
const rows = filterKeybindingRows(
120+
buildKeybindingRows([
106121
{
107122
command: "terminal.toggle",
108123
shortcut: {
@@ -118,7 +133,7 @@ describe("KeybindingsSettings.logic", () => {
118133
node: { type: "identifier", name: "terminalFocus" },
119134
},
120135
},
121-
] satisfies ResolvedKeybindingsConfig,
136+
] satisfies ResolvedKeybindingsConfig),
122137
"terminal",
123138
);
124139

@@ -345,81 +360,75 @@ describe("KeybindingsSettings.logic", () => {
345360
});
346361

347362
it("marks each default shortcut for multi-binding commands as default", () => {
348-
const rows = buildKeybindingRows(
349-
[
350-
{
351-
command: "chat.new",
352-
shortcut: {
353-
key: "n",
354-
modKey: true,
355-
metaKey: false,
356-
ctrlKey: false,
357-
altKey: false,
358-
shiftKey: false,
359-
},
360-
whenAst: {
361-
type: "not",
362-
node: { type: "identifier", name: "terminalFocus" },
363-
},
363+
const rows = buildKeybindingRows([
364+
{
365+
command: "chat.new",
366+
shortcut: {
367+
key: "n",
368+
modKey: true,
369+
metaKey: false,
370+
ctrlKey: false,
371+
altKey: false,
372+
shiftKey: false,
364373
},
365-
{
366-
command: "chat.new",
367-
shortcut: {
368-
key: "o",
369-
modKey: true,
370-
metaKey: false,
371-
ctrlKey: false,
372-
altKey: false,
373-
shiftKey: true,
374-
},
375-
whenAst: {
376-
type: "not",
377-
node: { type: "identifier", name: "terminalFocus" },
378-
},
374+
whenAst: {
375+
type: "not",
376+
node: { type: "identifier", name: "terminalFocus" },
379377
},
380-
] satisfies ResolvedKeybindingsConfig,
381-
"",
382-
);
378+
},
379+
{
380+
command: "chat.new",
381+
shortcut: {
382+
key: "o",
383+
modKey: true,
384+
metaKey: false,
385+
ctrlKey: false,
386+
altKey: false,
387+
shiftKey: true,
388+
},
389+
whenAst: {
390+
type: "not",
391+
node: { type: "identifier", name: "terminalFocus" },
392+
},
393+
},
394+
] satisfies ResolvedKeybindingsConfig);
383395

384396
expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
385397
});
386398

387399
it("reports conflicting shortcuts that share an active when context", () => {
388-
const rows = buildKeybindingRows(
389-
[
390-
{
391-
command: "chat.new",
392-
shortcut: {
393-
key: "n",
394-
modKey: true,
395-
metaKey: false,
396-
ctrlKey: false,
397-
altKey: false,
398-
shiftKey: false,
399-
},
400-
whenAst: {
401-
type: "not",
402-
node: { type: "identifier", name: "terminalFocus" },
403-
},
400+
const rows = buildKeybindingRows([
401+
{
402+
command: "chat.new",
403+
shortcut: {
404+
key: "n",
405+
modKey: true,
406+
metaKey: false,
407+
ctrlKey: false,
408+
altKey: false,
409+
shiftKey: false,
404410
},
405-
{
406-
command: "chat.newLocal",
407-
shortcut: {
408-
key: "n",
409-
modKey: true,
410-
metaKey: false,
411-
ctrlKey: false,
412-
altKey: false,
413-
shiftKey: false,
414-
},
415-
whenAst: {
416-
type: "not",
417-
node: { type: "identifier", name: "terminalFocus" },
418-
},
411+
whenAst: {
412+
type: "not",
413+
node: { type: "identifier", name: "terminalFocus" },
419414
},
420-
] satisfies ResolvedKeybindingsConfig,
421-
"",
422-
);
415+
},
416+
{
417+
command: "chat.newLocal",
418+
shortcut: {
419+
key: "n",
420+
modKey: true,
421+
metaKey: false,
422+
ctrlKey: false,
423+
altKey: false,
424+
shiftKey: false,
425+
},
426+
whenAst: {
427+
type: "not",
428+
node: { type: "identifier", name: "terminalFocus" },
429+
},
430+
},
431+
] satisfies ResolvedKeybindingsConfig);
423432

424433
expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
425434
expect(

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,7 @@ export function groupKeybindingRows(
243243

244244
export function buildKeybindingRows(
245245
keybindings: ResolvedKeybindingsConfig,
246-
query: string,
247246
): ReadonlyArray<KeybindingRow> {
248-
const normalizedQuery = query.trim().toLowerCase();
249247
const rows = keybindings.map((binding, index) => {
250248
const defaultBinding = defaultBindingForBinding(binding);
251249
const key = shortcutToKeybindingInput(binding.shortcut);
@@ -280,11 +278,18 @@ export function buildKeybindingRows(
280278
return left.key.localeCompare(right.key);
281279
});
282280

283-
if (normalizedQuery.length === 0) {
284-
return rowsWithConflicts;
285-
}
281+
return rowsWithConflicts;
282+
}
286283

287-
return rowsWithConflicts.filter((row) => {
284+
// Filtering keeps the row objects so rows that stay visible while the query
285+
// changes receive the same props and skip rendering.
286+
export function filterKeybindingRows(
287+
rows: ReadonlyArray<KeybindingRow>,
288+
query: string,
289+
): ReadonlyArray<KeybindingRow> {
290+
const normalizedQuery = query.trim().toLowerCase();
291+
if (normalizedQuery.length === 0) return rows;
292+
return rows.filter((row) => {
288293
return (
289294
row.command.toLowerCase().includes(normalizedQuery) ||
290295
commandLabel(row.command).toLowerCase().includes(normalizedQuery) ||

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { Toggle } from "../ui/toggle";
5252
import { toastManager } from "../ui/toast";
5353
import {
5454
buildKeybindingRows,
55+
filterKeybindingRows,
5556
buildKeybindingCommandOptions,
5657
buildWhenVariableOptions,
5758
commandLabel,
@@ -1271,6 +1272,18 @@ function keybindingAnchorIds(rows: ReturnType<typeof buildKeybindingRows>) {
12711272
return ids;
12721273
}
12731274

1275+
// Rows come from a hook so they are cached on the server keybindings alone. Built
1276+
// inline, the compiler joins them to the query's cache scope (filtering might
1277+
// mutate them), and every keystroke rebuilds and re-renders every row.
1278+
function useKeybindingCatalog() {
1279+
const serverKeybindings = useSettingsScope().environment?.serverConfig?.keybindings;
1280+
const merged = mergeWithDefaultKeybindings(serverKeybindings ?? []);
1281+
return {
1282+
commandOptions: buildKeybindingCommandOptions(merged),
1283+
allRows: buildKeybindingRows(merged),
1284+
};
1285+
}
1286+
12741287
export function KeybindingsSettingsPanel() {
12751288
const { environment: primaryEnvironment, connectedEnvironments } = useSettingsScope();
12761289
const canOpenKeybindingsFile = useEnvironmentScope(
@@ -1305,13 +1318,8 @@ export function KeybindingsSettingsPanel() {
13051318
if (searchTargetId.startsWith("keybinding-")) setQuery("");
13061319
}
13071320
const whenVariables = buildWhenVariableOptions();
1308-
const serverKeybindings = primaryEnvironment?.serverConfig?.keybindings;
1309-
// Each derivation merges the defaults itself so the command options key on the
1310-
// server keybindings alone and do not rebuild on every search keystroke.
1311-
const commandOptions = buildKeybindingCommandOptions(
1312-
mergeWithDefaultKeybindings(serverKeybindings ?? []),
1313-
);
1314-
const rows = buildKeybindingRows(mergeWithDefaultKeybindings(serverKeybindings ?? []), query);
1321+
const { commandOptions, allRows } = useKeybindingCatalog();
1322+
const rows = filterKeybindingRows(allRows, query);
13151323
const groups = groupKeybindingRows(rows);
13161324
const anchorIds = keybindingAnchorIds(rows);
13171325

@@ -1423,7 +1431,7 @@ export function KeybindingsSettingsPanel() {
14231431
const cancelAdd = () => setIsAddingBinding(false);
14241432

14251433
const rowActions: KeybindingRowActions = {
1426-
allRows: rows,
1434+
allRows,
14271435
variables: whenVariables,
14281436
onSave: saveKeybinding,
14291437
onReset: resetKeybinding,
@@ -1479,7 +1487,7 @@ export function KeybindingsSettingsPanel() {
14791487
<SettingsGroup>
14801488
<NewKeybindingSettingsRow
14811489
commandOptions={commandOptions}
1482-
allRows={rows}
1490+
allRows={allRows}
14831491
variables={whenVariables}
14841492
isSaving={savingCommand !== null}
14851493
onSave={saveKeybinding}

0 commit comments

Comments
 (0)