Compare commits

...
Author SHA1 Message Date
Cursor Agent 689afc5e89 Remove unused highlightedItemValue from filterBrowseEntries
Remove the highlightedItemValue parameter and highlightedEntry computation
from filterBrowseEntries since the result was never destructured or used
by the caller. This eliminates unnecessary useMemo recomputation on every
hover/highlight change in the command palette.
2026-04-17 05:15:13 +00: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 68 additions and 33 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,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";
@@ -50,10 +51,8 @@ export type CommandPaletteMode = "root" | "root-browse" | "submenu" | "submenu-b
export function filterBrowseEntries(input: {
browseEntries: ReadonlyArray<FilesystemBrowseEntry>;
browseFilterQuery: string;
highlightedItemValue: string | null;
}): {
filteredEntries: FilesystemBrowseEntry[];
highlightedEntry: FilesystemBrowseEntry | null;
exactEntry: FilesystemBrowseEntry | null;
} {
const lowerFilter = input.browseFilterQuery.toLowerCase();
@@ -65,18 +64,33 @@ export function filterBrowseEntries(input: {
(showHidden || !entry.name.startsWith(".")),
);
let highlightedEntry: FilesystemBrowseEntry | null = null;
if (input.highlightedItemValue?.startsWith("browse:")) {
const highlightedPath = input.highlightedItemValue.slice("browse:".length);
highlightedEntry = filteredEntries.find((entry) => entry.fullPath === highlightedPath) ?? null;
}
const exactEntry =
input.browseFilterQuery.length > 0
? (filteredEntries.find((entry) => entry.name === input.browseFilterQuery) ?? null)
: null;
return { filteredEntries, highlightedEntry, exactEntry };
return { filteredEntries, 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 {
+19 -24
View File
@@ -81,6 +81,7 @@ import {
type CommandPaletteView,
filterBrowseEntries,
filterCommandPaletteGroups,
getBrowsePrefetchPaths,
getCommandPaletteInputPlaceholder,
getCommandPaletteMode,
ITEM_ICON_CLASS,
@@ -367,13 +368,9 @@ function OpenCommandPaletteDialog() {
!relativePathNeedsActiveProject,
});
const browseEntries = browseResult?.entries ?? EMPTY_BROWSE_ENTRIES;
const {
filteredEntries: filteredBrowseEntries,
highlightedEntry: highlightedBrowseEntry,
exactEntry: exactBrowseEntry,
} = useMemo(
() => filterBrowseEntries({ browseEntries, browseFilterQuery, highlightedItemValue }),
[browseEntries, browseFilterQuery, highlightedItemValue],
const { filteredEntries: filteredBrowseEntries, exactEntry: exactBrowseEntry } = useMemo(
() => filterBrowseEntries({ browseEntries, browseFilterQuery }),
[browseEntries, browseFilterQuery],
);
const prefetchBrowsePath = useCallback(
@@ -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]) => {