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

FileBreadcrumbs.tsx

276 lines9.5 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import { ArrowLeftIcon, ChevronRightIcon, LoaderCircleIcon, RotateCwIcon } from "lucide-react";
3import { useEffect, useMemo, useState } from "react";
4​
5import { PierreEntryIcon } from "~/components/chat/PierreEntryIcon";
6import {
7 Menu,
8 MenuGroup,
9 MenuItem,
10 MenuPopup,
11 MenuSeparator,
12 MenuTrigger,
13} from "~/components/ui/menu";
14import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
15import { useTheme } from "~/hooks/useTheme";
16import { useWorkspaceMutationRefresh } from "~/hooks/useWorkspaceMutationRefresh";
17import { cn } from "~/lib/utils";
18import { isAbsolutePath } from "~/terminal-links";
19​
20import {
21 type FileBreadcrumb,
22 fileBreadcrumbChildren,
23 fileBreadcrumbParent,
24 fileBreadcrumbs,
25} from "./filePath";
26import { useProjectEntriesQuery } from "./projectFilesQueryState";
27​
28interface FileBreadcrumbsProps {
29 readonly cwd: string;
30 readonly environmentId: EnvironmentId;
31 readonly onOpenFile: (relativePath: string) => void;
32 readonly projectName: string;
33 readonly relativePath: string;
34 readonly workspaceMutationId: string | null;
36​
37function pathLabel(path: string, projectName: string): string {
38 return path.slice(path.lastIndexOf("/") + 1) || projectName;
40​
41function BreadcrumbLabel(props: {
42 readonly current?: boolean;
43 readonly label: string;
44 readonly pathLabel: string;
45}) {
46 return (
47 <Tooltip>
48 <TooltipTrigger
49 render={
50 <span
51 className={cn(
52 "block max-w-40 truncate rounded-sm px-0.5",
53 props.current ? "font-medium text-foreground" : "text-muted-foreground",
54 )}
55 />
56 }
57 >
58 {props.label}
59 </TooltipTrigger>
60 <TooltipPopup side="top" className="max-w-80">
61 {props.pathLabel}
62 </TooltipPopup>
63 </Tooltip>
64 );
66​
67function BreadcrumbMenuContent(props: {
68 readonly cwd: string;
69 readonly currentFilePath: string;
70 readonly directoryPath: string;
71 readonly environmentId: EnvironmentId;
72 readonly onDirectoryChange: (path: string) => void;
73 readonly onOpenChange: (open: boolean) => void;
74 readonly onOpenFile: (path: string) => void;
75 readonly projectName: string;
76 readonly rootPath: string;
77 readonly workspaceMutationId: string | null;
78}) {
79 const entriesQuery = useProjectEntriesQuery(props.environmentId, props.cwd);
80 useWorkspaceMutationRefresh({
81 mutationId: props.workspaceMutationId,
82 refresh: entriesQuery.refresh,
83 resourceKey: `files:${props.environmentId}:${props.cwd}`,
84 });
85 const { resolvedTheme } = useTheme();
86 const entries = entriesQuery.data?.entries ?? [];
87 const entriesTruncated = entriesQuery.data?.truncated ?? false;
88 const children = useMemo(
89 () => fileBreadcrumbChildren(entries, props.directoryPath),
90 [entries, props.directoryPath],
91 );
92 const directoryAvailable =
93 props.directoryPath === "" ||
94 entries.some((entry) => entry.kind === "directory" && entry.path === props.directoryPath);
95 const parentPath = fileBreadcrumbParent(props.directoryPath);
96 const canGoBack =
97 props.directoryPath !== props.rootPath &&
98 parentPath !== null &&
99 (props.rootPath === "" ||
100 parentPath === props.rootPath ||
101 parentPath.startsWith(`${props.rootPath}/`));
102​
103 return (
104 <MenuPopup
105 align="start"
106 side="bottom"
107 className="w-max min-w-32 max-w-[min(19rem,var(--available-width))]"
108 onKeyDown={(event) => {
109 if (event.key !== "ArrowLeft" || !canGoBack || parentPath === null) return;
110 event.preventDefault();
111 event.stopPropagation();
112 props.onDirectoryChange(parentPath);
113 }}
114 >
115 {canGoBack && parentPath !== null ? (
116 <>
117 <MenuItem closeOnClick={false} onClick={() => props.onDirectoryChange(parentPath)}>
118 <ArrowLeftIcon />
119 <span className="truncate">Back to {pathLabel(parentPath, props.projectName)}</span>
120 </MenuItem>
121 <MenuSeparator />
122 </>
123 ) : null}
124 <MenuGroup key={props.directoryPath}>
125 {entriesQuery.isPending && entriesQuery.data === null ? (
126 <MenuItem disabled>
127 <LoaderCircleIcon className="animate-spin" />
128 Loading folder…
129 </MenuItem>
130 ) : entriesQuery.error && entriesQuery.data === null ? (
131 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
132 <RotateCwIcon />
133 <span className="min-w-0 flex-1 truncate">Retry loading folder</span>
134 </MenuItem>
135 ) : !directoryAvailable && !entriesTruncated ? (
136 <MenuItem disabled>This folder is no longer available.</MenuItem>
137 ) : children.length === 0 ? (
138 <MenuItem disabled>
139 {entriesTruncated
140 ? "No entries from this folder are available in the partial workspace index."
141 : "This folder is empty."}
142 </MenuItem>
143 ) : (
144 children.map((entry) => {
145 const isCurrentFile = entry.kind === "file" && entry.path === props.currentFilePath;
146 return (
147 <MenuItem
148 key={entry.path}
149 closeOnClick={entry.kind === "file"}
150 aria-current={isCurrentFile ? "page" : undefined}
151 className={cn(isCurrentFile && "bg-foreground/[0.08]")}
152 onClick={() => {
153 if (entry.kind === "directory") {
154 props.onDirectoryChange(entry.path);
155 return;
156 }
157 props.onOpenChange(false);
158 props.onOpenFile(entry.path);
159 }}
160 >
161 <PierreEntryIcon pathValue={entry.path} kind={entry.kind} theme={resolvedTheme} />
162 <Tooltip>
163 <TooltipTrigger render={<span className="min-w-0 flex-1 truncate" />}>
164 {entry.label}
165 </TooltipTrigger>
166 <TooltipPopup side="right" className="max-w-80">
167 {entry.path}
168 </TooltipPopup>
169 </Tooltip>
170 {entry.kind === "directory" ? <ChevronRightIcon /> : null}
171 </MenuItem>
172 );
173 })
174 )}
175 </MenuGroup>
176 {entriesQuery.error && entriesQuery.data !== null ? (
177 <>
178 <MenuSeparator />
179 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
180 <RotateCwIcon />
181 Refresh failed — retry
182 </MenuItem>
183 </>
184 ) : null}
185 {entriesTruncated ? (
186 <>
187 <MenuSeparator />
188 <MenuItem disabled>Some workspace entries are not shown.</MenuItem>
189 </>
190 ) : null}
191 </MenuPopup>
192 );
194​
195function DirectoryBreadcrumb(props: FileBreadcrumbsProps & { readonly crumb: FileBreadcrumb }) {
196 const [open, setOpen] = useState(false);
197 const [directoryPath, setDirectoryPath] = useState(props.crumb.path);
198​
199 useEffect(() => {
200 setOpen(false);
201 setDirectoryPath(props.crumb.path);
202 }, [props.crumb.path, props.relativePath]);
203​
204 const handleOpenChange = (nextOpen: boolean) => {
205 setOpen(nextOpen);
206 if (nextOpen) setDirectoryPath(props.crumb.path);
207 };
208​
209 return (
210 <Menu open={open} onOpenChange={handleOpenChange}>
211 <Tooltip>
212 <TooltipTrigger
213 render={
214 <MenuTrigger
215 render={
216 <button
217 type="button"
218 aria-label={`Browse ${props.crumb.label}`}
219 className="relative block max-w-40 cursor-pointer rounded-sm px-0.5 text-left text-muted-foreground outline-none pointer-coarse:after:-inset-y-3 pointer-coarse:after:absolute pointer-coarse:after:inset-x-0 hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring data-popup-open:bg-accent data-popup-open:text-foreground"
220 />
221 }
222 />
223 }
224 >
225 <span className="block truncate">{props.crumb.label}</span>
226 </TooltipTrigger>
227 <TooltipPopup side="top" className="max-w-80">
228 {props.crumb.path || props.projectName}
229 </TooltipPopup>
230 </Tooltip>
231 {open ? (
232 <BreadcrumbMenuContent
233 cwd={props.cwd}
234 currentFilePath={props.relativePath}
235 directoryPath={directoryPath}
236 environmentId={props.environmentId}
237 onDirectoryChange={setDirectoryPath}
238 onOpenChange={handleOpenChange}
239 onOpenFile={props.onOpenFile}
240 projectName={props.projectName}
241 rootPath={props.crumb.path}
242 workspaceMutationId={props.workspaceMutationId}
243 />
244 ) : null}
245 </Menu>
246 );
248​
249export function FileBreadcrumbs(props: FileBreadcrumbsProps) {
250 const hostPath = isAbsolutePath(props.relativePath);
251 const breadcrumbs = useMemo(
252 () => fileBreadcrumbs(props.projectName, props.relativePath),
253 [props.projectName, props.relativePath],
254 );
255​
256 return breadcrumbs.map((crumb, index) => (
257 <div
258 key={crumb.path || "project"}
259 className="flex min-w-0 shrink-0 items-center"
260 data-current-file-crumb={crumb.kind === "file"}
261 >
262 {index > 0 ? (
263 <ChevronRightIcon className="mx-1 size-3.5 shrink-0 text-muted-foreground/60" />
264 ) : null}
265 {crumb.kind === "file" ? (
266 <span aria-current="page">
267 <BreadcrumbLabel current label={crumb.label} pathLabel={crumb.path} />
268 </span>
269 ) : hostPath ? (
270 <BreadcrumbLabel label={crumb.label} pathLabel={crumb.path} />
271 ) : (
272 <DirectoryBreadcrumb {...props} crumb={crumb} />
273 )}
274 </div>
275 ));