TypeScript
refactor(web): settings search items stay cached without "use memo" (#20771)
Julius Marminge68ed8d35bf38
diff --git a/apps/web/src/components/settings/useAvailableSettingsSearchItems.ts b/apps/web/src/components/settings/useAvailableSettingsSearchItems.tsindex e13fd997989..924b548515f 100644--- a/apps/web/src/components/settings/useAvailableSettingsSearchItems.ts+++ b/apps/web/src/components/settings/useAvailableSettingsSearchItems.ts@@ -18,30 +18,21 @@ import {export function useAvailableSettingsSearchItems(scopeSearch: SettingsScopeSearch = {}) {const { environments } = useEnvironments();- const primaryEnvironmentId = usePrimaryEnvironmentId();- const localEnvironmentDisabled = isLocalEnvironmentDisabled();- const canMaintain = useEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope);- const canManageLocalBackend = !localEnvironmentDisabled && canMaintain;- const desktopWsl = useEnvironmentQuery(- isElectron && canManageLocalBackend ? desktopWslStateAtom : null,- );- const cloudLinkState = usePrimaryCloudLinkState().data;- // Same fallback as the Connections row: older servers imply a tunnel from `linked`.- const managedTunnelActive =- cloudLinkState?.linkRevoked !== true &&- (cloudLinkState?.managedTunnelActive ?? cloudLinkState?.linked ?? false);-- return useFilteredSettingsSearchItems({- localEnvironmentDisabled,- hasCloudPublicConfig: hasCloudPublicConfig(),- hasEnvironment: environments.some((environment) => environment.serverConfig !== null),- hasProviderSettingsEnvironment: environments.some((environment) =>+ // The filtered list must be cached on boolean flags so it stays stable for the search and+ // the command palette while environments update. The compiler cannot tell that `.some()`+ // and other plain calls return primitives, so `=== true` marks each flag as one; without+ // it the flags join the filter's cache scope and the list is rebuilt every render.+ const hasEnvironment =+ environments.some((environment) => environment.serverConfig !== null) === true;+ const hasProviderSettingsEnvironment =+ environments.some((environment) =>isProviderSettingsEnvironmentAvailable({connectionPhase: environment.connection.phase,hasServerConfig: environment.serverConfig !== null,}),- ),- hasMacProviderSettingsEnvironment: environments.some(+ ) === true;+ const hasMacProviderSettingsEnvironment =+ environments.some((environment) =>(scopeSearch.machine === undefined || environment.environmentId === scopeSearch.machine) &&environment.serverConfig?.environment.platform.os === "darwin" &&@@ -49,43 +40,33 @@ export function useAvailableSettingsSearchItems(scopeSearch: SettingsScopeSearchconnectionPhase: environment.connection.phase,hasServerConfig: true,}),- ),- canManageLocalBackend,- isWslSettingsRowVisible: isWslSettingsRowVisible({- state: desktopWsl.data,- error: desktopWsl.error,- }),- hasThreadAutoSettlement:- getThreadAutoSettlementSearchAvailability(environments).eligibleEnvironmentIds.length > 0,- managedTunnelActive,- });-}+ ) === true;+ const hasThreadAutoSettlement =+ getThreadAutoSettlementSearchAvailability(environments).eligibleEnvironmentIds.length > 0;+ const primaryEnvironmentId = usePrimaryEnvironmentId();+ const canMaintain = useEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope);+ const cloudLinkState = usePrimaryCloudLinkState().data;+ // Same fallback as the Connections row: older servers imply a tunnel from `linked`.+ const managedTunnelActive =+ cloudLinkState?.linkRevoked !== true &&+ (cloudLinkState?.managedTunnelActive ?? cloudLinkState?.linked ?? false) === true;+ const localEnvironmentDisabled = isLocalEnvironmentDisabled() === true;+ const canManageLocalBackend = !localEnvironmentDisabled && canMaintain;+ const desktopWsl = useEnvironmentQuery(+ isElectron && canManageLocalBackend ? desktopWslStateAtom : null,+ );+ const cloudPublicConfig = hasCloudPublicConfig() === true;+ const wslSettingsRowVisible =+ isWslSettingsRowVisible({ state: desktopWsl.data, error: desktopWsl.error }) === true;-// The availability flags are primitives, so caching on them keeps the item list stable for the-// search memo and the command palette while environments update.-function useFilteredSettingsSearchItems(- availability: Required<Parameters<typeof filterAvailableSettingsSearchItems>[0]>,-) {- "use memo";- const {- localEnvironmentDisabled,- hasCloudPublicConfig,- hasEnvironment,- hasProviderSettingsEnvironment,- hasMacProviderSettingsEnvironment,- canManageLocalBackend,- isWslSettingsRowVisible,- hasThreadAutoSettlement,- managedTunnelActive,- } = availability;return filterAvailableSettingsSearchItems({localEnvironmentDisabled,- hasCloudPublicConfig,+ hasCloudPublicConfig: cloudPublicConfig,hasEnvironment,hasProviderSettingsEnvironment,hasMacProviderSettingsEnvironment,canManageLocalBackend,- isWslSettingsRowVisible,+ isWslSettingsRowVisible: wslSettingsRowVisible,hasThreadAutoSettlement,managedTunnelActive,});