Skip to content

Commit 2e16cf2

Browse files
refactor(web): the command palette caches its derived lookups without "use memo" (pingdotgg#21116)
1 parent 8f02648 commit 2e16cf2

2 files changed

Lines changed: 34 additions & 58 deletions

File tree

‎apps/web/src/components/CommandPalette.tsx‎

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,12 @@ function notifyThemeSaveFailure(): void {
371371
);
372372
}
373373

374-
// These helpers keep "use memo" until the palette's item building moves out of this
375-
// component: inlined, each result reaches the returned <CommandPaletteContent> through
376-
// the action and thread item arrays built after the browse hooks, and the compiler
377-
// leaves all of them uncached.
378-
function useThemeCards(
379-
customThemes: ReturnType<typeof useCustomThemes>,
380-
environmentThemes: ReturnType<typeof useEnvironmentThemeDefinitions>,
381-
) {
382-
// oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
383-
"use memo";
374+
// Each helper reads its own source hook so the compiler caches the derived value on that
375+
// source. Taking the source as an argument instead leaves the helper hook-free, and the
376+
// compiler then treats it as a plain function and caches nothing.
377+
function useThemeCards() {
378+
const customThemes = useCustomThemes();
379+
const environmentThemes = useEnvironmentThemeDefinitions();
384380
const seen = new Set<string>();
385381
return [
386382
...STANDARD_THEME_CARDS.map((card) => ({ ...card, id: null })),
@@ -394,21 +390,23 @@ function useThemeCards(
394390
];
395391
}
396392

397-
function useThreadContentMatchByKey(matches: ReturnType<typeof useThreadSearch>["matches"]) {
398-
// oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
399-
"use memo";
400-
return new Map(
401-
matches.flatMap((match) =>
393+
function useThreadSearchWithContentMatches(
394+
environmentIds: ReadonlyArray<EnvironmentId>,
395+
query: string,
396+
) {
397+
const threadSearch = useThreadSearch(environmentIds, query);
398+
const contentMatchByKey = new Map(
399+
threadSearch.matches.flatMap((match) =>
402400
match.source === "user" || match.source === "assistant"
403401
? [[threadSearchMatchKey(match), match] as const]
404402
: [],
405403
),
406404
);
405+
return { threadSearch, contentMatchByKey };
407406
}
408407

409-
function useEnvironmentLookups(environments: ReturnType<typeof useEnvironments>["environments"]) {
410-
// oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
411-
"use memo";
408+
function useEnvironmentLookups() {
409+
const { environments } = useEnvironments();
412410
const environmentLabelById = new Map(
413411
environments.map((environment) => [environment.environmentId, environment.label] as const),
414412
);
@@ -429,9 +427,8 @@ function useEnvironmentLookups(environments: ReturnType<typeof useEnvironments>[
429427
return { environmentLabelById, projectEnvironmentLocationById };
430428
}
431429

432-
function useProjectLookups(projects: ReturnType<typeof useProjects>) {
433-
// oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
434-
"use memo";
430+
function useProjectLookups() {
431+
const projects = useProjects();
435432
const projectCwdById = new Map<ProjectId, string>(
436433
projects.map((project) => [project.id, project.workspaceRoot]),
437434
);
@@ -450,25 +447,13 @@ const selectCommandPaletteClientSettings = (settings: ClientSettings) => ({
450447
sidebarProjectSortOrder: settings.sidebarProjectSortOrder,
451448
sidebarThreadSortOrder: settings.sidebarThreadSortOrder,
452449
});
453-
type CommandPaletteClientSettings = ReturnType<typeof selectCommandPaletteClientSettings>;
454-
455-
function useSortedProjectGroups({
456-
projects,
457-
projectOrder,
458-
clientSettings,
459-
primaryEnvironmentId,
460-
environmentLabelById,
461-
threads,
462-
}: {
463-
readonly projects: ReturnType<typeof useProjects>;
464-
readonly projectOrder: ReturnType<typeof useUiStateStore.getState>["projectOrder"];
465-
readonly clientSettings: CommandPaletteClientSettings;
466-
readonly primaryEnvironmentId: ReturnType<typeof usePrimaryEnvironmentId>;
467-
readonly environmentLabelById: ReadonlyMap<EnvironmentId, string>;
468-
readonly threads: ReturnType<typeof useThreadShells>;
469-
}) {
470-
// oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
471-
"use memo";
450+
451+
function useSortedProjectGroups(environmentLabelById: ReadonlyMap<EnvironmentId, string>) {
452+
const projects = useProjects();
453+
const threads = useThreadShells();
454+
const projectOrder = useUiStateStore((store) => store.projectOrder);
455+
const clientSettings = useClientSettings(selectCommandPaletteClientSettings);
456+
const primaryEnvironmentId = usePrimaryEnvironmentId();
472457
const projectGroupingSettings = {
473458
sidebarProjectGroupingMode: clientSettings.sidebarProjectGroupingMode,
474459
sidebarProjectGroupingOverrides: clientSettings.sidebarProjectGroupingOverrides,
@@ -806,7 +791,6 @@ function OpenCommandPaletteDialog(props: {
806791
);
807792
}
808793
};
809-
const projectOrder = useUiStateStore((store) => store.projectOrder);
810794
const threads = useThreadShells();
811795
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
812796
const {
@@ -818,9 +802,7 @@ function OpenCommandPaletteDialog(props: {
818802
setTheme,
819803
setThemeHalf,
820804
} = useTheme();
821-
const customThemes = useCustomThemes();
822-
const environmentThemes = useEnvironmentThemeDefinitions();
823-
const themeCards = useThemeCards(customThemes, environmentThemes);
805+
const themeCards = useThemeCards();
824806
const providers = useAtomValue(primaryServerProvidersAtom);
825807
const providersByEnvironment = useEnvironmentProviders();
826808
const providerEntryByEnvironmentAndInstanceId = new Map<string, ProviderInstanceEntry>(
@@ -844,8 +826,8 @@ function OpenCommandPaletteDialog(props: {
844826
.filter((environment) => environment.connection.phase === "connected")
845827
.map((environment) => environment.environmentId);
846828
const threadSearchQuery = currentView === null && !isActionsOnly ? deferredQuery : "";
847-
const threadSearch = useThreadSearch(environmentIds, threadSearchQuery);
848-
const threadContentMatchByKey = useThreadContentMatchByKey(threadSearch.matches);
829+
const { threadSearch, contentMatchByKey: threadContentMatchByKey } =
830+
useThreadSearchWithContentMatches(environmentIds, threadSearchQuery);
849831
const [browseGeneration, setBrowseGeneration] = useState(0);
850832
const [browseNavigation] = useState(createBrowseNavigationCoordinator);
851833
const [addProjectEnvironmentId, setAddProjectEnvironmentId] = useState<EnvironmentId | null>(
@@ -868,16 +850,8 @@ function OpenCommandPaletteDialog(props: {
868850
const cloneLookupGeneration = useRef(0);
869851
const [isRemoteProjectLookingUp, setIsRemoteProjectLookingUp] = useState(false);
870852
const [isRemoteProjectCloning, setIsRemoteProjectCloning] = useState(false);
871-
const { environmentLabelById, projectEnvironmentLocationById } =
872-
useEnvironmentLookups(environments);
873-
const projectGroups = useSortedProjectGroups({
874-
projects,
875-
projectOrder,
876-
clientSettings,
877-
primaryEnvironmentId,
878-
environmentLabelById,
879-
threads,
880-
});
853+
const { environmentLabelById, projectEnvironmentLocationById } = useEnvironmentLookups();
854+
const projectGroups = useSortedProjectGroups(environmentLabelById);
881855
const contextualProjectRef = resolveThreadActionProjectRef({
882856
activeDraftThread,
883857
activeThread: activeThread ?? undefined,
@@ -1000,7 +974,7 @@ function OpenCommandPaletteDialog(props: {
1000974
return ensureBrowseDirectoryPath(baseDirectory);
1001975
};
1002976

1003-
const { projectCwdById, projectByKey, projectTitleById } = useProjectLookups(projects);
977+
const { projectCwdById, projectByKey, projectTitleById } = useProjectLookups();
1004978

1005979
const activeThreadId = activeThread?.id;
1006980
const supportsThreadFind =

‎docs/internals/web-ui.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ Every file under `apps/web/src` must compile. Lint makes those rules errors ther
6666
no upstream rule bans them outright. It also rejects `"use memo"`. The compiler withholds a cache
6767
scope when a value reaches plain function calls on both sides of a hook call. A `"use memo"`
6868
helper forces one, but hides the ordering problem. Fix the order instead: plain uses of a value go
69-
before the hook, or the helper is inlined. Lint misses some bailouts, notably `try` with `finally` and
69+
before the hook, or the helper is inlined. A `use*` helper that calls no hook is compiled as a
70+
plain function and caches nothing, so a derived-value helper reads its source hook itself instead
71+
of taking the value as an argument. Lint misses some bailouts, notably `try` with `finally` and
7072
`throw` inside `try`, so [`reactCompilerCoverage.test.ts`](../../apps/web/src/reactCompilerCoverage.test.ts)
7173
compiles every file and fails on any skip. Async handlers rarely need `try`: an
7274
atom command settles into a result, so cleanup can follow the `await`. `useStableCallback` still

0 commit comments

Comments
 (0)