fix(explorer): apply directory toggles atomically

This commit is contained in:
Mohamed Boudra
2026-07-29 10:26:17 +00:00
parent c4aa12c8a2
commit af1b6be51f
3 changed files with 46 additions and 9 deletions

View File

@@ -42,6 +42,7 @@ import {
flattenExplorerTree,
reconcileRestoredExpandedPaths,
restoreExpandedDirectories,
setExpandedDirectoryPath,
showHiddenFilesAndRestoreExpandedDirectories,
type ExplorerTreeRow,
} from "@/file-explorer/tree";
@@ -755,15 +756,14 @@ function toggleDirectory({
return;
}
const isExpanded = expandedPaths.has(entry.path);
if (isExpanded) {
setExpandedPathsForWorkspace(
workspaceStateKey,
Array.from(expandedPaths).filter((path) => path !== entry.path),
);
return;
}
setExpandedPathsForWorkspace(workspaceStateKey, [...Array.from(expandedPaths), entry.path]);
if (!directories.has(entry.path)) {
setExpandedPathsForWorkspace(workspaceStateKey, (currentPaths) =>
setExpandedDirectoryPath({
currentExpandedPaths: currentPaths,
directoryPath: entry.path,
expanded: !isExpanded,
}),
);
if (!isExpanded && !directories.has(entry.path)) {
void requestDirectoryListing(entry.path, {
recordHistory: false,
setCurrentPath: false,

View File

@@ -5,6 +5,7 @@ import {
flattenExplorerTree,
reconcileRestoredExpandedPaths,
restoreExpandedDirectories,
setExpandedDirectoryPath,
showHiddenFilesAndRestoreExpandedDirectories,
} from "./tree";
@@ -153,6 +154,22 @@ describe("file explorer tree", () => {
expect(paths).toEqual([".", "manual"]);
});
it("applies a directory click to the latest restored expansion paths", () => {
const expanded = setExpandedDirectoryPath({
currentExpandedPaths: [".", "restored"],
directoryPath: "manual",
expanded: true,
});
const collapsed = setExpandedDirectoryPath({
currentExpandedPaths: expanded,
directoryPath: "manual",
expanded: false,
});
expect(expanded).toEqual([".", "restored", "manual"]);
expect(collapsed).toEqual([".", "restored"]);
});
it("shows hidden files before waiting for expanded directories to restore", async () => {
const rootDirectory = {
path: ".",

View File

@@ -36,6 +36,12 @@ interface ReconcileRestoredExpandedPathsInput {
restoredExpandedPaths: string[];
}
interface SetExpandedDirectoryPathInput {
currentExpandedPaths: readonly string[];
directoryPath: string;
expanded: boolean;
}
export function flattenExplorerTree({
directories,
expandedPaths,
@@ -153,6 +159,20 @@ export function reconcileRestoredExpandedPaths({
return Array.from(reconciledPaths);
}
export function setExpandedDirectoryPath({
currentExpandedPaths,
directoryPath,
expanded,
}: SetExpandedDirectoryPathInput): string[] {
const nextPaths = new Set(currentExpandedPaths);
if (expanded) {
nextPaths.add(directoryPath);
} else {
nextPaths.delete(directoryPath);
}
return Array.from(nextPaths);
}
function rowsForDirectory(
directory: ExplorerDirectory,
depth: number,