Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions apps/web/src/components/files/FileBrowserPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type {
import type { EnvironmentId, ProjectEntry } from "@t3tools/contracts";
import { FileTree, useFileTree, useFileTreeSearch } from "@pierre/trees/react";
import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
import { RotateCw } from "lucide-react";
import { useEffect, useMemo, useRef } from "react";
import { ChevronsDownUpIcon, ChevronsUpDownIcon, RotateCw } from "lucide-react";
import { useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from "react";

import { Button } from "~/components/ui/button";
import { InputGroup, InputGroupInput } from "~/components/ui/input-group";
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -252,6 +257,18 @@ export default function FileBrowserPanel({
unsafeCSS: TREE_UNSAFE_CSS,
});
const search = useFileTreeSearch(model);
const subscribeToTree = useCallback(
(onStoreChange: () => void) => model.subscribe(onStoreChange),
[model],
);
const allDirectoriesExpanded = useSyncExternalStore(
subscribeToTree,
() => areAllDirectoriesExpanded(model, directoryPaths),
() => false,
);
const toggleAllDirectories = () => {
setAllDirectoriesExpanded(model, directoryPaths, !allDirectoriesExpanded);
};
const handleSearchValueChange = (value: string) => {
if (value.trim().length === 0) {
search.close();
Expand Down Expand Up @@ -376,6 +393,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>
Expand Down
46 changes: 46 additions & 0 deletions apps/web/src/components/files/fileTreeExpansion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it, vi } from "vitest";

import { areAllDirectoriesExpanded, setAllDirectoriesExpanded } from "./fileTreeExpansion";

function makeModel(expanded: Record<string, boolean>) {
return {
getItem: (path: string) => ({
isDirectory: () => true as const,
isExpanded: () => expanded[path] ?? false,
expand: () => {
expanded[path] = true;
},
collapse: () => {
expanded[path] = false;
},
}),
};
}

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();
});
});
55 changes: 55 additions & 0 deletions apps/web/src/components/files/fileTreeExpansion.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading