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);
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 =
95 props.directoryPath === "" ||
96 entries.some((entry) => entry.kind === "directory" && entry.path === props.directoryPath);
97 const parentPath = fileBreadcrumbParent(props.directoryPath);
98 const canGoBack =
99 props.directoryPath !== props.rootPath &&
100 parentPath !== null &&
101 (props.rootPath === "" ||
102 parentPath === props.rootPath ||
103 parentPath.startsWith(`${props.rootPath}/`));
104
105 return (
106 <MenuPopup
107 align="start"
108 side="bottom"
109 className="w-max min-w-32 max-w-[min(19rem,var(--available-width))]"
110 onKeyDown={(event) => {
111 if (event.key !== "ArrowLeft" || !canGoBack || parentPath === null) return;
112 event.preventDefault();
113 event.stopPropagation();
114 props.onDirectoryChange(parentPath);
115 }}
116 >
117 {canGoBack && parentPath !== null ? (
118 <>
119 <MenuItem closeOnClick={false} onClick={() => props.onDirectoryChange(parentPath)}>
120 <ArrowLeftIcon />
121 <span className="truncate">Back to {pathLabel(parentPath, props.projectName)}</span>
122 </MenuItem>
123 <MenuSeparator />
124 </>
125 ) : null}
126 <MenuGroup key={props.directoryPath}>
127 {entriesQuery.isPending && entriesQuery.data === null ? (
128 <MenuItem disabled>
129 <Spinner />
130 Loading folder…
131 </MenuItem>
132 ) : entriesQuery.error && entriesQuery.data === null ? (
133 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
134 <RefreshIcon refreshing={entriesQuery.isPending} />
135 <span className="min-w-0 flex-1 truncate">Retry loading folder</span>
136 </MenuItem>
137 ) : !directoryAvailable && !entriesTruncated ? (
138 <MenuItem disabled>This folder is no longer available.</MenuItem>
139 ) : children.length === 0 ? (
140 <MenuItem disabled>
141 {entriesTruncated
142 ? "No entries from this folder are available in the partial workspace index."
143 : "This folder is empty."}
144 </MenuItem>
145 ) : (
146 children.map((entry) => {
147 const isCurrentFile = entry.kind === "file" && entry.path === props.currentFilePath;
148 return (
149 <MenuItem
150 key={entry.path}
151 closeOnClick={entry.kind === "file"}
152 aria-current={isCurrentFile ? "page" : undefined}
153 className={cn(isCurrentFile && "bg-foreground/[0.08]")}
154 onClick={() => {
155 if (entry.kind === "directory") {
156 props.onDirectoryChange(entry.path);
157 return;
158 }
159 props.onOpenChange(false);
160 props.onOpenFile(entry.path);
161 }}
162 >
163 <PierreEntryIcon pathValue={entry.path} kind={entry.kind} theme={resolvedTheme} />
164 <Tooltip>
165 <TooltipTrigger render={<span className="min-w-0 flex-1 truncate" />}>
166 {entry.label}
167 </TooltipTrigger>
168 <TooltipPopup side="right" className="max-w-80">
169 {entry.path}
170 </TooltipPopup>
171 </Tooltip>
172 {entry.kind === "directory" ? <ChevronRightIcon /> : null}
173 </MenuItem>
174 );
175 })
176 )}
177 </MenuGroup>
178 {entriesQuery.error && entriesQuery.data !== null ? (
179 <>
180 <MenuSeparator />
181 <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
182 <RefreshIcon refreshing={entriesQuery.isPending} />
183 Refresh failed — retry
184 </MenuItem>
185 </>
186 ) : null}
187 {entriesTruncated ? (
188 <>
189 <MenuSeparator />
190 <MenuItem disabled>Some workspace entries are not shown.</MenuItem>
191 </>
192 ) : null}
193 </MenuPopup>
194 );
195}
196
197function DirectoryBreadcrumb(props: FileBreadcrumbsProps & { readonly crumb: FileBreadcrumb }) {
198 const [open, setOpen] = useState(false);
199 const [directoryPath, setDirectoryPath] = useState(props.crumb.path);
200
201 useEffect(() => {
202 setOpen(false);
203 setDirectoryPath(props.crumb.path);
204 }, [props.crumb.path, props.relativePath]);
205
206 const handleOpenChange = (nextOpen: boolean) => {
207 setOpen(nextOpen);
208 if (nextOpen) setDirectoryPath(props.crumb.path);
209 };
210
211 return (
212 <Menu open={open} onOpenChange={handleOpenChange}>
213 <Tooltip>
214 <TooltipTrigger
215 render={
216 <MenuTrigger
217 render={
218 <button
219 type="button"
220 aria-label={`Browse ${props.crumb.label}`}
221 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"
222 />
223 }
224 />
225 }
226 >
227 <span className="block truncate">{props.crumb.label}</span>
228 </TooltipTrigger>
229 <TooltipPopup side="top" className="max-w-80">
230 {props.crumb.path || props.projectName}
231 </TooltipPopup>
232 </Tooltip>
233 {open ? (
234 <BreadcrumbMenuContent
235 cwd={props.cwd}
236 currentFilePath={props.relativePath}
237 directoryPath={directoryPath}
238 environmentId={props.environmentId}
239 onDirectoryChange={setDirectoryPath}
240 onOpenChange={handleOpenChange}
241 onOpenFile={props.onOpenFile}
242 projectName={props.projectName}
243 rootPath={props.crumb.path}
244 workspaceMutationId={props.workspaceMutationId}
245 />
246 ) : null}
247 </Menu>
248 );
249}
250
251export function FileBreadcrumbs(props: FileBreadcrumbsProps) {
252 const hostPath = isAbsolutePath(props.relativePath);
253 const breadcrumbs = useMemo(
254 () => fileBreadcrumbs(props.projectName, props.relativePath),
255 [props.projectName, props.relativePath],
256 );
257
258 return breadcrumbs.map((crumb, index) => (
259 <div
260 key={crumb.path || "project"}
261 className="flex min-w-0 shrink-0 items-center"
262 data-current-file-crumb={crumb.kind === "file"}
263 >
264 {index > 0 ? (
265 <ChevronRightIcon className="mx-1 size-3.5 shrink-0 text-muted-foreground/60" />
266 ) : null}
267 {crumb.kind === "file" ? (
268 <span aria-current="page">
269 <BreadcrumbLabel current label={crumb.label} pathLabel={crumb.path} />
270 </span>
271 ) : hostPath ? (
272 <BreadcrumbLabel label={crumb.label} pathLabel={crumb.path} />
273 ) : (
274 <DirectoryBreadcrumb {...props} crumb={crumb} />
275 )}
276 </div>
277 ));
278}