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

FileBreadcrumbs.tsx

346 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 { 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 { isHostPath } 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 /** 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 <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 = 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 );
217​
218function FolderContentsMenu(props: FileBreadcrumbsProps & { readonly crumb: FileBreadcrumb }) {
219 const [open, setOpen] = useState(false);
220 const [directoryPath, setDirectoryPath] = useState(props.crumb.path);
221 const [shownFor, setShownFor] = useState({
222 crumbPath: props.crumb.path,
223 relativePath: props.relativePath,
224 });
225 if (shownFor.crumbPath !== props.crumb.path || shownFor.relativePath !== props.relativePath) {
226 setShownFor({ crumbPath: props.crumb.path, relativePath: props.relativePath });
227 setOpen(false);
228 setDirectoryPath(props.crumb.path);
229 }
230​
231 const handleOpenChange = (nextOpen: boolean) => {
232 setOpen(nextOpen);
233 if (nextOpen) setDirectoryPath(props.crumb.path);
234 };
235​
236 return (
237 <Menu open={open} onOpenChange={handleOpenChange}>
238 <MenuTrigger
239 render={
240 <button
241 type="button"
242 aria-label={`Browse ${props.crumb.label}`}
243 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"
244 />
245 }
246 >
247 <ChevronRightIcon className="size-3.5" />
248 </MenuTrigger>
249 {open ? (
250 <BreadcrumbMenuContent
251 cwd={props.cwd}
252 currentFilePath={props.relativePath}
253 directoryPath={directoryPath}
254 environmentId={props.environmentId}
255 onDirectoryChange={setDirectoryPath}
256 onOpenChange={handleOpenChange}
257 onOpenFile={props.onOpenFile}
258 projectName={props.projectName}
259 rootPath={props.crumb.path}
260 workspaceMutationId={props.workspaceMutationId}
261 />
262 ) : null}
263 </Menu>
264 );
266​
267function FolderBreadcrumb(props: {
268 readonly crumb: FileBreadcrumb;
269 /** Whether this crumb is below the current folder. */
270 readonly descendant: boolean;
271 readonly onNavigate: (path: string) => void;
272 readonly projectName: string;
273}) {
274 return (
275 <Tooltip>
276 <TooltipTrigger
277 render={
278 <button
279 type="button"
280 onClick={() => props.onNavigate(props.crumb.path)}
281 className={cn(
282 "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",
283 props.descendant ? "text-muted-foreground/70" : "text-muted-foreground",
284 )}
285 />
286 }
287 >
288 <span className="block truncate">{props.crumb.label}</span>
289 </TooltipTrigger>
290 <TooltipPopup side="top">{props.crumb.path || props.projectName}</TooltipPopup>
291 </Tooltip>
292 );
294​
295/** Host paths outside the workspace omit contents menus; the browser already lists their destination. */
296export function FileBreadcrumbs(props: FileBreadcrumbsProps) {
297 const hostPath = isHostPath(props.relativePath);
298 const breadcrumbs = fileBreadcrumbs(props.projectName, props.trail);
299 const currentPath = breadcrumbPathOf(props.relativePath);
300 const currentIndex = breadcrumbs.findIndex((crumb) => crumb.path === currentPath);
301​
302 return (
303 <nav aria-label="File path" className="flex h-full">
304 <ol className="flex items-center">
305 {breadcrumbs.map((crumb, index) => {
306 const current = index === currentIndex;
307 return (
308 <li
309 key={crumb.path || "project"}
310 className="flex min-w-0 shrink-0 items-center"
311 data-file-crumb
312 data-current-file-crumb={current}
313 >
314 {current ? (
315 <span aria-current="page">
316 <BreadcrumbLabel
317 current
318 label={crumb.label}
319 pathLabel={crumb.path || props.projectName}
320 />
321 </span>
322 ) : hostPath && !isListableHostFolder(crumb.path) ? (
323 <BreadcrumbLabel label={crumb.label} pathLabel={crumb.path} />
324 ) : (
325 <FolderBreadcrumb
326 crumb={crumb}
327 descendant={index > currentIndex}
328 onNavigate={props.onNavigateFolder}
329 projectName={props.projectName}
330 />
331 )}
332 {index === breadcrumbs.length - 1 ? null : hostPath ? (
333 <ChevronRightIcon
334 aria-hidden
335 className="mx-1 size-3.5 shrink-0 text-muted-foreground/60"
336 />
337 ) : (
338 <FolderContentsMenu {...props} crumb={crumb} />
339 )}
340 </li>
341 );
342 })}
343 </ol>
344 </nav>
345 );