Prefetch browse parent and exact child paths

- Extract browse prefetch path selection into shared logic
- Warm parent directories and exact typed children only
This commit is contained in:
Julius Marminge committed 2026-04-16 22:06:12 -07:00
1 parent 505db9f696
commit cb79e67b9a
3 files changed
+65 -22

No files matched your search

@@ -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(["~/"]);
});
});
@@ -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, " ");
}
+17 -22
View File
@@ -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]) => {