Skip to content

Commit 11192b6

Browse files
Prefetch browse paths in command palette
- Await browse prefetches before switching views - Seed add-project browse queries from hover - Allow async browse navigation handlers
1 parent cb79e67 commit 11192b6

2 files changed

Lines changed: 72 additions & 35 deletions

File tree

‎apps/web/src/components/CommandPalette.logic.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ export function buildBrowseGroups(input: {
290290
canBrowseUp: boolean;
291291
upIcon: ReactNode;
292292
directoryIcon: ReactNode;
293-
browseUp: () => void;
294-
browseTo: (name: string) => void;
293+
browseUp: () => void | Promise<void>;
294+
browseTo: (name: string) => void | Promise<void>;
295295
}): CommandPaletteGroup[] {
296296
const items: CommandPaletteActionItem[] = [];
297297

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

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import {
7777
buildRootGroups,
7878
buildThreadActionItems,
7979
type CommandPaletteActionItem,
80+
type CommandPaletteGroup,
8081
type CommandPaletteSubmenuItem,
8182
type CommandPaletteView,
8283
filterBrowseEntries,
@@ -333,23 +334,32 @@ function OpenCommandPaletteDialog() {
333334
browseEnvironmentId && currentProjectEnvironmentId === browseEnvironmentId
334335
? currentProjectCwd
335336
: null;
337+
const getBrowseCwdForEnvironment = useCallback(
338+
(environmentId: EnvironmentId | null): string | null =>
339+
environmentId && currentProjectEnvironmentId === environmentId ? currentProjectCwd : null,
340+
[currentProjectCwd, currentProjectEnvironmentId],
341+
);
336342
const relativePathNeedsActiveProject =
337343
isExplicitRelativeProjectPath(query.trim()) && currentProjectCwdForBrowse === null;
338344
const browseDirectoryPath = isBrowsing ? getBrowseDirectoryPath(query) : "";
339345
const browseFilterQuery =
340346
isBrowsing && !hasTrailingPathSeparator(query) ? getBrowseLeafPathSegment(query) : "";
341347

342348
const fetchBrowseResult = useCallback(
343-
async (partialPath: string): Promise<FilesystemBrowseResult | null> => {
344-
if (!browseEnvironmentId) return null;
345-
const api = readEnvironmentApi(browseEnvironmentId);
349+
async (input: {
350+
environmentId: EnvironmentId | null;
351+
partialPath: string;
352+
cwd: string | null;
353+
}): Promise<FilesystemBrowseResult | null> => {
354+
if (!input.environmentId) return null;
355+
const api = readEnvironmentApi(input.environmentId);
346356
if (!api) return null;
347357
return api.filesystem.browse({
348-
partialPath,
349-
...(currentProjectCwdForBrowse ? { cwd: currentProjectCwdForBrowse } : {}),
358+
partialPath: input.partialPath,
359+
...(input.cwd ? { cwd: input.cwd } : {}),
350360
});
351361
},
352-
[browseEnvironmentId, currentProjectCwdForBrowse],
362+
[],
353363
);
354364

355365
const { data: browseResult, isPending: isBrowsePending } = useQuery({
@@ -359,7 +369,13 @@ function OpenCommandPaletteDialog() {
359369
browseDirectoryPath,
360370
currentProjectCwdForBrowse,
361371
],
362-
queryFn: () => fetchBrowseResult(browseDirectoryPath),
372+
queryFn: () =>
373+
fetchBrowseResult({
374+
environmentId: browseEnvironmentId,
375+
partialPath: browseDirectoryPath,
376+
cwd: currentProjectCwdForBrowse,
377+
}),
378+
placeholderData: (previousData) => previousData,
363379
staleTime: BROWSE_STALE_TIME_MS,
364380
enabled:
365381
isBrowsing &&
@@ -374,15 +390,23 @@ function OpenCommandPaletteDialog() {
374390
);
375391

376392
const prefetchBrowsePath = useCallback(
377-
(partialPath: string) => {
378-
void queryClient.prefetchQuery({
379-
queryKey: [
380-
"filesystemBrowse",
381-
browseEnvironmentId,
382-
partialPath,
383-
currentProjectCwdForBrowse,
384-
],
385-
queryFn: () => fetchBrowseResult(partialPath),
393+
async (
394+
partialPath: string,
395+
environmentId: EnvironmentId | null = browseEnvironmentId,
396+
cwd: string | null = currentProjectCwdForBrowse,
397+
) => {
398+
if (!environmentId) {
399+
return;
400+
}
401+
402+
await queryClient.prefetchQuery({
403+
queryKey: ["filesystemBrowse", environmentId, partialPath, cwd],
404+
queryFn: () =>
405+
fetchBrowseResult({
406+
environmentId,
407+
partialPath,
408+
cwd,
409+
}),
386410
staleTime: BROWSE_STALE_TIME_MS,
387411
});
388412
},
@@ -549,15 +573,23 @@ function OpenCommandPaletteDialog() {
549573
}
550574

551575
const startAddProjectBrowse = useCallback(
552-
(environmentId: EnvironmentId): void => {
576+
async (environmentId: EnvironmentId): Promise<void> => {
577+
const initialQuery = getAddProjectInitialQueryForEnvironment(environmentId);
578+
const initialBrowsePath = getBrowseDirectoryPath(initialQuery);
579+
const browseCwd = getBrowseCwdForEnvironment(environmentId);
580+
581+
if (initialBrowsePath.length > 0) {
582+
await prefetchBrowsePath(initialBrowsePath, environmentId, browseCwd);
583+
}
584+
553585
setAddProjectEnvironmentId(environmentId);
554586
pushPaletteView({
555587
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
556588
groups: [],
557-
initialQuery: getAddProjectInitialQueryForEnvironment(environmentId),
589+
initialQuery,
558590
});
559591
},
560-
[getAddProjectInitialQueryForEnvironment],
592+
[getAddProjectInitialQueryForEnvironment, getBrowseCwdForEnvironment, prefetchBrowsePath],
561593
);
562594

563595
const addProjectEnvironmentItems: CommandPaletteActionItem[] = addProjectEnvironmentOptions.map(
@@ -570,7 +602,7 @@ function OpenCommandPaletteDialog() {
570602
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
571603
keepOpen: true,
572604
run: async () => {
573-
startAddProjectBrowse(option.environmentId);
605+
await startAddProjectBrowse(option.environmentId);
574606
},
575607
}),
576608
);
@@ -586,7 +618,7 @@ function OpenCommandPaletteDialog() {
586618
[addProjectEnvironmentItems],
587619
);
588620

589-
const openAddProjectFlow = useCallback(() => {
621+
const openAddProjectFlow = useCallback(async () => {
590622
if (addProjectEnvironmentOptions.length > 1) {
591623
pushPaletteView({
592624
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
@@ -605,7 +637,7 @@ function OpenCommandPaletteDialog() {
605637
return;
606638
}
607639

608-
startAddProjectBrowse(environmentId);
640+
await startAddProjectBrowse(environmentId);
609641
}, [
610642
addProjectEnvironmentGroups,
611643
addProjectEnvironmentOptions.length,
@@ -618,7 +650,7 @@ function OpenCommandPaletteDialog() {
618650
return;
619651
}
620652
clearOpenIntent();
621-
openAddProjectFlow();
653+
void openAddProjectFlow();
622654
}, [clearOpenIntent, openAddProjectFlow, openIntent]);
623655

624656
const actionItems: Array<CommandPaletteActionItem | CommandPaletteSubmenuItem> = [];
@@ -682,7 +714,7 @@ function OpenCommandPaletteDialog() {
682714
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
683715
keepOpen: true,
684716
run: async () => {
685-
openAddProjectFlow();
717+
await openAddProjectFlow();
686718
},
687719
});
688720
}
@@ -803,23 +835,28 @@ function OpenCommandPaletteDialog() {
803835
],
804836
);
805837

806-
function browseTo(name: string): void {
807-
const nextQuery = appendBrowsePathSegment(query, name);
808-
setHighlightedItemValue(null);
809-
setQuery(nextQuery);
810-
setBrowseGeneration((generation) => generation + 1);
811-
}
838+
const browseTo = useCallback(
839+
async (name: string): Promise<void> => {
840+
const nextQuery = appendBrowsePathSegment(query, name);
841+
await prefetchBrowsePath(getBrowseDirectoryPath(nextQuery));
842+
setHighlightedItemValue(null);
843+
setQuery(nextQuery);
844+
setBrowseGeneration((generation) => generation + 1);
845+
},
846+
[prefetchBrowsePath, query],
847+
);
812848

813-
function browseUp(): void {
849+
const browseUp = useCallback(async (): Promise<void> => {
814850
const parentPath = getBrowseParentPath(query);
815851
if (parentPath === null) {
816852
return;
817853
}
818854

855+
await prefetchBrowsePath(parentPath);
819856
setHighlightedItemValue(null);
820857
setQuery(parentPath);
821858
setBrowseGeneration((generation) => generation + 1);
822-
}
859+
}, [prefetchBrowsePath, query]);
823860

824861
// Resolve the add-project path from browse data when available. When the
825862
// query has a trailing separator (e.g. "~/projects/foo/"), parentPath is the
@@ -842,7 +879,7 @@ function OpenCommandPaletteDialog() {
842879
browseTo,
843880
});
844881

845-
let displayedGroups = filteredGroups;
882+
let displayedGroups: ReadonlyArray<CommandPaletteGroup> = filteredGroups;
846883
if (isBrowsing) {
847884
displayedGroups = relativePathNeedsActiveProject ? [] : browseGroups;
848885
}

0 commit comments

Comments
 (0)