Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FileBreadcrumbs.tsx

355 lines12.5 KB

1import { RefreshIcon } from "~/components/ui/refresh-icon";
2import { Spinner } from "~/components/ui/spinner";
3import type { EnvironmentId } from "@t3tools/contracts";
4import { ArrowLeftIcon, ChevronRightIcon } from "lucide-react";
5import { useEffect, useMemo, useState } from "react";
6​
7import { PierreEntryIcon } from "~/components/chat/PierreEntryIcon";
8import {
9 Menu,
10 MenuGroup,
11 MenuItem,
12 MenuPopup,
13 MenuRadioGroup,
14 MenuRadioItem,
15 MenuSeparator,
16 MenuTrigger,
17} from "~/components/ui/menu";
18import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
19import { useTheme } from "~/hooks/useTheme";
20import { useWorkspaceMutationRefresh } from "~/hooks/useWorkspaceMutationRefresh";
21import { cn } from "~/lib/utils";
22import { isAbsolutePath } from "@t3tools/shared/path";
23​
24import {
25 breadcrumbPathOf,
26 type FileBreadcrumb,
27 fileBreadcrumbChildren,
28 fileBreadcrumbParent,
29 fileBreadcrumbs,
30 isListableHostFolder,
31} from "./filePath";
32import { useProjectEntriesQuery } from "./projectFilesQueryState";
33​
34interface FileBreadcrumbsProps {
35 readonly cwd: string;
36 readonly environmentId: EnvironmentId;
37 /** Moves the browser to a folder crumb; `""` is the workspace root. */
38 readonly onNavigateFolder: (relativePath: string) => void;
39 readonly onOpenFile: (relativePath: string) => void;
40 readonly projectName: string;
41 /** The current path; `""` is the workspace root. */
42 readonly relativePath: string;
43 /** The deepest path to show: `relativePath`, or a path inside it the browser came up from. */
44 readonly trail: string;
45 readonly workspaceMutationId: string | null;
47​
48function pathLabel(path: string, projectName: string): string {
49 return path.slice(path.lastIndexOf("/") + 1) || projectName;
51​
52function BreadcrumbLabel(props: {
53 readonly current?: boolean;
54 readonly label: string;
55 readonly pathLabel: string;
56}) {
57 return (
58 <Tooltip>
59 <TooltipTrigger
60 render={
61 <span
62 className={cn(
63 "block max-w-40 truncate rounded-sm px-0.5",
64 props.current ? "font-medium text-foreground" : "text-muted-foreground",
65 )}
66 />
67 }
68 >
69 {props.label}
70 </TooltipTrigger>
71 <TooltipPopup side="top">{props.pathLabel}</TooltipPopup>
72 </Tooltip>
73 );
75​
76function BreadcrumbMenuContent(props: {
77 readonly cwd: string;
78 readonly currentFilePath: string;
79 readonly directoryPath: string;
80 readonly environmentId: EnvironmentId;
81 readonly onDirectoryChange: (path: string) => void;
82 readonly onOpenChange: (open: boolean) => void;
83 readonly onOpenFile: (path: string) => void;
84 readonly projectName: string;
85 readonly rootPath: string;
86 readonly workspaceMutationId: string | null;
87}) {
88 const entriesQuery = useProjectEntriesQuery(props.environmentId, props.cwd, props.directoryPath);
89 useWorkspaceMutationRefresh({
90 mutationId: props.workspaceMutationId,
91 refresh: entriesQuery.refresh,
92 resourceKey: `files:${props.environmentId}:${props.cwd}`,
93 });
94 const { resolvedTheme } = useTheme();
95 const entries = entriesQuery.data?.entries ?? [];
96 const entriesTruncated = entriesQuery.data?.truncated ?? false;
97 const children = useMemo(
98 () => fileBreadcrumbChildren(entries, props.directoryPath),
99 [entries, props.directoryPath],
100 );
101 const directoryAvailable = entriesQuery.data !== null;
102 const parentPath = fileBreadcrumbParent(props.directoryPath);
103 const canGoBack =
104 props.directoryPath !== props.rootPath &&
105 parentPath !== null &&
106 (props.rootPath === "" ||
107 parentPath === props.rootPath ||
108 parentPath.startsWith(`${props.rootPath}/`));
109​
110 return (
111 <MenuPopup
112 align="start"
113 side="bottom"
114 onKeyDown={(event) => {
115 if (event.key !== "ArrowLeft" || !canGoBack || parentPath === null) return;
116 event.preventDefault();
117 event.stopPropagation();
118 props.onDirectoryChange(parentPath);
119 }}
120 >
121 {canGoBack && parentPath !== null ? (
122 <>
123 <MenuItem closeOnClick={false} onClick={() => props.onDirectoryChange(parentPath)}>
124 <ArrowLeftIcon />
125 <span className="truncate">Back to {pathLabel(parentPath, props.projectName)}</span>
126 </MenuItem>
127 <MenuSeparator />
128 </>
129 ) : null}
130 <MenuGroup key={props.directoryPath}>
131 {entriesQuery.isPending && entriesQuery.data === null ? (
132 <MenuItem disabled>
133 <Spinner />
134 Loading folder…
135 </MenuItem>
136 ) : entriesQuery.error && entriesQuery.data === null ? (
137 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
138 <RefreshIcon refreshing={entriesQuery.isPending} />
139 <span className="min-w-0 flex-1 truncate">Retry loading folder</span>
140 </MenuItem>
141 ) : !directoryAvailable && !entriesTruncated ? (
142 <MenuItem disabled>This folder is no longer available.</MenuItem>
143 ) : children.length === 0 ? (
144 <MenuItem disabled>
145 {entriesTruncated
146 ? "No entries from this folder are available in the partial workspace index."
147 : "This folder is empty."}
148 </MenuItem>
149 ) : (
150 // Files form a radio group keyed by path so the open file is marked as checked;
151 // directories only navigate the menu, so they stay plain items.
152 <MenuRadioGroup
153 value={props.currentFilePath}
154 onValueChange={(path) => {
155 props.onOpenChange(false);
156 props.onOpenFile(path);
157 }}
158 >
159 {children.map((entry) => {
160 const isCurrentFile = entry.kind === "file" && entry.path === props.currentFilePath;
161 const row = (
162 <>
163 <PierreEntryIcon pathValue={entry.path} kind={entry.kind} theme={resolvedTheme} />
164 <Tooltip>
165 <TooltipTrigger
166 render={
167 <span
168 className={cn(
169 "min-w-0 flex-1 truncate",
170 entry.ignored && "text-muted-foreground",
171 )}
172 />
173 }
174 >
175 {entry.label}
176 </TooltipTrigger>
177 <TooltipPopup side="right">{entry.path}</TooltipPopup>
178 </Tooltip>
179 </>
180 );
181 return entry.kind === "directory" ? (
182 <MenuItem
183 key={entry.path}
184 closeOnClick={false}
185 onClick={() => props.onDirectoryChange(entry.path)}
186 >
187 {row}
188 <ChevronRightIcon />
189 </MenuItem>
190 ) : (
191 <MenuRadioItem
192 key={entry.path}
193 value={entry.path}
194 closeOnClick
195 aria-current={isCurrentFile ? "page" : undefined}
196 >
197 <span className="flex min-w-0 items-center gap-2">{row}</span>
198 </MenuRadioItem>
199 );
200 })}
201 </MenuRadioGroup>
202 )}
203 </MenuGroup>
204 {entriesQuery.error && entriesQuery.data !== null ? (
205 <>
206 <MenuSeparator />
207 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
208 <RefreshIcon refreshing={entriesQuery.isPending} />
209 Refresh failed — retry
210 </MenuItem>
211 </>
212 ) : null}
213 {entriesTruncated ? (
214 <>
215 <MenuSeparator />
216 <MenuItem disabled>Some workspace entries are not shown.</MenuItem>
217 </>
218 ) : null}
219 </MenuPopup>
220 );
222​
223/** The chevron after a folder crumb lists that folder's entries. */
224function FolderContentsMenu(props: FileBreadcrumbsProps & { readonly crumb: FileBreadcrumb }) {
225 const [open, setOpen] = useState(false);
226 const [directoryPath, setDirectoryPath] = useState(props.crumb.path);
227​
228 useEffect(() => {
229 setOpen(false);
230 setDirectoryPath(props.crumb.path);
231 }, [props.crumb.path, props.relativePath]);
232​
233 const handleOpenChange = (nextOpen: boolean) => {
234 setOpen(nextOpen);
235 if (nextOpen) setDirectoryPath(props.crumb.path);
236 };
237​
238 return (
239 <Menu open={open} onOpenChange={handleOpenChange}>
240 <MenuTrigger
241 render={
242 <button
243 type="button"
244 aria-label={`Browse ${props.crumb.label}`}
245 className="relative mx-0.5 flex h-5 w-4 shrink-0 cursor-pointer items-center justify-center rounded-sm text-muted-foreground/60 outline-none pointer-coarse:after:absolute pointer-coarse:after:-inset-3 hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring data-popup-open:bg-accent data-popup-open:text-foreground"
246 />
247 }
248 >
249 <ChevronRightIcon className="size-3.5" />
250 </MenuTrigger>
251 {open ? (
252 <BreadcrumbMenuContent
253 cwd={props.cwd}
254 currentFilePath={props.relativePath}
255 directoryPath={directoryPath}
256 environmentId={props.environmentId}
257 onDirectoryChange={setDirectoryPath}
258 onOpenChange={handleOpenChange}
259 onOpenFile={props.onOpenFile}
260 projectName={props.projectName}
261 rootPath={props.crumb.path}
262 workspaceMutationId={props.workspaceMutationId}
263 />
264 ) : null}
265 </Menu>
266 );
268​
269function FolderBreadcrumb(props: {
270 readonly crumb: FileBreadcrumb;
271 /** Below the current folder, kept from where the browser came from. */
272 readonly descendant: boolean;
273 readonly onNavigate: (path: string) => void;
274 readonly projectName: string;
275}) {
276 return (
277 <Tooltip>
278 <TooltipTrigger
279 render={
280 <button
281 type="button"
282 onClick={() => props.onNavigate(props.crumb.path)}
283 className={cn(
284 "relative block max-w-40 cursor-pointer rounded-sm px-0.5 text-left outline-none pointer-coarse:after:absolute pointer-coarse:after:inset-x-0 pointer-coarse:after:-inset-y-3 hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
285 props.descendant ? "text-muted-foreground/70" : "text-muted-foreground",
286 )}
287 />
288 }
289 >
290 <span className="block truncate">{props.crumb.label}</span>
291 </TooltipTrigger>
292 <TooltipPopup side="top">{props.crumb.path || props.projectName}</TooltipPopup>
293 </Tooltip>
294 );
296​
297/**
298 * Each folder crumb moves the browser to that folder, and the chevron after it
299 * lists the folder's entries. A host path outside the workspace has no contents
300 * menu: the browser below already lists the folder it moves to.
301 */
302export function FileBreadcrumbs(props: FileBreadcrumbsProps) {
303 const hostPath = isAbsolutePath(props.relativePath);
304 const breadcrumbs = useMemo(
305 () => fileBreadcrumbs(props.projectName, props.trail),
306 [props.projectName, props.trail],
307 );
308 const currentPath = breadcrumbPathOf(props.relativePath);
309 const currentIndex = breadcrumbs.findIndex((crumb) => crumb.path === currentPath);
310​
311 return (
312 <nav aria-label="File path" className="flex h-full">
313 <ol className="flex items-center">
314 {breadcrumbs.map((crumb, index) => {
315 const current = index === currentIndex;
316 return (
317 <li
318 key={crumb.path || "project"}
319 className="flex min-w-0 shrink-0 items-center"
320 data-file-crumb
321 data-current-file-crumb={current}
322 >
323 {current ? (
324 <span aria-current="page">
325 <BreadcrumbLabel
326 current
327 label={crumb.label}
328 pathLabel={crumb.path || props.projectName}
329 />
330 </span>
331 ) : hostPath && !isListableHostFolder(crumb.path) ? (
332 <BreadcrumbLabel label={crumb.label} pathLabel={crumb.path} />
333 ) : (
334 <FolderBreadcrumb
335 crumb={crumb}
336 descendant={index > currentIndex}
337 onNavigate={props.onNavigateFolder}
338 projectName={props.projectName}
339 />
340 )}
341 {index === breadcrumbs.length - 1 ? null : hostPath ? (
342 <ChevronRightIcon
343 aria-hidden
344 className="mx-1 size-3.5 shrink-0 text-muted-foreground/60"
345 />
346 ) : (
347 <FolderContentsMenu {...props} crumb={crumb} />
348 )}
349 </li>
350 );
351 })}
352 </ol>
353 </nav>
354 );