Open History to see the last commit
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 { 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;
46}
47
48function pathLabel(path: string, projectName: string): string {
49 return path.slice(path.lastIndexOf("/") + 1) || projectName;
50}
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 );
74}
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 = fileBreadcrumbChildren(entries, props.directoryPath);
98 const directoryAvailable = entriesQuery.data !== null;
99 const parentPath = fileBreadcrumbParent(props.directoryPath);
100 const canGoBack =
101 props.directoryPath !== props.rootPath &&
102 parentPath !== null &&
103 (props.rootPath === "" ||
104 parentPath === props.rootPath ||
105 parentPath.startsWith(`${props.rootPath}/`));
106
107 return (
108 <MenuPopup
109 align="start"
110 side="bottom"
111 onKeyDown={(event) => {
112 if (event.key !== "ArrowLeft" || !canGoBack || parentPath === null) return;
113 event.preventDefault();
114 event.stopPropagation();
115 props.onDirectoryChange(parentPath);
116 }}
117 >
118 {canGoBack && parentPath !== null ? (
119 <>
120 <MenuItem closeOnClick={false} onClick={() => props.onDirectoryChange(parentPath)}>
121 <ArrowLeftIcon />
122 <span className="truncate">Back to {pathLabel(parentPath, props.projectName)}</span>
123 </MenuItem>
124 <MenuSeparator />
125 </>
126 ) : null}
127 <MenuGroup key={props.directoryPath}>
128 {entriesQuery.isPending && entriesQuery.data === null ? (
129 <MenuItem disabled>
130 <Spinner />
131 Loading folder…
132 </MenuItem>
133 ) : entriesQuery.error && entriesQuery.data === null ? (
134 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
135 <RefreshIcon refreshing={entriesQuery.isPending} />
136 <span className="min-w-0 flex-1 truncate">Retry loading folder</span>
137 </MenuItem>
138 ) : !directoryAvailable && !entriesTruncated ? (
139 <MenuItem disabled>This folder is no longer available.</MenuItem>
140 ) : children.length === 0 ? (
141 <MenuItem disabled>
142 {entriesTruncated
143 ? "No entries from this folder are available in the partial workspace index."
144 : "This folder is empty."}
145 </MenuItem>
146 ) : (
147 <MenuRadioGroup
148 value={props.currentFilePath}
149 onValueChange={(path) => {
150 props.onOpenChange(false);
151 props.onOpenFile(path);
152 }}
153 >
154 {children.map((entry) => {
155 const isCurrentFile = entry.kind === "file" && entry.path === props.currentFilePath;
156 const row = (
157 <>
158 <PierreEntryIcon pathValue={entry.path} kind={entry.kind} theme={resolvedTheme} />
159 <Tooltip>
160 <TooltipTrigger
161 render={
162 <span
163 className={cn(
164 "min-w-0 flex-1 truncate",
165 entry.ignored && "text-muted-foreground",
166 )}
167 />
168 }
169 >
170 {entry.label}
171 </TooltipTrigger>
172 <TooltipPopup side="right">{entry.path}</TooltipPopup>
173 </Tooltip>
174 </>
175 );
176 return entry.kind === "directory" ? (
177 <MenuItem
178 key={entry.path}
179 closeOnClick={false}
180 onClick={() => props.onDirectoryChange(entry.path)}
181 >
182 {row}
183 <ChevronRightIcon />
184 </MenuItem>
185 ) : (
186 <MenuRadioItem
187 key={entry.path}
188 value={entry.path}
189 closeOnClick
190 aria-current={isCurrentFile ? "page" : undefined}
191 >
192 <span className="flex min-w-0 items-center gap-2">{row}</span>
193 </MenuRadioItem>
194 );
195 })}
196 </MenuRadioGroup>
197 )}
198 </MenuGroup>
199 {entriesQuery.error && entriesQuery.data !== null ? (
200 <>
201 <MenuSeparator />
202 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
203 <RefreshIcon refreshing={entriesQuery.isPending} />
204 Refresh failed — retry
205 </MenuItem>
206 </>
207 ) : null}
208 {entriesTruncated ? (
209 <>
210 <MenuSeparator />
211 <MenuItem disabled>Some workspace entries are not shown.</MenuItem>
212 </>
213 ) : null}
214 </MenuPopup>
215 );
216}
217
218/** The chevron after a folder crumb lists that folder's entries. */
219function FolderContentsMenu(props: FileBreadcrumbsProps & { readonly crumb: FileBreadcrumb }) {
220 const [open, setOpen] = useState(false);
221 const [directoryPath, setDirectoryPath] = useState(props.crumb.path);
222 // Opening another file or moving the crumb closes the menu and returns it to the crumb's folder.
223 const [shownFor, setShownFor] = useState({
224 crumbPath: props.crumb.path,
225 relativePath: props.relativePath,
226 });
227 if (shownFor.crumbPath !== props.crumb.path || shownFor.relativePath !== props.relativePath) {
228 setShownFor({ crumbPath: props.crumb.path, relativePath: props.relativePath });
229 setOpen(false);
230 setDirectoryPath(props.crumb.path);
231 }
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 );
267}
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 );
295}
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 = fileBreadcrumbs(props.projectName, props.trail);
305 const currentPath = breadcrumbPathOf(props.relativePath);
306 const currentIndex = breadcrumbs.findIndex((crumb) => crumb.path === currentPath);
307
308 return (
309 <nav aria-label="File path" className="flex h-full">
310 <ol className="flex items-center">
311 {breadcrumbs.map((crumb, index) => {
312 const current = index === currentIndex;
313 return (
314 <li
315 key={crumb.path || "project"}
316 className="flex min-w-0 shrink-0 items-center"
317 data-file-crumb
318 data-current-file-crumb={current}
319 >
320 {current ? (
321 <span aria-current="page">
322 <BreadcrumbLabel
323 current
324 label={crumb.label}
325 pathLabel={crumb.path || props.projectName}
326 />
327 </span>
328 ) : hostPath && !isListableHostFolder(crumb.path) ? (
329 <BreadcrumbLabel label={crumb.label} pathLabel={crumb.path} />
330 ) : (
331 <FolderBreadcrumb
332 crumb={crumb}
333 descendant={index > currentIndex}
334 onNavigate={props.onNavigateFolder}
335 projectName={props.projectName}
336 />
337 )}
338 {index === breadcrumbs.length - 1 ? null : hostPath ? (
339 <ChevronRightIcon
340 aria-hidden
341 className="mx-1 size-3.5 shrink-0 text-muted-foreground/60"
342 />
343 ) : (
344 <FolderContentsMenu {...props} crumb={crumb} />
345 )}
346 </li>
347 );
348 })}
349 </ol>
350 </nav>
351 );
352}