feat(web): add expand/collapse all control to the files surface (#8889)

This commit is contained in:
Utkarsh Patil authored and GitHub committed 2026-08-31 16:50:02 -07:00
1 parent c50b0b4ef8
commit 17f00f6024
3 files changed
+154 -2

No files matched your search

@@ -3,9 +3,9 @@ import type {
ContextMenuOpenContext as TreeContextMenuOpenContext,
} from "@pierre/trees";
import type { EnvironmentId, ProjectEntry } from "@t3tools/contracts";
import { FileTree, useFileTree, useFileTreeSearch } from "@pierre/trees/react";
import { FileTree, useFileTree, useFileTreeSearch, useFileTreeSelector } from "@pierre/trees/react";
import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
import { RotateCw } from "lucide-react";
import { ChevronsDownUpIcon, ChevronsUpDownIcon, RotateCw } from "lucide-react";
import { useEffect, useMemo, useRef } from "react";
import { Button } from "~/components/ui/button";
@@ -21,6 +21,7 @@ import { readLocalApi } from "~/localApi";
import { T3_PIERRE_ICONS } from "~/pierre-icons";
import { createFileTreeDragMentionController } from "./fileTreeDragMention";
import { areAllDirectoriesExpanded, setAllDirectoriesExpanded } from "./fileTreeExpansion";
import { useProjectEntriesQuery } from "./projectFilesQueryState";
interface FileBrowserPanelProps {
@@ -121,6 +122,10 @@ export default function FileBrowserPanel({
);
const entryKindsRef = useRef<ReadonlyMap<string, ProjectEntry["kind"]>>(entryKinds);
const treePaths = useMemo(() => entries.map(treePath), [entries]);
const directoryPaths = useMemo(
() => entries.filter((entry) => entry.kind === "directory").map(treePath),
[entries],
);
const previousTreePathsRef = useRef<readonly string[]>([]);
const syncingSelectionRef = useRef(false);
const treeSelectionPathRef = useRef<string | null>(null);
@@ -252,6 +257,12 @@ export default function FileBrowserPanel({
unsafeCSS: TREE_UNSAFE_CSS,
});
const search = useFileTreeSearch(model);
const allDirectoriesExpanded = useFileTreeSelector(model, (currentModel) =>
areAllDirectoriesExpanded(currentModel, directoryPaths),
);
const toggleAllDirectories = () => {
setAllDirectoriesExpanded(model, directoryPaths, !allDirectoriesExpanded);
};
const handleSearchValueChange = (value: string) => {
if (value.trim().length === 0) {
search.close();
@@ -376,6 +387,32 @@ export default function FileBrowserPanel({
onValueChange={handleSearchValueChange}
onClose={search.close}
/>
{directoryPaths.length > 0 ? (
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
size="icon-xs"
variant="ghost"
aria-label={
allDirectoriesExpanded ? "Collapse all folders" : "Expand all folders"
}
onClick={toggleAllDirectories}
/>
}
>
{allDirectoriesExpanded ? (
<ChevronsDownUpIcon className="size-3.5" />
) : (
<ChevronsUpDownIcon className="size-3.5" />
)}
</TooltipTrigger>
<TooltipPopup>
{allDirectoriesExpanded ? "Collapse all folders" : "Expand all folders"}
</TooltipPopup>
</Tooltip>
) : null}
</div>
{entriesQuery.error && entriesQuery.data === null ? (
<div className="p-4 text-xs leading-relaxed text-destructive">{entriesQuery.error}</div>
@@ -0,0 +1,60 @@
import { describe, expect, it, vi } from "@effect/vitest";
import { areAllDirectoriesExpanded, setAllDirectoriesExpanded } from "./fileTreeExpansion";
type FakeDirectoryItem = {
isDirectory: () => true;
isExpanded: () => boolean;
expand: () => void;
collapse: () => void;
};
function makeModel(expanded: Record<string, boolean>) {
const items = new Map<string, FakeDirectoryItem>();
return {
getItem: (path: string) => {
const existing = items.get(path);
if (existing !== undefined) return existing;
const item: FakeDirectoryItem = {
isDirectory: () => true,
isExpanded: () => expanded[path] ?? false,
expand: () => {
expanded[path] = true;
},
collapse: () => {
expanded[path] = false;
},
};
items.set(path, item);
return item;
},
};
}
describe("file tree expansion", () => {
it("requires at least one directory and detects whether all are expanded", () => {
const model = makeModel({ "src/": true, "test/": true });
expect(areAllDirectoriesExpanded(model, [])).toBe(false);
expect(areAllDirectoriesExpanded(model, ["src/", "test/"])).toBe(true);
expect(
areAllDirectoriesExpanded(makeModel({ "src/": true, "test/": false }), ["src/", "test/"]),
).toBe(false);
});
it("expands and collapses every directory", () => {
const expanded = { "src/": true, "test/": false };
const model = makeModel(expanded);
setAllDirectoriesExpanded(model, ["src/", "test/"], true);
expect(expanded).toEqual({ "src/": true, "test/": true });
setAllDirectoriesExpanded(model, ["src/", "test/"], false);
expect(expanded).toEqual({ "src/": false, "test/": false });
});
it("skips directories already at the requested state", () => {
const model = makeModel({ "src/": true });
const item = model.getItem("src/");
const collapse = vi.spyOn(item, "collapse");
setAllDirectoriesExpanded(model, ["src/"], true);
expect(collapse).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,55 @@
export interface FileTreeExpansionModel {
getItem(path: string): unknown;
}
type DirectoryHandle = {
isDirectory(): boolean;
isExpanded(): boolean;
expand(): void;
collapse(): void;
};
function asDirectoryHandle(item: unknown): DirectoryHandle | null {
if (
typeof item !== "object" ||
item === null ||
!("isDirectory" in item) ||
typeof item.isDirectory !== "function" ||
!item.isDirectory() ||
!("isExpanded" in item) ||
typeof item.isExpanded !== "function" ||
!("expand" in item) ||
typeof item.expand !== "function" ||
!("collapse" in item) ||
typeof item.collapse !== "function"
) {
return null;
}
return item as DirectoryHandle;
}
export function areAllDirectoriesExpanded(
model: FileTreeExpansionModel,
directoryPaths: readonly string[],
): boolean {
return (
directoryPaths.length > 0 &&
directoryPaths.every((path) => {
const item = asDirectoryHandle(model.getItem(path));
return item !== null && item.isExpanded();
})
);
}
export function setAllDirectoriesExpanded(
model: FileTreeExpansionModel,
directoryPaths: readonly string[],
expanded: boolean,
): void {
for (const path of directoryPaths) {
const item = asDirectoryHandle(model.getItem(path));
if (item === null || item.isExpanded() === expanded) continue;
if (expanded) item.expand();
else item.collapse();
}
}