Open History to see the last commit
1import type { EnvironmentId, ProjectEntry } from "@t3tools/contracts";
2import { FileTree, useFileTree } from "@pierre/trees/react";
3import { RefreshCw, Search } from "lucide-react";
4import { useEffect, useMemo, useRef } from "react";
5
6import { useTheme } from "~/hooks/useTheme";
7import { cn } from "~/lib/utils";
8import { T3_PIERRE_ICONS } from "~/pierre-icons";
9
10import { useProjectEntriesQuery } from "./projectFilesQueryState";
11
12interface FileBrowserPanelProps {
13 environmentId: EnvironmentId;
14 cwd: string;
15 projectName: string;
16 onOpenFile: (relativePath: string) => void;
17}
18
19const TREE_UNSAFE_CSS = `
20 :host {
21 --trees-bg-override: transparent;
22 --trees-selected-bg-override: color-mix(in srgb, currentColor 12%, transparent);
23 --trees-hover-bg-override: color-mix(in srgb, currentColor 7%, transparent);
24 --trees-border-color-override: color-mix(in srgb, currentColor 14%, transparent);
25 --trees-font-family-override: var(--font-sans);
26 --trees-font-size-override: 12px;
27 }
28 button[data-type='item'] { border-radius: 5px; }
29`;
30
31function treePath(entry: ProjectEntry): string {
32 return entry.kind === "directory" ? `${entry.path}/` : entry.path;
33}
34
35export default function FileBrowserPanel({
36 environmentId,
37 cwd,
38 projectName,
39 onOpenFile,
40}: FileBrowserPanelProps) {
41 const { resolvedTheme } = useTheme();
42 const entriesQuery = useProjectEntriesQuery(environmentId, cwd);
43 const entries = entriesQuery.data?.entries ?? [];
44 const entryKinds = useMemo(
45 () => new Map(entries.map((entry) => [entry.path, entry.kind] as const)),
46 [entries],
47 );
48 const entryKindsRef = useRef<ReadonlyMap<string, ProjectEntry["kind"]>>(entryKinds);
49 const treePaths = useMemo(() => entries.map(treePath), [entries]);
50 const previousTreePathsRef = useRef<readonly string[]>([]);
51
52 const { model } = useFileTree({
53 density: "compact",
54 fileTreeSearchMode: "hide-non-matches",
55 flattenEmptyDirectories: true,
56 initialExpansion: 1,
57 icons: T3_PIERRE_ICONS,
58 onSelectionChange: (selectedPaths) => {
59 const selectedPath = selectedPaths.at(-1)?.replace(/\/$/, "");
60 if (selectedPath && entryKindsRef.current.get(selectedPath) === "file") {
61 onOpenFile(selectedPath);
62 }
63 },
64 paths: [],
65 search: true,
66 unsafeCSS: TREE_UNSAFE_CSS,
67 });
68
69 useEffect(() => {
70 if (previousTreePathsRef.current === treePaths) return;
71 entryKindsRef.current = entryKinds;
72 previousTreePathsRef.current = treePaths;
73 model.resetPaths(treePaths);
74 }, [entryKinds, model, treePaths]);
75
76 const fileCount = useMemo(
77 () => entries.reduce((count, entry) => count + (entry.kind === "file" ? 1 : 0), 0),
78 [entries],
79 );
80
81 return (
82 <div
83 className="flex min-h-0 flex-1 flex-col bg-background"
84 data-file-browser-panel={`${environmentId}:${cwd}`}
85 >
86 <div className="flex h-9 shrink-0 items-center gap-2 border-b border-border/60 px-3">
87 <div className="min-w-0 flex-1">
88 <div className="truncate text-xs font-medium text-foreground">{projectName}</div>
89 <div className="truncate text-[10px] leading-none text-muted-foreground">
90 {entriesQuery.isPending && entriesQuery.data === null
91 ? "Indexing…"
92 : `${fileCount.toLocaleString()} files`}
93 {entriesQuery.data?.truncated ? " · partial" : ""}
94 </div>
95 </div>
96 <button
97 type="button"
98 className="rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground"
99 aria-label="Search workspace files"
100 onClick={() => model.openSearch()}
101 >
102 <Search className="size-3.5" />
103 </button>
104 <button
105 type="button"
106 className="rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground"
107 aria-label="Refresh workspace files"
108 onClick={entriesQuery.refresh}
109 >
110 <RefreshCw className={cn("size-3.5", entriesQuery.isPending && "animate-spin")} />
111 </button>
112 </div>
113 {entriesQuery.error && entriesQuery.data === null ? (
114 <div className="p-4 text-xs leading-relaxed text-destructive">{entriesQuery.error}</div>
115 ) : (
116 <FileTree
117 model={model}
118 aria-label={`${projectName} files`}
119 className="min-h-0 flex-1 overflow-hidden"
120 style={{
121 colorScheme: resolvedTheme,
122 ["--trees-fg-override" as string]: "var(--foreground)",
123 }}
124 />
125 )}
126 </div>
127 );
128}