@devries-cameron20 / t3code-1 / commits / d915777397
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): keybindings settings compile without manual memoization (#20598)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 22:03 UTC d9157773977d2f91992b5fba5a9d2c59ab3e5887
1 file changed +115 -110
apps/web/src/components/settings/KeybindingsSettings.tsx
+115 -110
@@ -16,8 +16,6 @@ import {
16 type KeyboardEvent,
17 type ReactNode,
18 type RefObject,
19 - useCallback,
20 - useMemo,
19 useReducer,
20 useRef,
21 useState,
@@ -566,7 +564,7 @@ function WhenExpressionBuilder({
564 }) {
565 const expression = whenAstToExpression(value);
566 const [expressionDraft, setExpressionDraft] = useState(expression);
569 - const parseResult = useMemo(() => parseWhenExpressionDraft(expressionDraft), [expressionDraft]);
567 + const parseResult = parseWhenExpressionDraft(expressionDraft);
568 const parseError = parseResult.ok ? null : parseResult.message;
569 const unknownIdentifiers = parseResult.ok ? unknownWhenVariables(parseResult.value) : [];
570
@@ -1262,6 +1260,36 @@ function BrowserKeybindingNotice() {
1260 );
1261 }
1262
1263 +// The panel calls hooks after these, so they would lose their caches inline. Separate hooks
1264 +// keep the command options from rebuilding on every search keystroke.
1265 +function useKeybindings(
1266 + serverKeybindings: Parameters<typeof mergeWithDefaultKeybindings>[0] | undefined,
1267 +) {
1268 + "use memo";
1269 + const keybindings = mergeWithDefaultKeybindings(serverKeybindings ?? []);
1270 + return { keybindings, commandOptions: buildKeybindingCommandOptions(keybindings) };
1271 +}
1272 +
1273 +function useKeybindingRows(
1274 + keybindings: ReturnType<typeof mergeWithDefaultKeybindings>,
1275 + query: string,
1276 +) {
1277 + "use memo";
1278 + const rows = buildKeybindingRows(keybindings, query);
1279 + return { rows, groups: groupKeybindingRows(rows), anchorIds: keybindingAnchorIds(rows) };
1280 +}
1281 +
1282 +function keybindingAnchorIds(rows: ReturnType<typeof buildKeybindingRows>) {
1283 + const ids = new Map<string, string>();
1284 + const seen = new Set<KeybindingCommand>();
1285 + for (const row of rows) {
1286 + if (seen.has(row.command)) continue;
1287 + seen.add(row.command);
1288 + ids.set(row.id, keybindingSearchAnchorId(row.command));
1289 + }
1290 + return ids;
1291 +}
1292 +
1293 export function KeybindingsSettingsPanel() {
1294 const { environment: primaryEnvironment, connectedEnvironments } = useSettingsScope();
1295 const canOpenKeybindingsFile = useEnvironmentScope(
@@ -1272,10 +1300,8 @@ export function KeybindingsSettingsPanel() {
1300 const canWriteSettings =
1301 connectedEnvironments.length > 0 &&
1302 connectedEnvironments.every((target) => writableIds.has(target.environmentId));
1275 - const serverKeybindings = primaryEnvironment?.serverConfig?.keybindings;
1276 - const keybindings = useMemo(
1277 - () => mergeWithDefaultKeybindings(serverKeybindings ?? []),
1278 - [serverKeybindings],
1303 + const { keybindings, commandOptions } = useKeybindings(
1304 + primaryEnvironment?.serverConfig?.keybindings,
1305 );
1306 const keybindingsConfigPath = primaryEnvironment?.serverConfig?.keybindingsConfigPath ?? null;
1307 const availableEditors = primaryEnvironment?.serverConfig?.availableEditors ?? [];
@@ -1293,18 +1319,7 @@ export function KeybindingsSettingsPanel() {
1319 const searchInputRef = useRef<HTMLInputElement>(null);
1320 const [savingCommand, setSavingCommand] = useState<KeybindingCommand | null>(null);
1321 const [isAddingBinding, setIsAddingBinding] = useState(false);
1296 - const rows = useMemo(() => buildKeybindingRows(keybindings, query), [keybindings, query]);
1297 - const groups = useMemo(() => groupKeybindingRows(rows), [rows]);
1298 - const anchorIds = useMemo(() => {
1299 - const ids = new Map<string, string>();
1300 - const seen = new Set<KeybindingCommand>();
1301 - for (const row of rows) {
1302 - if (seen.has(row.command)) continue;
1303 - seen.add(row.command);
1304 - ids.set(row.id, keybindingSearchAnchorId(row.command));
1305 - }
1306 - return ids;
1307 - }, [rows]);
1322 + const { rows, groups, anchorIds } = useKeybindingRows(keybindings, query);
1323 const searchTargetId = useLocation({ select: (location) => location.hash.replace(/^#/, "") });
1324 const [handledSearchTargetId, setHandledSearchTargetId] = useState(searchTargetId);
1325
@@ -1312,10 +1327,9 @@ export function KeybindingsSettingsPanel() {
1327 setHandledSearchTargetId(searchTargetId);
1328 if (searchTargetId.startsWith("keybinding-")) setQuery("");
1329 }
1315 - const commandOptions = useMemo(() => buildKeybindingCommandOptions(keybindings), [keybindings]);
1316 - const whenVariables = useMemo(() => buildWhenVariableOptions(), []);
1330 + const whenVariables = buildWhenVariableOptions();
1331
1318 - const openKeybindingsFile = useCallback(() => {
1332 + const openKeybindingsFile = () => {
1333 if (
1334 !keybindingsConfigPath ||
1335 !primaryEnvironment ||
@@ -1335,101 +1349,92 @@ export function KeybindingsSettingsPanel() {
1349 type: "error",
1350 });
1351 })();
1338 - }, [keybindingsConfigPath, openInPreferredEditor, primaryEnvironment]);
1339 -
1340 - const saveKeybinding = useCallback(
1341 - (input: ServerUpsertKeybindingInput) => {
1342 - if (
1343 - !primaryEnvironment ||
1344 - !connectedEnvironments.every((target) =>
1345 - readEnvironmentScope(target.environmentId, AuthSettingsWriteScope),
1346 - )
1352 + };
1353 +
1354 + const saveKeybinding = (input: ServerUpsertKeybindingInput) => {
1355 + if (
1356 + !primaryEnvironment ||
1357 + !connectedEnvironments.every((target) =>
1358 + readEnvironmentScope(target.environmentId, AuthSettingsWriteScope),
1359 )
1360 + )
1361 + return;
1362 + setSavingCommand(input.command);
1363 + const payload: ServerUpsertKeybindingInput = {
1364 + command: input.command,
1365 + key: input.key.trim(),
1366 + ...(input.when?.trim() ? { when: input.when.trim() } : {}),
1367 + ...(input.replace ? { replace: input.replace } : {}),
1368 + };
1369 + void (async () => {
1370 + const results = await Promise.all(
1371 + connectedEnvironments.map((target) =>
1372 + upsertKeybinding({ environmentId: target.environmentId, input: payload }),
1373 + ),
1374 + );
1375 + setSavingCommand(null);
1376 + const failed = results.find((result) => result._tag === "Failure");
1377 + if (!failed) {
1378 + setIsAddingBinding(false);
1379 return;
1349 - setSavingCommand(input.command);
1350 - const payload: ServerUpsertKeybindingInput = {
1351 - command: input.command,
1352 - key: input.key.trim(),
1353 - ...(input.when?.trim() ? { when: input.when.trim() } : {}),
1354 - ...(input.replace ? { replace: input.replace } : {}),
1355 - };
1356 - void (async () => {
1357 - const results = await Promise.all(
1358 - connectedEnvironments.map((target) =>
1359 - upsertKeybinding({ environmentId: target.environmentId, input: payload }),
1360 - ),
1361 - );
1362 - setSavingCommand(null);
1363 - const failed = results.find((result) => result._tag === "Failure");
1364 - if (!failed) {
1365 - setIsAddingBinding(false);
1366 - return;
1367 - }
1368 - if (!isAtomCommandInterrupted(failed)) {
1369 - const error = squashAtomCommandFailure(failed);
1370 - toastManager.add({
1371 - title: "Unable to save keybinding",
1372 - description: error instanceof Error ? error.message : "The keybinding was not saved.",
1373 - type: "error",
1374 - });
1375 - }
1376 - })();
1377 - },
1378 - [connectedEnvironments, primaryEnvironment, upsertKeybinding],
1379 - );
1380 + }
1381 + if (!isAtomCommandInterrupted(failed)) {
1382 + const error = squashAtomCommandFailure(failed);
1383 + toastManager.add({
1384 + title: "Unable to save keybinding",
1385 + description: error instanceof Error ? error.message : "The keybinding was not saved.",
1386 + type: "error",
1387 + });
1388 + }
1389 + })();
1390 + };
1391
1381 - const removeKeybinding = useCallback(
1382 - (row: KeybindingRow) => {
1383 - if (
1384 - !primaryEnvironment ||
1385 - !connectedEnvironments.every((target) =>
1386 - readEnvironmentScope(target.environmentId, AuthSettingsWriteScope),
1387 - )
1392 + const removeKeybinding = (row: KeybindingRow) => {
1393 + if (
1394 + !primaryEnvironment ||
1395 + !connectedEnvironments.every((target) =>
1396 + readEnvironmentScope(target.environmentId, AuthSettingsWriteScope),
1397 )
1389 - return;
1390 - setSavingCommand(row.command);
1391 - void (async () => {
1392 - const results = await Promise.all(
1393 - connectedEnvironments.map((target) =>
1394 - removeKeybindingMutation({
1395 - environmentId: target.environmentId,
1396 - input: rowKeybindingTarget(row),
1397 - }),
1398 - ),
1399 - );
1400 - setSavingCommand(null);
1401 - const result = results.find((entry) => entry._tag === "Failure") ?? results[0];
1402 - if (result?._tag === "Failure" && !isAtomCommandInterrupted(result)) {
1403 - const error = squashAtomCommandFailure(result);
1404 - toastManager.add({
1405 - title: "Unable to remove keybinding",
1406 - description: error instanceof Error ? error.message : "The keybinding was not removed.",
1407 - type: "error",
1408 - });
1409 - }
1410 - })();
1411 - },
1412 - [connectedEnvironments, primaryEnvironment, removeKeybindingMutation],
1413 - );
1398 + )
1399 + return;
1400 + setSavingCommand(row.command);
1401 + void (async () => {
1402 + const results = await Promise.all(
1403 + connectedEnvironments.map((target) =>
1404 + removeKeybindingMutation({
1405 + environmentId: target.environmentId,
1406 + input: rowKeybindingTarget(row),
1407 + }),
1408 + ),
1409 + );
1410 + setSavingCommand(null);
1411 + const result = results.find((entry) => entry._tag === "Failure") ?? results[0];
1412 + if (result?._tag === "Failure" && !isAtomCommandInterrupted(result)) {
1413 + const error = squashAtomCommandFailure(result);
1414 + toastManager.add({
1415 + title: "Unable to remove keybinding",
1416 + description: error instanceof Error ? error.message : "The keybinding was not removed.",
1417 + type: "error",
1418 + });
1419 + }
1420 + })();
1421 + };
1422
1415 - const resetKeybinding = useCallback(
1416 - (row: KeybindingRow) => {
1417 - if (!row.defaultKey) return;
1418 - saveKeybinding({
1423 + const resetKeybinding = (row: KeybindingRow) => {
1424 + if (!row.defaultKey) return;
1425 + saveKeybinding({
1426 + command: row.command,
1427 + key: row.defaultKey,
1428 + when: row.defaultWhen.trim().length > 0 ? row.defaultWhen : undefined,
1429 + replace: {
1430 command: row.command,
1420 - key: row.defaultKey,
1421 - when: row.defaultWhen.trim().length > 0 ? row.defaultWhen : undefined,
1422 - replace: {
1423 - command: row.command,
1424 - key: row.key,
1425 - ...(row.when.trim().length > 0 ? { when: row.when } : {}),
1426 - },
1427 - });
1428 - },
1429 - [saveKeybinding],
1430 - );
1431 + key: row.key,
1432 + ...(row.when.trim().length > 0 ? { when: row.when } : {}),
1433 + },
1434 + });
1435 + };
1436
1432 - const cancelAdd = useCallback(() => setIsAddingBinding(false), []);
1437 + const cancelAdd = () => setIsAddingBinding(false);
1438
1439 const rowActions: KeybindingRowActions = {
1440 allRows: rows,