@@ -16,8 +16,6 @@ import {
1616 type KeyboardEvent ,
1717 type ReactNode ,
1818 type RefObject ,
19- useCallback ,
20- useMemo ,
2119 useReducer ,
2220 useRef ,
2321 useState ,
@@ -566,7 +564,7 @@ function WhenExpressionBuilder({
566564} ) {
567565 const expression = whenAstToExpression ( value ) ;
568566 const [ expressionDraft , setExpressionDraft ] = useState ( expression ) ;
569- const parseResult = useMemo ( ( ) => parseWhenExpressionDraft ( expressionDraft ) , [ expressionDraft ] ) ;
567+ const parseResult = parseWhenExpressionDraft ( expressionDraft ) ;
570568 const parseError = parseResult . ok ? null : parseResult . message ;
571569 const unknownIdentifiers = parseResult . ok ? unknownWhenVariables ( parseResult . value ) : [ ] ;
572570
@@ -1262,6 +1260,36 @@ function BrowserKeybindingNotice() {
12621260 ) ;
12631261}
12641262
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+
12651293export function KeybindingsSettingsPanel ( ) {
12661294 const { environment : primaryEnvironment , connectedEnvironments } = useSettingsScope ( ) ;
12671295 const canOpenKeybindingsFile = useEnvironmentScope (
@@ -1272,10 +1300,8 @@ export function KeybindingsSettingsPanel() {
12721300 const canWriteSettings =
12731301 connectedEnvironments . length > 0 &&
12741302 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 ,
12791305 ) ;
12801306 const keybindingsConfigPath = primaryEnvironment ?. serverConfig ?. keybindingsConfigPath ?? null ;
12811307 const availableEditors = primaryEnvironment ?. serverConfig ?. availableEditors ?? [ ] ;
@@ -1293,29 +1319,17 @@ export function KeybindingsSettingsPanel() {
12931319 const searchInputRef = useRef < HTMLInputElement > ( null ) ;
12941320 const [ savingCommand , setSavingCommand ] = useState < KeybindingCommand | null > ( null ) ;
12951321 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 ) ;
13081323 const searchTargetId = useLocation ( { select : ( location ) => location . hash . replace ( / ^ # / , "" ) } ) ;
13091324 const [ handledSearchTargetId , setHandledSearchTargetId ] = useState ( searchTargetId ) ;
13101325
13111326 if ( searchTargetId !== handledSearchTargetId ) {
13121327 setHandledSearchTargetId ( searchTargetId ) ;
13131328 if ( searchTargetId . startsWith ( "keybinding-" ) ) setQuery ( "" ) ;
13141329 }
1315- const commandOptions = useMemo ( ( ) => buildKeybindingCommandOptions ( keybindings ) , [ keybindings ] ) ;
1316- const whenVariables = useMemo ( ( ) => buildWhenVariableOptions ( ) , [ ] ) ;
1330+ const whenVariables = buildWhenVariableOptions ( ) ;
13171331
1318- const openKeybindingsFile = useCallback ( ( ) => {
1332+ const openKeybindingsFile = ( ) => {
13191333 if (
13201334 ! keybindingsConfigPath ||
13211335 ! primaryEnvironment ||
@@ -1335,101 +1349,92 @@ export function KeybindingsSettingsPanel() {
13351349 type : "error" ,
13361350 } ) ;
13371351 } ) ( ) ;
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 ) ,
13471359 )
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 ) ;
13481379 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+ } ;
13801391
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 ) ,
13881397 )
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+ } ;
14141422
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 : {
14191430 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+ } ;
14311436
1432- const cancelAdd = useCallback ( ( ) => setIsAddingBinding ( false ) , [ ] ) ;
1437+ const cancelAdd = ( ) => setIsAddingBinding ( false ) ;
14331438
14341439 const rowActions : KeybindingRowActions = {
14351440 allRows : rows ,
0 commit comments