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 { useEffect, useMemo, useState } from "react";
6
7import { PierreEntryIcon } from "~/components/chat/PierreEntryIcon";
8import {
9 Menu,
10 MenuGroup,
11 MenuItem,
12 MenuPopup,
13 MenuSeparator,
14 MenuTrigger,
15} from "~/components/ui/menu";
16import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
17import { useTheme } from "~/hooks/useTheme";
18import { useWorkspaceMutationRefresh } from "~/hooks/useWorkspaceMutationRefresh";
19import { cn } from "~/lib/utils";
20import { isAbsolutePath } from "~/terminal-links";
21
22import {
23 type FileBreadcrumb,
24 fileBreadcrumbChildren,
25 fileBreadcrumbParent,
26 fileBreadcrumbs,
27} from "./filePath";
28import { useProjectEntriesQuery } from "./projectFilesQueryState";
29
30interface FileBreadcrumbsProps {
31 readonly cwd: string;
32 readonly environmentId: EnvironmentId;
33 readonly onOpenFile: (relativePath: string) => void;
34 readonly projectName: string;
35 readonly relativePath: string;
36 readonly workspaceMutationId: string | null;
37}
38
39function pathLabel(path: string, projectName: string): string {
40 return path.slice(path.lastIndexOf("/") + 1) || projectName;
41}
42
43function BreadcrumbLabel(props: {
44 readonly current?: boolean;
45 readonly label: string;
46 readonly pathLabel: string;
47}) {
48 return (
49 <Tooltip>
50 <TooltipTrigger
51 render={
52 <span
53 className={cn(
54 "block max-w-40 truncate rounded-sm px-0.5",
55 props.current ? "font-medium text-foreground" : "text-muted-foreground",
56 )}
57 />
58 }
59 >
60 {props.label}
61 </TooltipTrigger>
62 <TooltipPopup side="top" className="max-w-80">
63 {props.pathLabel}
64 </TooltipPopup>
65 </Tooltip>
66 );
67}
68
69function BreadcrumbMenuContent(props: {
70 readonly cwd: string;
71 readonly currentFilePath: string;
72 readonly directoryPath: string;
73 readonly environmentId: EnvironmentId;
74 readonly onDirectoryChange: (path: string) => void;
75 readonly onOpenChange: (open: boolean) => void;
76 readonly onOpenFile: (path: string) => void;
77 readonly projectName: string;
78 readonly rootPath: string;
79 readonly workspaceMutationId: string | null;
80}) {
81 const entriesQuery = useProjectEntriesQuery(props.environmentId, props.cwd, props.directoryPath);
82 useWorkspaceMutationRefresh({
83 mutationId: props.workspaceMutationId,
84 refresh: entriesQuery.refresh,
85 resourceKey: `files:${props.environmentId}:${props.cwd}`,
86 });
87 const { resolvedTheme } = useTheme();
88 const entries = entriesQuery.data?.entries ?? [];
89 const entriesTruncated = entriesQuery.data?.truncated ?? false;
90 const children = useMemo(
91 () => fileBreadcrumbChildren(entries, props.directoryPath),
92 [entries, props.directoryPath],
93 );
94 const directoryAvailable = entriesQuery.data !== null;
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 <Spinner />
128 Loading folder…
129 </MenuItem>
130 ) : entriesQuery.error && entriesQuery.data === null ? (
131 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
132 <RefreshIcon refreshing={entriesQuery.isPending} />
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(
152 isCurrentFile && "bg-foreground/[0.08]",
153 entry.ignored && "text-muted-foreground",
154 )}
155 onClick={() => {
156 if (entry.kind === "directory") {
157 props.onDirectoryChange(entry.path);
158 return;
159 }
160 props.onOpenChange(false);
161 props.onOpenFile(entry.path);
162 }}
163 >
164 <PierreEntryIcon pathValue={entry.path} kind={entry.kind} theme={resolvedTheme} />
165 <Tooltip>
166 <TooltipTrigger render={<span className="min-w-0 flex-1 truncate" />}>
167 {entry.label}
168 </TooltipTrigger>
169 <TooltipPopup side="right" className="max-w-80">
170 {entry.path}
171 </TooltipPopup>
172 </Tooltip>
173 {entry.kind === "directory" ? <ChevronRightIcon /> : null}
174 </MenuItem>
175 );
176 })
177 )}
178 </MenuGroup>
179 {entriesQuery.error && entriesQuery.data !== null ? (
180 <>
181 <MenuSeparator />
182 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
183 <RefreshIcon refreshing={entriesQuery.isPending} />
184 Refresh failed — retry
185 </MenuItem>
186 </>
187 ) : null}
188 {entriesTruncated ? (
189 <>
190 <MenuSeparator />
191 <MenuItem disabled>Some workspace entries are not shown.</MenuItem>
192 </>
193 ) : null}
194 </MenuPopup>
195 );
196}
197
198function DirectoryBreadcrumb(props: FileBreadcrumbsProps & { readonly crumb: FileBreadcrumb }) {
199 const [open, setOpen] = useState(false);
200 const [directoryPath, setDirectoryPath] = useState(props.crumb.path);
201
202 useEffect(() => {
203 setOpen(false);
204 setDirectoryPath(props.crumb.path);
205 }, [props.crumb.path, props.relativePath]);
206
207 const handleOpenChange = (nextOpen: boolean) => {
208 setOpen(nextOpen);
209 if (nextOpen) setDirectoryPath(props.crumb.path);
210 };
211
212 return (
213 <Menu open={open} onOpenChange={handleOpenChange}>
214 <Tooltip>
215 <TooltipTrigger
216 render={
217 <MenuTrigger
218 render={
219 <button
220 type="button"
221 aria-label={`Browse ${props.crumb.label}`}
222 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"
223 />
224 }
225 />
226 }
227 >
228 <span className="block truncate">{props.crumb.label}</span>
229 </TooltipTrigger>
230 <TooltipPopup side="top" className="max-w-80">
231 {props.crumb.path || props.projectName}
232 </TooltipPopup>
233 </Tooltip>
234 {open ? (
235 <BreadcrumbMenuContent
236 cwd={props.cwd}
237 currentFilePath={props.relativePath}
238 directoryPath={directoryPath}
239 environmentId={props.environmentId}
240 onDirectoryChange={setDirectoryPath}
241 onOpenChange={handleOpenChange}
242 onOpenFile={props.onOpenFile}
243 projectName={props.projectName}
244 rootPath={props.crumb.path}
245 workspaceMutationId={props.workspaceMutationId}
246 />
247 ) : null}
248 </Menu>
249 );
250}
251
252export function FileBreadcrumbs(props: FileBreadcrumbsProps) {
253 const hostPath = isAbsolutePath(props.relativePath);
254 const breadcrumbs = useMemo(
255 () => fileBreadcrumbs(props.projectName, props.relativePath),
256 [props.projectName, props.relativePath],
257 );
258
259 return breadcrumbs.map((crumb, index) => (
260 <div
261 key={crumb.path || "project"}
262 className="flex min-w-0 shrink-0 items-center"
263 data-current-file-crumb={crumb.kind === "file"}
264 >
265 {index > 0 ? (
266 <ChevronRightIcon className="mx-1 size-3.5 shrink-0 text-muted-foreground/60" />
267 ) : null}
268 {crumb.kind === "file" ? (
269 <span aria-current="page">
270 <BreadcrumbLabel current label={crumb.label} pathLabel={crumb.path} />
271 </span>
272 ) : hostPath ? (
273 <BreadcrumbLabel label={crumb.label} pathLabel={crumb.path} />
274 ) : (
275 <DirectoryBreadcrumb {...props} crumb={crumb} />
276 )}
277 </div>
278 ));
279}