Repository navigation
fix(web): browse folders from file breadcrumbs #8910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+431
−40
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0c6e6ac
fix(web): make file breadcrumbs navigable
404khai 2a6592e
fix(web): nest breadcrumb labels in menu groups
404khai a53b1e4
fix(web): browse folders from file breadcrumbs
404khai 38f275b
fix(web): refresh breadcrumb entries after mutations
404khai a24d350
fix(web): handle truncated breadcrumb listings
404khai 748ff5c
Merge branch 'main' into main
404khai 0226d1b
Update apps/web/src/components/files/FileBreadcrumbs.tsx
404khai 59a8b3e
fix(web): preserve breadcrumb touch targets
404khai be3bf15
Merge branch 'main' into main
404khai 8c52942
fix(web): refine breadcrumb menus
maria-rcks e21da11
chore: merge main into breadcrumb navigation
maria-rcks ff41d34
fix(web): keep breadcrumb menus readable
maria-rcks 97b1bb2
fix(web): keep host file breadcrumbs static
maria-rcks d66eb1b
style(web): format breadcrumb imports
maria-rcks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,283 @@ | ||
| import type { EnvironmentId } from "@t3tools/contracts"; | ||
| import { | ||
| ArrowLeftIcon, | ||
| CheckIcon, | ||
| ChevronRightIcon, | ||
| LoaderCircleIcon, | ||
| RotateCwIcon, | ||
| } from "lucide-react"; | ||
| import { useEffect, useMemo, useState } from "react"; | ||
|
|
||
| import { PierreEntryIcon } from "~/components/chat/PierreEntryIcon"; | ||
| import { | ||
| Menu, | ||
| MenuGroup, | ||
| MenuGroupLabel, | ||
| MenuItem, | ||
| MenuPopup, | ||
| MenuSeparator, | ||
| MenuTrigger, | ||
| } from "~/components/ui/menu"; | ||
| import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip"; | ||
| import { useTheme } from "~/hooks/useTheme"; | ||
| import { cn } from "~/lib/utils"; | ||
|
|
||
| import { | ||
| type FileBreadcrumb, | ||
| fileBreadcrumbChildren, | ||
| fileBreadcrumbParent, | ||
| fileBreadcrumbs, | ||
| } from "./filePath"; | ||
| import { useProjectEntriesQuery } from "./projectFilesQueryState"; | ||
|
|
||
| interface FileBreadcrumbsProps { | ||
| readonly cwd: string; | ||
| readonly environmentId: EnvironmentId; | ||
| readonly onOpenFile: (relativePath: string) => void; | ||
| readonly projectName: string; | ||
| readonly relativePath: string; | ||
| } | ||
|
|
||
| function pathLabel(path: string, projectName: string): string { | ||
| return path.slice(path.lastIndexOf("/") + 1) || projectName; | ||
| } | ||
|
|
||
| function BreadcrumbLabel(props: { | ||
| readonly current?: boolean; | ||
| readonly label: string; | ||
| readonly pathLabel: string; | ||
| }) { | ||
| return ( | ||
| <Tooltip> | ||
| <TooltipTrigger | ||
| render={ | ||
| <span | ||
| className={cn( | ||
| "block max-w-40 truncate rounded-sm px-0.5", | ||
| props.current ? "font-medium text-foreground" : "text-muted-foreground", | ||
| )} | ||
| /> | ||
| } | ||
| > | ||
| {props.label} | ||
| </TooltipTrigger> | ||
| <TooltipPopup side="top" className="max-w-80"> | ||
| {props.pathLabel} | ||
| </TooltipPopup> | ||
| </Tooltip> | ||
| ); | ||
| } | ||
|
|
||
| function BreadcrumbMenuContent(props: { | ||
| readonly cwd: string; | ||
| readonly currentFilePath: string; | ||
| readonly directoryPath: string; | ||
| readonly environmentId: EnvironmentId; | ||
| readonly onDirectoryChange: (path: string) => void; | ||
| readonly onOpenChange: (open: boolean) => void; | ||
| readonly onOpenFile: (path: string) => void; | ||
| readonly projectName: string; | ||
| readonly rootPath: string; | ||
| }) { | ||
| const entriesQuery = useProjectEntriesQuery(props.environmentId, props.cwd); | ||
| const { resolvedTheme } = useTheme(); | ||
| const entries = entriesQuery.data?.entries ?? []; | ||
| const children = useMemo( | ||
| () => fileBreadcrumbChildren(entries, props.directoryPath), | ||
| [entries, props.directoryPath], | ||
| ); | ||
| const directoryAvailable = | ||
| props.directoryPath === "" || | ||
| entries.some((entry) => entry.kind === "directory" && entry.path === props.directoryPath); | ||
| const parentPath = fileBreadcrumbParent(props.directoryPath); | ||
| const canGoBack = | ||
| props.directoryPath !== props.rootPath && | ||
| parentPath !== null && | ||
| (props.rootPath === "" || | ||
| parentPath === props.rootPath || | ||
| parentPath.startsWith(`${props.rootPath}/`)); | ||
|
|
||
| return ( | ||
| <MenuPopup | ||
| align="start" | ||
| side="bottom" | ||
| className="w-[min(19rem,var(--available-width))]" | ||
| onKeyDown={(event) => { | ||
| if (event.key !== "ArrowLeft" || !canGoBack || parentPath === null) return; | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| props.onDirectoryChange(parentPath); | ||
| }} | ||
| > | ||
| <MenuGroup> | ||
| <MenuGroupLabel className="flex min-w-0 items-center gap-1 px-2 py-1.5"> | ||
| <span className="truncate text-foreground"> | ||
| {pathLabel(props.directoryPath, props.projectName)} | ||
| </span> | ||
| <span className="min-w-0 flex-1 truncate text-right font-normal opacity-70"> | ||
| {props.directoryPath || props.projectName} | ||
| </span> | ||
| </MenuGroupLabel> | ||
| </MenuGroup> | ||
| {canGoBack && parentPath !== null ? ( | ||
| <> | ||
| <MenuItem closeOnClick={false} onClick={() => props.onDirectoryChange(parentPath)}> | ||
| <ArrowLeftIcon /> | ||
| <span className="truncate">Back to {pathLabel(parentPath, props.projectName)}</span> | ||
| </MenuItem> | ||
| <MenuSeparator /> | ||
| </> | ||
| ) : null} | ||
| <MenuGroup key={props.directoryPath}> | ||
| {entriesQuery.isPending && entriesQuery.data === null ? ( | ||
| <MenuItem disabled> | ||
| <LoaderCircleIcon className="animate-spin" /> | ||
| Loading folder… | ||
| </MenuItem> | ||
| ) : entriesQuery.error && entriesQuery.data === null ? ( | ||
| <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}> | ||
| <RotateCwIcon /> | ||
| <span className="min-w-0 flex-1 truncate">Retry loading folder</span> | ||
| </MenuItem> | ||
| ) : !directoryAvailable ? ( | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Outdated
|
||
| <MenuItem disabled>This folder is no longer available.</MenuItem> | ||
| ) : children.length === 0 ? ( | ||
| <MenuItem disabled>This folder is empty.</MenuItem> | ||
| ) : ( | ||
| children.map((entry) => { | ||
| const isCurrentFile = entry.kind === "file" && entry.path === props.currentFilePath; | ||
| const containsCurrentFile = | ||
| entry.kind === "directory" && props.currentFilePath.startsWith(`${entry.path}/`); | ||
| return ( | ||
| <MenuItem | ||
| key={entry.path} | ||
| closeOnClick={entry.kind === "file"} | ||
| aria-current={isCurrentFile ? "page" : undefined} | ||
| onClick={() => { | ||
| if (entry.kind === "directory") { | ||
| props.onDirectoryChange(entry.path); | ||
| return; | ||
| } | ||
| props.onOpenChange(false); | ||
| props.onOpenFile(entry.path); | ||
| }} | ||
| > | ||
| <PierreEntryIcon pathValue={entry.path} kind={entry.kind} theme={resolvedTheme} /> | ||
| <Tooltip> | ||
| <TooltipTrigger render={<span className="min-w-0 flex-1 truncate" />}> | ||
| {entry.label} | ||
| </TooltipTrigger> | ||
| <TooltipPopup side="right" className="max-w-80"> | ||
| {entry.path} | ||
| </TooltipPopup> | ||
| </Tooltip> | ||
| {isCurrentFile ? ( | ||
| <CheckIcon className="text-primary" aria-label="Current file" /> | ||
| ) : entry.kind === "directory" ? ( | ||
| <ChevronRightIcon | ||
| className={cn(containsCurrentFile && "text-primary")} | ||
| aria-label={containsCurrentFile ? "Contains current file" : undefined} | ||
| /> | ||
| ) : null} | ||
| </MenuItem> | ||
| ); | ||
| }) | ||
| )} | ||
| </MenuGroup> | ||
| {entriesQuery.error && entriesQuery.data !== null ? ( | ||
| <> | ||
| <MenuSeparator /> | ||
| <MenuItem closeOnClick={false} onClick={entriesQuery.refresh}> | ||
| <RotateCwIcon /> | ||
| Refresh failed — retry | ||
| </MenuItem> | ||
| </> | ||
| ) : null} | ||
| {entriesQuery.data?.truncated ? ( | ||
| <> | ||
| <MenuSeparator /> | ||
| <MenuItem disabled>Some workspace entries are not shown.</MenuItem> | ||
| </> | ||
| ) : null} | ||
| </MenuPopup> | ||
| ); | ||
| } | ||
|
|
||
| function DirectoryBreadcrumb(props: FileBreadcrumbsProps & { readonly crumb: FileBreadcrumb }) { | ||
| const [open, setOpen] = useState(false); | ||
| const [directoryPath, setDirectoryPath] = useState(props.crumb.path); | ||
|
|
||
| useEffect(() => { | ||
| setOpen(false); | ||
| setDirectoryPath(props.crumb.path); | ||
| }, [props.crumb.path, props.relativePath]); | ||
|
|
||
| const handleOpenChange = (nextOpen: boolean) => { | ||
| setOpen(nextOpen); | ||
| if (nextOpen) setDirectoryPath(props.crumb.path); | ||
| }; | ||
|
|
||
| return ( | ||
| <Menu open={open} onOpenChange={handleOpenChange}> | ||
| <Tooltip> | ||
| <TooltipTrigger | ||
| render={ | ||
| <MenuTrigger | ||
| render={ | ||
| <button | ||
| type="button" | ||
| aria-label={`Browse ${props.crumb.label}`} | ||
| className="block max-w-40 cursor-pointer truncate rounded-sm px-0.5 text-left text-muted-foreground outline-none hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring data-popup-open:bg-accent data-popup-open:text-foreground" | ||
| /> | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| } | ||
| /> | ||
| } | ||
| > | ||
| {props.crumb.label} | ||
| </TooltipTrigger> | ||
| <TooltipPopup side="top" className="max-w-80"> | ||
| {props.crumb.path || props.projectName} | ||
| </TooltipPopup> | ||
| </Tooltip> | ||
| {open ? ( | ||
| <BreadcrumbMenuContent | ||
| cwd={props.cwd} | ||
| currentFilePath={props.relativePath} | ||
| directoryPath={directoryPath} | ||
| environmentId={props.environmentId} | ||
| onDirectoryChange={setDirectoryPath} | ||
| onOpenChange={handleOpenChange} | ||
| onOpenFile={props.onOpenFile} | ||
| projectName={props.projectName} | ||
| rootPath={props.crumb.path} | ||
| /> | ||
| ) : null} | ||
| </Menu> | ||
| ); | ||
| } | ||
|
|
||
| export function FileBreadcrumbs(props: FileBreadcrumbsProps) { | ||
| const breadcrumbs = useMemo( | ||
| () => fileBreadcrumbs(props.projectName, props.relativePath), | ||
| [props.projectName, props.relativePath], | ||
| ); | ||
|
|
||
| return breadcrumbs.map((crumb, index) => ( | ||
| <div | ||
| key={crumb.path || "project"} | ||
| className="flex min-w-0 shrink-0 items-center" | ||
| data-current-file-crumb={crumb.kind === "file"} | ||
| > | ||
| {index > 0 ? ( | ||
| <ChevronRightIcon className="mx-1 size-3.5 shrink-0 text-muted-foreground/60" /> | ||
| ) : null} | ||
| {crumb.kind === "file" ? ( | ||
| <span aria-current="page"> | ||
| <BreadcrumbLabel current label={crumb.label} pathLabel={crumb.path} /> | ||
| </span> | ||
| ) : ( | ||
| <DirectoryBreadcrumb {...props} crumb={crumb} /> | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| )} | ||
| </div> | ||
| )); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.