| 1 | import { workspaceBasename as basename, workspaceParentPath as parentPath } from "../lib/workspacePanelFormat"; |
| 2 | import { Tooltip } from "./Tooltip"; |
| 3 | |
| 4 | export interface WorkspacePathBreadcrumb { |
| 5 | label: string; |
| 6 | full: string; |
| 7 | } |
| 8 | |
| 9 | export function buildWorkspacePathBreadcrumbs(cwd: string | undefined, path: string): WorkspacePathBreadcrumb[] { |
| 10 | if (!path) return []; |
| 11 | const root = basename(cwd ?? ""); |
| 12 | const dirParts = parentPath(path).split("/").filter(Boolean); |
| 13 | const dirCrumbs: WorkspacePathBreadcrumb[] = []; |
| 14 | let currentPath = ""; |
| 15 | for (const part of dirParts) { |
| 16 | currentPath += `${currentPath ? "/" : ""}${part}`; |
| 17 | dirCrumbs.push({ label: part, full: `${cwd ?? ""}/${currentPath}` }); |
| 18 | } |
| 19 | const crumbs = [{ label: root, full: cwd ?? "" }]; |
| 20 | if (dirParts.length > 0 && dirParts[0] !== root) crumbs.push(...dirCrumbs); |
| 21 | return crumbs; |
| 22 | } |
| 23 | |
| 24 | export function WorkspacePathBreadcrumbs({ crumbs }: { crumbs: WorkspacePathBreadcrumb[] }) { |
| 25 | if (crumbs.length === 0) return null; |
| 26 | return ( |
| 27 | <span className="workspace-current-file__path"> |
| 28 | {crumbs.map((crumb, index) => ( |
| 29 | <span key={index} className="workspace-current-file__crumb"> |
| 30 | {index > 0 && <span className="workspace-current-file__crumb-sep" aria-hidden="true">›</span>} |
| 31 | <Tooltip label={crumb.full}> |
| 32 | <span>{crumb.label}</span> |
| 33 | </Tooltip> |
| 34 | </span> |
| 35 | ))} |
| 36 | </span> |
| 37 | ); |
| 38 | } |
| 39 |