From cb79e67b9aaf92b14717588ed8e4f7136de3547b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 16 Apr 2026 22:06:12 -0700 Subject: [PATCH] Prefetch browse parent and exact child paths - Extract browse prefetch path selection into shared logic - Warm parent directories and exact typed children only --- .../components/CommandPalette.logic.test.ts | 26 +++++++++++++ .../src/components/CommandPalette.logic.ts | 22 +++++++++++ apps/web/src/components/CommandPalette.tsx | 39 ++++++++----------- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/apps/web/src/components/CommandPalette.logic.test.ts b/apps/web/src/components/CommandPalette.logic.test.ts index a49dadc851..4517d18a1e 100644 --- a/apps/web/src/components/CommandPalette.logic.test.ts +++ b/apps/web/src/components/CommandPalette.logic.test.ts @@ -4,6 +4,7 @@ import type { Thread } from "../types"; import { buildThreadActionItems, filterCommandPaletteGroups, + getBrowsePrefetchPaths, type CommandPaletteGroup, } from "./CommandPalette.logic"; @@ -164,3 +165,28 @@ describe("buildThreadActionItems", () => { expect(items.map((item) => item.value)).toEqual(["thread:thread-active"]); }); }); + +describe("getBrowsePrefetchPaths", () => { + it("prefetches the parent path and an exact typed child path", () => { + expect( + getBrowsePrefetchPaths({ + browseQuery: "~/Downloads", + canBrowseUp: true, + exactEntry: { + name: "Downloads", + fullPath: "/Users/test/Downloads", + }, + }), + ).toEqual(["~/", "~/Downloads/"]); + }); + + it("does not prefetch a child path when the user only has a highlighted browse row", () => { + expect( + getBrowsePrefetchPaths({ + browseQuery: "~/Do", + canBrowseUp: true, + exactEntry: null, + }), + ).toEqual(["~/"]); + }); +}); diff --git a/apps/web/src/components/CommandPalette.logic.ts b/apps/web/src/components/CommandPalette.logic.ts index 3e2f1ec890..9a0a408249 100644 --- a/apps/web/src/components/CommandPalette.logic.ts +++ b/apps/web/src/components/CommandPalette.logic.ts @@ -1,6 +1,7 @@ import { type KeybindingCommand, type FilesystemBrowseEntry } from "@t3tools/contracts"; import type { SidebarThreadSortOrder } from "@t3tools/contracts/settings"; import { type ReactNode } from "react"; +import { appendBrowsePathSegment, getBrowseParentPath } from "../lib/projectPaths"; import { sortThreads } from "../lib/threadSort"; import { formatRelativeTimeLabel } from "../timestampFormat"; import { type Project, type SidebarThreadSummary, type Thread } from "../types"; @@ -79,6 +80,27 @@ export function filterBrowseEntries(input: { return { filteredEntries, highlightedEntry, exactEntry }; } +export function getBrowsePrefetchPaths(input: { + browseQuery: string; + canBrowseUp: boolean; + exactEntry: FilesystemBrowseEntry | null; +}): string[] { + const paths: string[] = []; + + if (input.canBrowseUp) { + const parentPath = getBrowseParentPath(input.browseQuery); + if (parentPath !== null) { + paths.push(parentPath); + } + } + + if (input.exactEntry) { + paths.push(appendBrowsePathSegment(input.browseQuery, input.exactEntry.name)); + } + + return paths; +} + export function normalizeSearchText(value: string): string { return value.trim().toLowerCase().replace(/\s+/g, " "); } diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx index fbbeda1013..537c0665b0 100644 --- a/apps/web/src/components/CommandPalette.tsx +++ b/apps/web/src/components/CommandPalette.tsx @@ -81,6 +81,7 @@ import { type CommandPaletteView, filterBrowseEntries, filterCommandPaletteGroups, + getBrowsePrefetchPaths, getCommandPaletteInputPlaceholder, getCommandPaletteMode, ITEM_ICON_CLASS, @@ -367,11 +368,7 @@ function OpenCommandPaletteDialog() { !relativePathNeedsActiveProject, }); const browseEntries = browseResult?.entries ?? EMPTY_BROWSE_ENTRIES; - const { - filteredEntries: filteredBrowseEntries, - highlightedEntry: highlightedBrowseEntry, - exactEntry: exactBrowseEntry, - } = useMemo( + const { filteredEntries: filteredBrowseEntries, exactEntry: exactBrowseEntry } = useMemo( () => filterBrowseEntries({ browseEntries, browseFilterQuery, highlightedItemValue }), [browseEntries, browseFilterQuery, highlightedItemValue], ); @@ -392,27 +389,25 @@ function OpenCommandPaletteDialog() { [browseEnvironmentId, currentProjectCwdForBrowse, fetchBrowseResult, queryClient], ); - // Prefetch the parent and the most likely next child so browse navigation - // stays warm without scanning every child directory in large trees. + const browsePrefetchPaths = useMemo( + () => + getBrowsePrefetchPaths({ + browseQuery: query, + canBrowseUp: canNavigateUp(query), + exactEntry: exactBrowseEntry, + }), + [exactBrowseEntry, query], + ); + + // Prefetch the parent and only an exact typed child so browse navigation + // stays warm without turning pointer hover into a directory read. useEffect(() => { if (!isBrowsing || filteredBrowseEntries.length === 0) return; - if (canNavigateUp(query)) { - prefetchBrowsePath(getBrowseParentPath(query)!); + for (const partialPath of browsePrefetchPaths) { + prefetchBrowsePath(partialPath); } - - const nextChild = highlightedBrowseEntry ?? exactBrowseEntry; - if (nextChild) { - prefetchBrowsePath(appendBrowsePathSegment(query, nextChild.name)); - } - }, [ - exactBrowseEntry, - filteredBrowseEntries.length, - highlightedBrowseEntry, - isBrowsing, - prefetchBrowsePath, - query, - ]); + }, [filteredBrowseEntries.length, browsePrefetchPaths, isBrowsing, prefetchBrowsePath]); const openProjectFromSearch = useMemo( () => async (project: (typeof projects)[number]) => {