fix(web): defer command palette filesystem navigation (#2109)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
5fcdefd057
commit
1ba3d01bc6
3 files changed
+276
-48
No files matched your search
@@ -2,7 +2,10 @@ import { describe, expect, it, vi } from "vite-plus/test";
|
||||
import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
|
||||
import type { Thread } from "../types";
|
||||
import {
|
||||
buildBrowseGroups,
|
||||
buildThreadActionItems,
|
||||
canPreloadBrowsePath,
|
||||
createBrowseNavigationCoordinator,
|
||||
enumerateCommandPaletteItems,
|
||||
filterCommandPaletteGroups,
|
||||
type CommandPaletteGroup,
|
||||
@@ -193,3 +196,103 @@ describe("buildThreadActionItems", () => {
|
||||
expect(items.map((item) => item.value)).toEqual(["thread:thread-active"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildBrowseGroups", () => {
|
||||
it("waits for asynchronous browse navigation actions", async () => {
|
||||
let finishNavigation: (() => void) | undefined;
|
||||
const browseTo = vi.fn(
|
||||
() =>
|
||||
new Promise<void>((resolve) => {
|
||||
finishNavigation = resolve;
|
||||
}),
|
||||
);
|
||||
const groups = buildBrowseGroups({
|
||||
browseEntries: [{ name: "Downloads", fullPath: "/Users/test/Downloads" }],
|
||||
browseQuery: "~/",
|
||||
canBrowseUp: false,
|
||||
upIcon: null,
|
||||
directoryIcon: null,
|
||||
browseUp: vi.fn(),
|
||||
browseTo,
|
||||
});
|
||||
const item = groups[0]?.items[0];
|
||||
if (!item || item.kind !== "action") {
|
||||
throw new Error("Expected a browse action");
|
||||
}
|
||||
|
||||
let actionSettled = false;
|
||||
const action = item.run().then(() => {
|
||||
actionSettled = true;
|
||||
});
|
||||
await Promise.resolve();
|
||||
|
||||
expect(browseTo).toHaveBeenCalledWith("Downloads");
|
||||
expect(actionSettled).toBe(false);
|
||||
|
||||
finishNavigation?.();
|
||||
await action;
|
||||
expect(actionSettled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createBrowseNavigationCoordinator", () => {
|
||||
it("only commits the latest overlapping navigation", async () => {
|
||||
const coordinator = createBrowseNavigationCoordinator();
|
||||
let finishFirst: (() => void) | undefined;
|
||||
let finishSecond: (() => void) | undefined;
|
||||
const commits: string[] = [];
|
||||
|
||||
const first = coordinator.run({
|
||||
load: () =>
|
||||
new Promise<void>((resolve) => {
|
||||
finishFirst = resolve;
|
||||
}),
|
||||
commit: () => {
|
||||
commits.push("first");
|
||||
},
|
||||
});
|
||||
const second = coordinator.run({
|
||||
load: () =>
|
||||
new Promise<void>((resolve) => {
|
||||
finishSecond = resolve;
|
||||
}),
|
||||
commit: () => {
|
||||
commits.push("second");
|
||||
},
|
||||
});
|
||||
|
||||
finishSecond?.();
|
||||
await expect(second).resolves.toBe(true);
|
||||
finishFirst?.();
|
||||
await expect(first).resolves.toBe(false);
|
||||
expect(commits).toEqual(["second"]);
|
||||
});
|
||||
|
||||
it("does not commit after newer user input invalidates the navigation", async () => {
|
||||
const coordinator = createBrowseNavigationCoordinator();
|
||||
let finishNavigation: (() => void) | undefined;
|
||||
const commit = vi.fn();
|
||||
const navigation = coordinator.run({
|
||||
load: () =>
|
||||
new Promise<void>((resolve) => {
|
||||
finishNavigation = resolve;
|
||||
}),
|
||||
commit,
|
||||
});
|
||||
|
||||
coordinator.invalidate();
|
||||
finishNavigation?.();
|
||||
|
||||
await expect(navigation).resolves.toBe(false);
|
||||
expect(commit).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("canPreloadBrowsePath", () => {
|
||||
it("only preloads paths for connected environments", () => {
|
||||
expect(canPreloadBrowsePath("connected")).toBe(true);
|
||||
expect(canPreloadBrowsePath("offline")).toBe(false);
|
||||
expect(canPreloadBrowsePath("reconnecting")).toBe(false);
|
||||
expect(canPreloadBrowsePath(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
type FilesystemBrowseEntry,
|
||||
THREAD_JUMP_KEYBINDING_COMMANDS,
|
||||
} from "@t3tools/contracts";
|
||||
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
|
||||
import type { SidebarThreadSortOrder } from "@t3tools/contracts/settings";
|
||||
import * as Arr from "effect/Array";
|
||||
import * as Result from "effect/Result";
|
||||
@@ -15,6 +16,39 @@ export const RECENT_THREAD_LIMIT = 12;
|
||||
export const ITEM_ICON_CLASS = "size-4 text-muted-foreground/80";
|
||||
export const ADDON_ICON_CLASS = "size-4";
|
||||
|
||||
export interface BrowseNavigationCoordinator {
|
||||
readonly invalidate: () => void;
|
||||
readonly run: (input: {
|
||||
readonly load: () => Promise<void>;
|
||||
readonly commit: () => void;
|
||||
}) => Promise<boolean>;
|
||||
}
|
||||
|
||||
export function createBrowseNavigationCoordinator(): BrowseNavigationCoordinator {
|
||||
let generation = 0;
|
||||
|
||||
return {
|
||||
invalidate: () => {
|
||||
generation += 1;
|
||||
},
|
||||
run: async (input) => {
|
||||
const navigationGeneration = ++generation;
|
||||
await input.load();
|
||||
if (navigationGeneration !== generation) {
|
||||
return false;
|
||||
}
|
||||
input.commit();
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function canPreloadBrowsePath(
|
||||
connectionPhase: EnvironmentConnectionPhase | null | undefined,
|
||||
): boolean {
|
||||
return connectionPhase === "connected";
|
||||
}
|
||||
|
||||
export interface CommandPaletteItem {
|
||||
readonly kind: "action" | "submenu";
|
||||
readonly value: string;
|
||||
@@ -73,10 +107,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();
|
||||
@@ -88,18 +120,12 @@ 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 normalizeSearchText(value: string): string {
|
||||
@@ -302,8 +328,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[] = [];
|
||||
|
||||
@@ -316,7 +342,7 @@ export function buildBrowseGroups(input: {
|
||||
icon: input.upIcon,
|
||||
keepOpen: true,
|
||||
run: async () => {
|
||||
input.browseUp();
|
||||
await input.browseUp();
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -330,7 +356,7 @@ export function buildBrowseGroups(input: {
|
||||
icon: input.directoryIcon,
|
||||
keepOpen: true,
|
||||
run: async () => {
|
||||
input.browseTo(entry.name);
|
||||
await input.browseTo(entry.name);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -92,6 +92,8 @@ import {
|
||||
buildProjectActionItems,
|
||||
buildRootGroups,
|
||||
buildThreadActionItems,
|
||||
canPreloadBrowsePath,
|
||||
createBrowseNavigationCoordinator,
|
||||
enumerateCommandPaletteItems,
|
||||
type CommandPaletteActionItem,
|
||||
type CommandPaletteSubmenuItem,
|
||||
@@ -486,6 +488,10 @@ function OpenCommandPaletteDialog(props: {
|
||||
const lookupRepository = useAtomQueryRunner(sourceControlEnvironment.repository, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const loadBrowsePath = useAtomQueryRunner(filesystemEnvironment.browse, {
|
||||
reportFailure: false,
|
||||
reportDefect: false,
|
||||
});
|
||||
const cloneRepository = useAtomCommand(sourceControlEnvironment.cloneRepository, {
|
||||
reportFailure: false,
|
||||
});
|
||||
@@ -502,6 +508,13 @@ function OpenCommandPaletteDialog(props: {
|
||||
const [viewStack, setViewStack] = useState<CommandPaletteView[]>([]);
|
||||
const currentView = viewStack.at(-1) ?? null;
|
||||
const [browseGeneration, setBrowseGeneration] = useState(0);
|
||||
const browseNavigationRef = useRef<ReturnType<typeof createBrowseNavigationCoordinator> | null>(
|
||||
null,
|
||||
);
|
||||
if (browseNavigationRef.current === null) {
|
||||
browseNavigationRef.current = createBrowseNavigationCoordinator();
|
||||
}
|
||||
const browseNavigation = browseNavigationRef.current;
|
||||
const [addProjectEnvironmentId, setAddProjectEnvironmentId] = useState<EnvironmentId | null>(
|
||||
null,
|
||||
);
|
||||
@@ -710,6 +723,11 @@ function OpenCommandPaletteDialog(props: {
|
||||
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) : "";
|
||||
@@ -733,8 +751,42 @@ function OpenCommandPaletteDialog(props: {
|
||||
const isBrowsePending = browseQuery.isPending;
|
||||
const browseEntries = browseResult?.entries ?? EMPTY_BROWSE_ENTRIES;
|
||||
const { filteredEntries: filteredBrowseEntries, exactEntry: exactBrowseEntry } = useMemo(
|
||||
() => filterBrowseEntries({ browseEntries, browseFilterQuery, highlightedItemValue }),
|
||||
[browseEntries, browseFilterQuery, highlightedItemValue],
|
||||
() => filterBrowseEntries({ browseEntries, browseFilterQuery }),
|
||||
[browseEntries, browseFilterQuery],
|
||||
);
|
||||
|
||||
const prefetchBrowsePath = useCallback(
|
||||
async (
|
||||
partialPath: string,
|
||||
environmentId: EnvironmentId | null = browseEnvironmentId,
|
||||
cwd: string | null = currentProjectCwdForBrowse,
|
||||
): Promise<void> => {
|
||||
if (!environmentId) {
|
||||
return;
|
||||
}
|
||||
const environment = environments.find(
|
||||
(candidate) => candidate.environmentId === environmentId,
|
||||
);
|
||||
if (!canPreloadBrowsePath(environment?.connection.phase)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await loadBrowsePath({
|
||||
environmentId,
|
||||
input: {
|
||||
partialPath,
|
||||
...(cwd ? { cwd } : {}),
|
||||
},
|
||||
});
|
||||
},
|
||||
[browseEnvironmentId, currentProjectCwdForBrowse, environments, loadBrowsePath],
|
||||
);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
browseNavigation.invalidate();
|
||||
},
|
||||
[browseNavigation],
|
||||
);
|
||||
|
||||
const openProjectFromSearch = useMemo(
|
||||
@@ -865,18 +917,22 @@ function OpenCommandPaletteDialog(props: {
|
||||
);
|
||||
const recentThreadItems = allThreadItems.slice(0, RECENT_THREAD_LIMIT);
|
||||
|
||||
function pushPaletteView(view: CommandPaletteView): void {
|
||||
setViewStack((previousViews) => [
|
||||
...previousViews,
|
||||
{
|
||||
addonIcon: view.addonIcon,
|
||||
groups: view.groups,
|
||||
...(view.initialQuery ? { initialQuery: view.initialQuery } : {}),
|
||||
},
|
||||
]);
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(view.initialQuery ?? "");
|
||||
}
|
||||
const pushPaletteView = useCallback(
|
||||
(view: CommandPaletteView): void => {
|
||||
browseNavigation.invalidate();
|
||||
setViewStack((previousViews) => [
|
||||
...previousViews,
|
||||
{
|
||||
addonIcon: view.addonIcon,
|
||||
groups: view.groups,
|
||||
...(view.initialQuery ? { initialQuery: view.initialQuery } : {}),
|
||||
},
|
||||
]);
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(view.initialQuery ?? "");
|
||||
},
|
||||
[browseNavigation],
|
||||
);
|
||||
|
||||
function pushView(item: CommandPaletteSubmenuItem): void {
|
||||
pushPaletteView({
|
||||
@@ -887,6 +943,7 @@ function OpenCommandPaletteDialog(props: {
|
||||
}
|
||||
|
||||
function popView(): void {
|
||||
browseNavigation.invalidate();
|
||||
setAddProjectCloneFlow(null);
|
||||
if (viewStack.length <= 1) {
|
||||
setAddProjectEnvironmentId(null);
|
||||
@@ -897,6 +954,7 @@ function OpenCommandPaletteDialog(props: {
|
||||
}
|
||||
|
||||
function handleQueryChange(nextQuery: string): void {
|
||||
browseNavigation.invalidate();
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(nextQuery);
|
||||
if (nextQuery === "" && currentView?.initialQuery) {
|
||||
@@ -905,16 +963,35 @@ function OpenCommandPaletteDialog(props: {
|
||||
}
|
||||
|
||||
const startAddProjectBrowse = useCallback(
|
||||
(environmentId: EnvironmentId): void => {
|
||||
setAddProjectEnvironmentId(environmentId);
|
||||
setAddProjectCloneFlow(null);
|
||||
pushPaletteView({
|
||||
async (environmentId: EnvironmentId): Promise<void> => {
|
||||
const initialQuery = getAddProjectInitialQueryForEnvironment(environmentId);
|
||||
const initialBrowsePath = getBrowseDirectoryPath(initialQuery);
|
||||
const browseCwd = getBrowseCwdForEnvironment(environmentId);
|
||||
const view: CommandPaletteView = {
|
||||
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
|
||||
groups: [],
|
||||
initialQuery: getAddProjectInitialQueryForEnvironment(environmentId),
|
||||
initialQuery,
|
||||
};
|
||||
|
||||
await browseNavigation.run({
|
||||
load: () =>
|
||||
initialBrowsePath.length > 0
|
||||
? prefetchBrowsePath(initialBrowsePath, environmentId, browseCwd)
|
||||
: Promise.resolve(),
|
||||
commit: () => {
|
||||
setAddProjectEnvironmentId(environmentId);
|
||||
setAddProjectCloneFlow(null);
|
||||
pushPaletteView(view);
|
||||
},
|
||||
});
|
||||
},
|
||||
[getAddProjectInitialQueryForEnvironment],
|
||||
[
|
||||
browseNavigation,
|
||||
getAddProjectInitialQueryForEnvironment,
|
||||
getBrowseCwdForEnvironment,
|
||||
prefetchBrowsePath,
|
||||
pushPaletteView,
|
||||
],
|
||||
);
|
||||
|
||||
const startAddProjectClone = useCallback(
|
||||
@@ -927,7 +1004,7 @@ function OpenCommandPaletteDialog(props: {
|
||||
initialQuery: "",
|
||||
});
|
||||
},
|
||||
[],
|
||||
[pushPaletteView],
|
||||
);
|
||||
|
||||
const openSourceControlSettings = useCallback(() => {
|
||||
@@ -950,7 +1027,7 @@ function OpenCommandPaletteDialog(props: {
|
||||
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
|
||||
keepOpen: true,
|
||||
run: async () => {
|
||||
startAddProjectBrowse(environmentId);
|
||||
await startAddProjectBrowse(environmentId);
|
||||
},
|
||||
},
|
||||
];
|
||||
@@ -1043,7 +1120,12 @@ function OpenCommandPaletteDialog(props: {
|
||||
),
|
||||
});
|
||||
},
|
||||
[browseEnvironmentId, buildAddProjectSourceGroups, sourceControlDiscovery.data],
|
||||
[
|
||||
browseEnvironmentId,
|
||||
buildAddProjectSourceGroups,
|
||||
pushPaletteView,
|
||||
sourceControlDiscovery.data,
|
||||
],
|
||||
);
|
||||
|
||||
const addProjectEnvironmentItems: CommandPaletteActionItem[] = addProjectEnvironmentOptions.map(
|
||||
@@ -1098,6 +1180,7 @@ function OpenCommandPaletteDialog(props: {
|
||||
addProjectEnvironmentGroups,
|
||||
addProjectEnvironmentOptions.length,
|
||||
defaultAddProjectEnvironmentId,
|
||||
pushPaletteView,
|
||||
startAddProjectSourceSelection,
|
||||
]);
|
||||
|
||||
@@ -1114,6 +1197,7 @@ function OpenCommandPaletteDialog(props: {
|
||||
return;
|
||||
}
|
||||
clearOpenIntent();
|
||||
browseNavigation.invalidate();
|
||||
setAddProjectCloneFlow(null);
|
||||
setViewStack([]);
|
||||
setQuery("");
|
||||
@@ -1139,10 +1223,12 @@ function OpenCommandPaletteDialog(props: {
|
||||
});
|
||||
}, [
|
||||
clearOpenIntent,
|
||||
browseNavigation,
|
||||
currentProjectEnvironmentId,
|
||||
currentProjectId,
|
||||
openIntent,
|
||||
projectThreadItems,
|
||||
pushPaletteView,
|
||||
]);
|
||||
|
||||
const actionItems: Array<CommandPaletteActionItem | CommandPaletteSubmenuItem> = [];
|
||||
@@ -1225,7 +1311,7 @@ function OpenCommandPaletteDialog(props: {
|
||||
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
|
||||
keepOpen: true,
|
||||
run: async () => {
|
||||
startAddProjectBrowse(wslAddProjectEnvironmentOption.environmentId);
|
||||
await startAddProjectBrowse(wslAddProjectEnvironmentOption.environmentId);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1541,23 +1627,36 @@ function OpenCommandPaletteDialog(props: {
|
||||
await handleAddProject(cloneResult.value.cwd);
|
||||
}
|
||||
|
||||
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 browseNavigation.run({
|
||||
load: () => prefetchBrowsePath(getBrowseDirectoryPath(nextQuery)),
|
||||
commit: () => {
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(nextQuery);
|
||||
setBrowseGeneration((generation) => generation + 1);
|
||||
},
|
||||
});
|
||||
},
|
||||
[browseNavigation, prefetchBrowsePath, query],
|
||||
);
|
||||
|
||||
function browseUp(): void {
|
||||
const browseUp = useCallback(async (): Promise<void> => {
|
||||
const parentPath = getBrowseParentPath(query);
|
||||
if (parentPath === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(parentPath);
|
||||
setBrowseGeneration((generation) => generation + 1);
|
||||
}
|
||||
await browseNavigation.run({
|
||||
load: () => prefetchBrowsePath(parentPath),
|
||||
commit: () => {
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(parentPath);
|
||||
setBrowseGeneration((generation) => generation + 1);
|
||||
},
|
||||
});
|
||||
}, [browseNavigation, 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
|
||||
|
||||
Reference in new issue
Block a user