Compare commits

...
Author SHA1 Message Date
Cursor Agent f6ccddbfd3 Fix getBrowsePrefetchPaths tests to use reachable state combinations
The tests were passing canBrowseUp: true for queries without trailing
path separators, but in the actual CommandPalette.tsx code, canBrowseUp
is computed via canNavigateUp(query) which requires
hasTrailingPathSeparator(query). This made canBrowseUp: true and
non-null exactEntry mutually exclusive conditions that could never occur
together.

Updated tests to reflect realistic scenarios:
- No trailing separator + exact entry match → prefetch child path only
- Trailing separator + no exact entry → prefetch parent path only
- Partial query with no match → no prefetch paths
2026-04-17 05:43:59 +00:00
Julius Marminge 11192b698d Prefetch browse paths in command palette
- Await browse prefetches before switching views
- Seed add-project browse queries from hover
- Allow async browse navigation handlers
2026-04-16 22:31:52 -07:00
Julius Marminge cb79e67b9a Prefetch browse parent and exact child paths
- Extract browse prefetch path selection into shared logic
- Warm parent directories and exact typed children only
2026-04-16 22:06:12 -07:00
3 changed files with 147 additions and 57 deletions

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,38 @@ describe("buildThreadActionItems", () => {
expect(items.map((item) => item.value)).toEqual(["thread:thread-active"]);
});
});
describe("getBrowsePrefetchPaths", () => {
it("prefetches child path when query has no trailing separator and an exact entry matches", () => {
expect(
getBrowsePrefetchPaths({
browseQuery: "~/Downloads",
canBrowseUp: false,
exactEntry: {
name: "Downloads",
fullPath: "/Users/test/Downloads",
},
}),
).toEqual(["~/Downloads/"]);
});
it("prefetches parent path when query has a trailing separator", () => {
expect(
getBrowsePrefetchPaths({
browseQuery: "~/Downloads/",
canBrowseUp: true,
exactEntry: null,
}),
).toEqual(["~/"]);
});
it("returns no paths for a partial query with no exact match", () => {
expect(
getBrowsePrefetchPaths({
browseQuery: "~/Do",
canBrowseUp: false,
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, " ");
}
@@ -268,8 +290,8 @@ export function buildBrowseGroups(input: {
canBrowseUp: boolean;
upIcon: ReactNode;
directoryIcon: ReactNode;
browseUp: () => void;
browseTo: (name: string) => void;
browseUp: () => void | Promise<void>;
browseTo: (name: string) => void | Promise<void>;
}): CommandPaletteGroup[] {
const items: CommandPaletteActionItem[] = [];
+87 -55
View File
@@ -77,10 +77,12 @@ import {
buildRootGroups,
buildThreadActionItems,
type CommandPaletteActionItem,
type CommandPaletteGroup,
type CommandPaletteSubmenuItem,
type CommandPaletteView,
filterBrowseEntries,
filterCommandPaletteGroups,
getBrowsePrefetchPaths,
getCommandPaletteInputPlaceholder,
getCommandPaletteMode,
ITEM_ICON_CLASS,
@@ -332,6 +334,11 @@ function OpenCommandPaletteDialog() {
browseEnvironmentId && currentProjectEnvironmentId === browseEnvironmentId
? currentProjectCwd
: null;
const getBrowseCwdForEnvironment = useCallback(
(environmentId: EnvironmentId | null): string | null =>
environmentId && currentProjectEnvironmentId === environmentId ? currentProjectCwd : null,
[currentProjectCwd, currentProjectEnvironmentId],
);
const relativePathNeedsActiveProject =
isExplicitRelativeProjectPath(query.trim()) && currentProjectCwdForBrowse === null;
const browseDirectoryPath = isBrowsing ? getBrowseDirectoryPath(query) : "";
@@ -339,16 +346,20 @@ function OpenCommandPaletteDialog() {
isBrowsing && !hasTrailingPathSeparator(query) ? getBrowseLeafPathSegment(query) : "";
const fetchBrowseResult = useCallback(
async (partialPath: string): Promise<FilesystemBrowseResult | null> => {
if (!browseEnvironmentId) return null;
const api = readEnvironmentApi(browseEnvironmentId);
async (input: {
environmentId: EnvironmentId | null;
partialPath: string;
cwd: string | null;
}): Promise<FilesystemBrowseResult | null> => {
if (!input.environmentId) return null;
const api = readEnvironmentApi(input.environmentId);
if (!api) return null;
return api.filesystem.browse({
partialPath,
...(currentProjectCwdForBrowse ? { cwd: currentProjectCwdForBrowse } : {}),
partialPath: input.partialPath,
...(input.cwd ? { cwd: input.cwd } : {}),
});
},
[browseEnvironmentId, currentProjectCwdForBrowse],
[],
);
const { data: browseResult, isPending: isBrowsePending } = useQuery({
@@ -358,7 +369,13 @@ function OpenCommandPaletteDialog() {
browseDirectoryPath,
currentProjectCwdForBrowse,
],
queryFn: () => fetchBrowseResult(browseDirectoryPath),
queryFn: () =>
fetchBrowseResult({
environmentId: browseEnvironmentId,
partialPath: browseDirectoryPath,
cwd: currentProjectCwdForBrowse,
}),
placeholderData: (previousData) => previousData,
staleTime: BROWSE_STALE_TIME_MS,
enabled:
isBrowsing &&
@@ -367,52 +384,54 @@ 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],
);
const prefetchBrowsePath = useCallback(
(partialPath: string) => {
void queryClient.prefetchQuery({
queryKey: [
"filesystemBrowse",
browseEnvironmentId,
partialPath,
currentProjectCwdForBrowse,
],
queryFn: () => fetchBrowseResult(partialPath),
async (
partialPath: string,
environmentId: EnvironmentId | null = browseEnvironmentId,
cwd: string | null = currentProjectCwdForBrowse,
) => {
if (!environmentId) {
return;
}
await queryClient.prefetchQuery({
queryKey: ["filesystemBrowse", environmentId, partialPath, cwd],
queryFn: () =>
fetchBrowseResult({
environmentId,
partialPath,
cwd,
}),
staleTime: BROWSE_STALE_TIME_MS,
});
},
[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]) => {
@@ -554,15 +573,23 @@ function OpenCommandPaletteDialog() {
}
const startAddProjectBrowse = useCallback(
(environmentId: EnvironmentId): void => {
async (environmentId: EnvironmentId): Promise<void> => {
const initialQuery = getAddProjectInitialQueryForEnvironment(environmentId);
const initialBrowsePath = getBrowseDirectoryPath(initialQuery);
const browseCwd = getBrowseCwdForEnvironment(environmentId);
if (initialBrowsePath.length > 0) {
await prefetchBrowsePath(initialBrowsePath, environmentId, browseCwd);
}
setAddProjectEnvironmentId(environmentId);
pushPaletteView({
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
groups: [],
initialQuery: getAddProjectInitialQueryForEnvironment(environmentId),
initialQuery,
});
},
[getAddProjectInitialQueryForEnvironment],
[getAddProjectInitialQueryForEnvironment, getBrowseCwdForEnvironment, prefetchBrowsePath],
);
const addProjectEnvironmentItems: CommandPaletteActionItem[] = addProjectEnvironmentOptions.map(
@@ -575,7 +602,7 @@ function OpenCommandPaletteDialog() {
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
keepOpen: true,
run: async () => {
startAddProjectBrowse(option.environmentId);
await startAddProjectBrowse(option.environmentId);
},
}),
);
@@ -591,7 +618,7 @@ function OpenCommandPaletteDialog() {
[addProjectEnvironmentItems],
);
const openAddProjectFlow = useCallback(() => {
const openAddProjectFlow = useCallback(async () => {
if (addProjectEnvironmentOptions.length > 1) {
pushPaletteView({
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
@@ -610,7 +637,7 @@ function OpenCommandPaletteDialog() {
return;
}
startAddProjectBrowse(environmentId);
await startAddProjectBrowse(environmentId);
}, [
addProjectEnvironmentGroups,
addProjectEnvironmentOptions.length,
@@ -623,7 +650,7 @@ function OpenCommandPaletteDialog() {
return;
}
clearOpenIntent();
openAddProjectFlow();
void openAddProjectFlow();
}, [clearOpenIntent, openAddProjectFlow, openIntent]);
const actionItems: Array<CommandPaletteActionItem | CommandPaletteSubmenuItem> = [];
@@ -687,7 +714,7 @@ function OpenCommandPaletteDialog() {
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
keepOpen: true,
run: async () => {
openAddProjectFlow();
await openAddProjectFlow();
},
});
}
@@ -808,23 +835,28 @@ function OpenCommandPaletteDialog() {
],
);
function browseTo(name: string): void {
const nextQuery = appendBrowsePathSegment(query, name);
setHighlightedItemValue(null);
setQuery(nextQuery);
setBrowseGeneration((generation) => generation + 1);
}
const browseTo = useCallback(
async (name: string): Promise<void> => {
const nextQuery = appendBrowsePathSegment(query, name);
await prefetchBrowsePath(getBrowseDirectoryPath(nextQuery));
setHighlightedItemValue(null);
setQuery(nextQuery);
setBrowseGeneration((generation) => generation + 1);
},
[prefetchBrowsePath, query],
);
function browseUp(): void {
const browseUp = useCallback(async (): Promise<void> => {
const parentPath = getBrowseParentPath(query);
if (parentPath === null) {
return;
}
await prefetchBrowsePath(parentPath);
setHighlightedItemValue(null);
setQuery(parentPath);
setBrowseGeneration((generation) => generation + 1);
}
}, [prefetchBrowsePath, query]);
// Resolve the add-project path from browse data when available. When the
// query has a trailing separator (e.g. "~/projects/foo/"), parentPath is the
@@ -847,7 +879,7 @@ function OpenCommandPaletteDialog() {
browseTo,
});
let displayedGroups = filteredGroups;
let displayedGroups: ReadonlyArray<CommandPaletteGroup> = filteredGroups;
if (isBrowsing) {
displayedGroups = relativePathNeedsActiveProject ? [] : browseGroups;
}