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

FileBreadcrumbs.tsx

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