The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { RefreshIcon } from "~/components/ui/refresh-icon"; |
| 2 | import { Spinner } from "~/components/ui/spinner"; |
| 3 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 4 | import { ArrowLeftIcon, ChevronRightIcon } from "lucide-react"; |
| 5 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 6 | import { useState } from "react"; |
| 7 | |
| 8 | import { PierreEntryIcon } from "~/components/chat/PierreEntryIcon"; |
| 9 | import { |
| 10 | Menu, |
| 11 | MenuGroup, |
| 12 | MenuItem, |
| 13 | MenuPopup, |
| 14 | MenuRadioGroup, |
| 15 | MenuRadioItem, |
| 16 | MenuSeparator, |
| 17 | MenuTrigger, |
| 18 | } from "~/components/ui/menu"; |
| 19 | import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip"; |
| 20 | import { useTheme } from "~/hooks/useTheme"; |
| 21 | import { cn } from "~/lib/utils"; |
| 22 | import { isHostPath } from "@t3tools/shared/path"; |
| 23 | |
| 24 | import { |
| 25 | breadcrumbPathOf, |
| 26 | type FileBreadcrumb, |
| 27 | fileBreadcrumbChildren, |
| 28 | fileBreadcrumbParent, |
| 29 | fileBreadcrumbs, |
| 30 | isListableHostFolder, |
| 31 | } from "./filePath"; |
| 32 | import { useProjectEntriesQuery } from "./projectFilesQueryState"; |
| 33 | |
| 34 | interface 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; |
| 46 | } |
| 47 | |
| 48 | function pathLabel(path: string, projectName: string): string { |
| 49 | return path.slice(path.lastIndexOf("/") + 1) || projectName; |
| 50 | } |
| 51 | |
| 52 | function 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 | ); |
| 74 | } |
| 75 | |
| 76 | function 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 messages = useMessages(); |
| 89 | const entriesQuery = useProjectEntriesQuery( |
| 90 | props.environmentId, |
| 91 | props.cwd, |
| 92 | props.directoryPath, |
| 93 | props.workspaceMutationId, |
| 94 | messages, |
| 95 | ); |
| 96 | const gt = useGT(); |
| 97 | const { resolvedTheme } = useTheme(); |
| 98 | const entries = entriesQuery.data?.entries ?? []; |
| 99 | const entriesTruncated = entriesQuery.data?.truncated ?? false; |
| 100 | const children = fileBreadcrumbChildren(entries, props.directoryPath); |
| 101 | const directoryAvailable = entriesQuery.data !== null; |
| 102 | const parentPath = fileBreadcrumbParent(props.directoryPath); |
| 103 | const canGoBack = |
| 104 | props.directoryPath !== props.rootPath && |
| 105 | parentPath !== null && |
| 106 | (props.rootPath === "" || |
| 107 | parentPath === props.rootPath || |
| 108 | parentPath.startsWith(`${props.rootPath}/`)); |
| 109 | |
| 110 | return ( |
| 111 | <MenuPopup |
| 112 | align="start" |
| 113 | side="bottom" |
| 114 | onKeyDown={(event) => { |
| 115 | if (event.key !== "ArrowLeft" || !canGoBack || parentPath === null) return; |
| 116 | event.preventDefault(); |
| 117 | event.stopPropagation(); |
| 118 | props.onDirectoryChange(parentPath); |
| 119 | }} |
| 120 | > |
| 121 | {canGoBack && parentPath !== null ? ( |
| 122 | <> |
| 123 | <MenuItem closeOnClick={false} onClick={() => props.onDirectoryChange(parentPath)}> |
| 124 | <ArrowLeftIcon /> |
| 125 | <span className="truncate"> |
| 126 | <T> |
| 127 | Back to <Var>{pathLabel(parentPath, props.projectName)}</Var> |
| 128 | </T> |
| 129 | </span> |
| 130 | </MenuItem> |
| 131 | <MenuSeparator /> |
| 132 | </> |
| 133 | ) : null} |
| 134 | <MenuGroup key={props.directoryPath}> |
| 135 | {entriesQuery.isPending && entriesQuery.data === null ? ( |
| 136 | <MenuItem disabled> |
| 137 | <Spinner /> |
| 138 | <T>Loading folder…</T> |
| 139 | </MenuItem> |
| 140 | ) : entriesQuery.error && entriesQuery.data === null ? ( |
| 141 | <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}> |
| 142 | <RefreshIcon refreshing={entriesQuery.isPending} /> |
| 143 | <span className="min-w-0 flex-1 truncate"> |
| 144 | <T>Retry loading folder</T> |
| 145 | </span> |
| 146 | </MenuItem> |
| 147 | ) : !directoryAvailable && !entriesTruncated ? ( |
| 148 | <MenuItem disabled> |
| 149 | <T>This folder is no longer available.</T> |
| 150 | </MenuItem> |
| 151 | ) : children.length === 0 ? ( |
| 152 | <MenuItem disabled> |
| 153 | {entriesTruncated |
| 154 | ? gt("No entries from this folder are available in the partial workspace index.") |
| 155 | : gt("This folder is empty.")} |
| 156 | </MenuItem> |
| 157 | ) : ( |
| 158 | <MenuRadioGroup |
| 159 | value={props.currentFilePath} |
| 160 | onValueChange={(path) => { |
| 161 | props.onOpenChange(false); |
| 162 | props.onOpenFile(path); |
| 163 | }} |
| 164 | > |
| 165 | {children.map((entry) => { |
| 166 | const isCurrentFile = entry.kind === "file" && entry.path === props.currentFilePath; |
| 167 | const row = ( |
| 168 | <> |
| 169 | <PierreEntryIcon pathValue={entry.path} kind={entry.kind} theme={resolvedTheme} /> |
| 170 | <Tooltip> |
| 171 | <TooltipTrigger |
| 172 | render={ |
| 173 | <span |
| 174 | className={cn( |
| 175 | "min-w-0 flex-1 truncate", |
| 176 | entry.ignored && "text-muted-foreground", |
| 177 | )} |
| 178 | /> |
| 179 | } |
| 180 | > |
| 181 | {entry.label} |
| 182 | </TooltipTrigger> |
| 183 | <TooltipPopup side="right">{entry.path}</TooltipPopup> |
| 184 | </Tooltip> |
| 185 | </> |
| 186 | ); |
| 187 | return entry.kind === "directory" ? ( |
| 188 | <MenuItem |
| 189 | key={entry.path} |
| 190 | closeOnClick={false} |
| 191 | onClick={() => props.onDirectoryChange(entry.path)} |
| 192 | > |
| 193 | {row} |
| 194 | <ChevronRightIcon /> |
| 195 | </MenuItem> |
| 196 | ) : ( |
| 197 | <MenuRadioItem |
| 198 | key={entry.path} |
| 199 | value={entry.path} |
| 200 | closeOnClick |
| 201 | aria-current={isCurrentFile ? "page" : undefined} |
| 202 | > |
| 203 | <span className="flex min-w-0 items-center gap-2">{row}</span> |
| 204 | </MenuRadioItem> |
| 205 | ); |
| 206 | })} |
| 207 | </MenuRadioGroup> |
| 208 | )} |
| 209 | </MenuGroup> |
| 210 | {entriesQuery.error && entriesQuery.data !== null ? ( |
| 211 | <> |
| 212 | <MenuSeparator /> |
| 213 | <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}> |
| 214 | <RefreshIcon refreshing={entriesQuery.isPending} /> |
| 215 | <T>Refresh failed. Retry</T> |
| 216 | </MenuItem> |
| 217 | </> |
| 218 | ) : null} |
| 219 | {entriesTruncated ? ( |
| 220 | <> |
| 221 | <MenuSeparator /> |
| 222 | <MenuItem disabled> |
| 223 | <T>Some workspace entries are not shown.</T> |
| 224 | </MenuItem> |
| 225 | </> |
| 226 | ) : null} |
| 227 | </MenuPopup> |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | function FolderContentsMenu(props: FileBreadcrumbsProps & { readonly crumb: FileBreadcrumb }) { |
| 232 | const gt = useGT(); |
| 233 | const [open, setOpen] = useState(false); |
| 234 | const [directoryPath, setDirectoryPath] = useState(props.crumb.path); |
| 235 | const [shownFor, setShownFor] = useState({ |
| 236 | crumbPath: props.crumb.path, |
| 237 | relativePath: props.relativePath, |
| 238 | }); |
| 239 | if (shownFor.crumbPath !== props.crumb.path || shownFor.relativePath !== props.relativePath) { |
| 240 | setShownFor({ crumbPath: props.crumb.path, relativePath: props.relativePath }); |
| 241 | setOpen(false); |
| 242 | setDirectoryPath(props.crumb.path); |
| 243 | } |
| 244 | |
| 245 | const handleOpenChange = (nextOpen: boolean) => { |
| 246 | setOpen(nextOpen); |
| 247 | if (nextOpen) setDirectoryPath(props.crumb.path); |
| 248 | }; |
| 249 | |
| 250 | return ( |
| 251 | <Menu open={open} onOpenChange={handleOpenChange}> |
| 252 | <MenuTrigger |
| 253 | render={ |
| 254 | <button |
| 255 | type="button" |
| 256 | aria-label={gt("Browse {folder}", { folder: props.crumb.label })} |
| 257 | 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" |
| 258 | /> |
| 259 | } |
| 260 | > |
| 261 | <ChevronRightIcon className="size-3.5" /> |
| 262 | </MenuTrigger> |
| 263 | {open ? ( |
| 264 | <BreadcrumbMenuContent |
| 265 | cwd={props.cwd} |
| 266 | currentFilePath={props.relativePath} |
| 267 | directoryPath={directoryPath} |
| 268 | environmentId={props.environmentId} |
| 269 | onDirectoryChange={setDirectoryPath} |
| 270 | onOpenChange={handleOpenChange} |
| 271 | onOpenFile={props.onOpenFile} |
| 272 | projectName={props.projectName} |
| 273 | rootPath={props.crumb.path} |
| 274 | workspaceMutationId={props.workspaceMutationId} |
| 275 | /> |
| 276 | ) : null} |
| 277 | </Menu> |
| 278 | ); |
| 279 | } |
| 280 | |
| 281 | function FolderBreadcrumb(props: { |
| 282 | readonly crumb: FileBreadcrumb; |
| 283 | /** Whether this crumb is below the current folder. */ |
| 284 | readonly descendant: boolean; |
| 285 | readonly onNavigate: (path: string) => void; |
| 286 | readonly projectName: string; |
| 287 | }) { |
| 288 | return ( |
| 289 | <Tooltip> |
| 290 | <TooltipTrigger |
| 291 | render={ |
| 292 | <button |
| 293 | type="button" |
| 294 | onClick={() => props.onNavigate(props.crumb.path)} |
| 295 | className={cn( |
| 296 | "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", |
| 297 | props.descendant ? "text-muted-foreground/70" : "text-muted-foreground", |
| 298 | )} |
| 299 | /> |
| 300 | } |
| 301 | > |
| 302 | <span className="block truncate">{props.crumb.label}</span> |
| 303 | </TooltipTrigger> |
| 304 | <TooltipPopup side="top">{props.crumb.path || props.projectName}</TooltipPopup> |
| 305 | </Tooltip> |
| 306 | ); |
| 307 | } |
| 308 | |
| 309 | /** Host paths outside the workspace omit contents menus; the browser already lists their destination. */ |
| 310 | export function FileBreadcrumbs(props: FileBreadcrumbsProps) { |
| 311 | const hostPath = isHostPath(props.relativePath); |
| 312 | const breadcrumbs = fileBreadcrumbs(props.projectName, props.trail); |
| 313 | const currentPath = breadcrumbPathOf(props.relativePath); |
| 314 | const currentIndex = breadcrumbs.findIndex((crumb) => crumb.path === currentPath); |
| 315 | const gt = useGT(); |
| 316 | |
| 317 | return ( |
| 318 | <nav aria-label={gt("File path")} className="flex h-full"> |
| 319 | <ol className="flex items-center"> |
| 320 | {breadcrumbs.map((crumb, index) => { |
| 321 | const current = index === currentIndex; |
| 322 | return ( |
| 323 | <li |
| 324 | key={crumb.path || "project"} |
| 325 | className="flex min-w-0 shrink-0 items-center" |
| 326 | data-file-crumb |
| 327 | data-current-file-crumb={current} |
| 328 | > |
| 329 | {current ? ( |
| 330 | <span aria-current="page"> |
| 331 | <BreadcrumbLabel |
| 332 | current |
| 333 | label={crumb.label} |
| 334 | pathLabel={crumb.path || props.projectName} |
| 335 | /> |
| 336 | </span> |
| 337 | ) : hostPath && !isListableHostFolder(crumb.path) ? ( |
| 338 | <BreadcrumbLabel label={crumb.label} pathLabel={crumb.path} /> |
| 339 | ) : ( |
| 340 | <FolderBreadcrumb |
| 341 | crumb={crumb} |
| 342 | descendant={index > currentIndex} |
| 343 | onNavigate={props.onNavigateFolder} |
| 344 | projectName={props.projectName} |
| 345 | /> |
| 346 | )} |
| 347 | {index === breadcrumbs.length - 1 ? null : hostPath ? ( |
| 348 | <ChevronRightIcon |
| 349 | aria-hidden |
| 350 | className="mx-1 size-3.5 shrink-0 text-muted-foreground/60" |
| 351 | /> |
| 352 | ) : ( |
| 353 | <FolderContentsMenu {...props} crumb={crumb} /> |
| 354 | )} |
| 355 | </li> |
| 356 | ); |
| 357 | })} |
| 358 | </ol> |
| 359 | </nav> |
| 360 | ); |
| 361 | } |