2026-10-07 13:53:48 +08:00
|
|
|
import { FullAccessIcon, WorkspaceIcon, RestrictedAccessIcon } from "@/components/icons/product-icons";
|
2026-10-07 15:12:58 +08:00
|
|
|
import type { HTMLAttributes, ReactElement } from "react";
|
2026-10-07 13:53:48 +08:00
|
|
|
import { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
2026-10-08 10:06:31 +08:00
|
|
|
import { ArrowLeft, ArrowRight, ArrowUp, Check, ChevronRight, FolderSearch, Pencil, Search, Star, X } from "lucide-react";
|
2026-05-29 03:42:53 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-10-07 13:53:48 +08:00
|
|
|
import { ToggleButton } from "@/components/settings/ToggleButton";
|
2026-05-29 03:42:53 +08:00
|
|
|
import { Button } from "@/components/ui/button";
|
2026-10-07 14:47:16 +08:00
|
|
|
import { floatingItemClassName, floatingItemFocusClassName } from "@/components/ui/floating-surface";
|
2026-10-07 13:53:48 +08:00
|
|
|
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
|
2026-05-29 03:42:53 +08:00
|
|
|
import { Input } from "@/components/ui/input";
|
2026-10-07 13:53:48 +08:00
|
|
|
import { Dialog, DialogContent, DialogTrigger, DialogTitle, DialogDescription } from "@/components/ui/dialog";
|
|
|
|
|
import type { WorkspaceScopePayload, ProjectDirectory, WorkspaceDirectoriesPayload, WorkspacesPayload } from "@/lib/types";
|
|
|
|
|
import { createWorkspaceDirectoryCache } from "@/lib/workspace-directory-cache";
|
2026-10-08 10:06:31 +08:00
|
|
|
import { useMediaQuery } from "@/hooks/useMediaQuery";
|
2026-05-29 03:42:53 +08:00
|
|
|
import { cn } from "@/lib/utils";
|
2026-10-08 10:06:31 +08:00
|
|
|
import { isAbsoluteWorkspacePath, projectNameFromPath, sameWorkspacePath, scopeWithAccessMode, workspaceBreadcrumbs, workspacePathCompletionQuery, workspaceDirectoryPrefix, selectedProjectScope, type BrowseWorkspaceDirectories } from "@/lib/workspace";
|
2026-05-29 03:42:53 +08:00
|
|
|
|
2026-10-07 14:47:16 +08:00
|
|
|
function WorkspaceFavoriteButton({ path, pinned, busy, onToggle }: {
|
|
|
|
|
path: string; pinned: boolean; busy: boolean; onToggle: () => void;
|
2026-10-07 13:53:48 +08:00
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
2026-10-07 14:47:16 +08:00
|
|
|
const label = t(pinned ? "workspace.picker.unpin" : "workspace.picker.pin", { name: projectNameFromPath(path) });
|
2026-10-07 13:53:48 +08:00
|
|
|
return (
|
2026-10-07 14:47:16 +08:00
|
|
|
<WorkspacePickerTooltip label={label}>
|
|
|
|
|
<button type="button" aria-label={label} aria-pressed={pinned} disabled={busy}
|
|
|
|
|
onMouseDown={event => event.preventDefault()} onClick={onToggle}
|
|
|
|
|
className="workspace-picker-favorite touch-target absolute right-1 top-1/2 inline-flex h-8 w-8 -translate-y-1/2 items-center justify-center rounded-control text-muted-foreground outline-none hover:bg-foreground/[0.055] hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none">
|
|
|
|
|
<Star className={cn("h-3.5 w-3.5", pinned && "fill-current text-foreground")} />
|
|
|
|
|
</button>
|
|
|
|
|
</WorkspacePickerTooltip>
|
2026-10-07 13:53:48 +08:00
|
|
|
);
|
2026-08-10 15:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 10:06:31 +08:00
|
|
|
type PickerOption = ProjectDirectory & { kind: "directory" | "manual" };
|
|
|
|
|
type PickerColumn = { path: string; options: PickerOption[]; selectedPath: string | null; hidden: boolean };
|
2026-10-07 13:53:48 +08:00
|
|
|
|
|
|
|
|
function WorkspacePickerTooltip({ label, children }: { label: string; children: ReactElement }) {
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>{children}</TooltipTrigger>
|
|
|
|
|
<TooltipContent className="max-w-[min(28rem,calc(100vw-2rem))] whitespace-pre-line break-all">{label}</TooltipContent>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 10:06:31 +08:00
|
|
|
function PickerToolButton({ label, disabled, onClick, children }: {
|
|
|
|
|
label: string; disabled?: boolean; onClick: () => void; children: ReactElement;
|
|
|
|
|
}) {
|
|
|
|
|
return <WorkspacePickerTooltip label={label}>
|
|
|
|
|
<Button type="button" variant="ghost" size="icon" aria-label={label} disabled={disabled} onClick={onClick}
|
|
|
|
|
className="h-11 w-11 shrink-0 rounded-control text-muted-foreground sm:h-9 sm:w-9">{children}</Button>
|
|
|
|
|
</WorkspacePickerTooltip>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-07 15:12:58 +08:00
|
|
|
const DIRECTORY_ROW_HEIGHT = 44;
|
|
|
|
|
const DIRECTORY_OVERSCAN = 4;
|
|
|
|
|
|
|
|
|
|
function WorkspaceDirectoryColumn({ options, activeIndex, renderOption, children, ...props }: {
|
2026-10-08 10:06:31 +08:00
|
|
|
options: PickerOption[];
|
2026-10-07 15:12:58 +08:00
|
|
|
activeIndex: number | null;
|
2026-10-08 10:06:31 +08:00
|
|
|
renderOption: (option: PickerOption, index: number) => ReactElement;
|
2026-10-07 15:12:58 +08:00
|
|
|
} & HTMLAttributes<HTMLDivElement>) {
|
|
|
|
|
const viewportRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const [firstRow, setFirstRow] = useState(0);
|
|
|
|
|
const [visibleRows, setVisibleRows] = useState(1);
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const viewport = viewportRef.current;
|
|
|
|
|
if (!viewport) return;
|
|
|
|
|
const measure = () => setVisibleRows(Math.max(1, Math.ceil(viewport.clientHeight / DIRECTORY_ROW_HEIGHT)));
|
|
|
|
|
measure();
|
|
|
|
|
const observer = new ResizeObserver(measure);
|
|
|
|
|
observer.observe(viewport);
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
}, []);
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const viewport = viewportRef.current;
|
|
|
|
|
if (!viewport) return;
|
|
|
|
|
viewport.scrollTop = 0;
|
|
|
|
|
setFirstRow(0);
|
|
|
|
|
}, [options]);
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const viewport = viewportRef.current;
|
|
|
|
|
if (!viewport || activeIndex === null) return;
|
|
|
|
|
const top = activeIndex * DIRECTORY_ROW_HEIGHT + 4;
|
|
|
|
|
const bottom = top + DIRECTORY_ROW_HEIGHT;
|
|
|
|
|
if (top < viewport.scrollTop) viewport.scrollTop = top;
|
|
|
|
|
else if (bottom > viewport.scrollTop + viewport.clientHeight) viewport.scrollTop = Math.max(0, bottom - viewport.clientHeight);
|
|
|
|
|
setFirstRow(Math.floor(viewport.scrollTop / DIRECTORY_ROW_HEIGHT));
|
|
|
|
|
}, [activeIndex, visibleRows, options]);
|
|
|
|
|
const start = Math.max(0, firstRow - DIRECTORY_OVERSCAN);
|
|
|
|
|
const end = Math.min(options.length, firstRow + visibleRows + DIRECTORY_OVERSCAN);
|
|
|
|
|
return (
|
|
|
|
|
<div {...props} ref={viewportRef} onScroll={event => setFirstRow(Math.floor(event.currentTarget.scrollTop / DIRECTORY_ROW_HEIGHT))}>
|
|
|
|
|
<div role="presentation" className="relative" style={{ height: options.length * DIRECTORY_ROW_HEIGHT }}>
|
|
|
|
|
{options.slice(start, end).map((option, offset) => {
|
|
|
|
|
const index = start + offset;
|
2026-10-08 10:06:31 +08:00
|
|
|
return <div key={`${option.kind}-${option.path}`} role="presentation" className="absolute inset-x-0" style={{ top: index * DIRECTORY_ROW_HEIGHT }}>
|
2026-10-07 15:12:58 +08:00
|
|
|
{renderOption(option, index)}
|
|
|
|
|
</div>;
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-07 13:53:48 +08:00
|
|
|
function WorkspaceDirectorySkeleton() {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const viewportRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const [rows, setRows] = useState(1);
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const viewport = viewportRef.current;
|
|
|
|
|
if (!viewport) return;
|
2026-10-07 15:12:58 +08:00
|
|
|
const measure = () => setRows(Math.max(1, Math.ceil(viewport.clientHeight / DIRECTORY_ROW_HEIGHT)));
|
2026-10-07 13:53:48 +08:00
|
|
|
measure();
|
|
|
|
|
const observer = new ResizeObserver(measure);
|
|
|
|
|
observer.observe(viewport);
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
}, []);
|
|
|
|
|
const widths = ["64%", "78%", "52%", "70%", "60%", "74%"];
|
|
|
|
|
return (
|
|
|
|
|
<div ref={viewportRef} role="status" aria-label={t("workspace.picker.loading")} className="workspace-directory-loading h-full">
|
|
|
|
|
<span className="sr-only">{t("workspace.picker.loading")}</span>
|
|
|
|
|
<div aria-hidden="true">
|
|
|
|
|
{Array.from({ length: rows }, (_, index) => (
|
|
|
|
|
<div key={index} className="flex h-11 items-center px-3 py-2">
|
|
|
|
|
<span className="h-3.5 rounded-control bg-foreground/[0.055]" style={{ width: widths[index % widths.length] }} />
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function WorkspacePickerPath({ path }: { path: string }) {
|
|
|
|
|
const parts = workspacePathCompletionQuery(path);
|
|
|
|
|
return (
|
|
|
|
|
<WorkspacePickerTooltip label={path}>
|
|
|
|
|
<span className="flex min-w-0 flex-1 text-[13px]">
|
|
|
|
|
<span className="min-w-0 truncate">{parts?.path ?? path}</span>
|
|
|
|
|
{parts?.query && <span className="max-w-[65%] shrink-0 truncate">{parts.query}</span>}
|
|
|
|
|
</span>
|
|
|
|
|
</WorkspacePickerTooltip>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 10:06:31 +08:00
|
|
|
function WorkspaceSavedProjectLabel({ path }: { path: string }) {
|
|
|
|
|
const parts = workspacePathCompletionQuery(path);
|
|
|
|
|
return (
|
|
|
|
|
<span className="min-w-0 flex-1">
|
|
|
|
|
<span className="block truncate text-[13px] font-medium leading-5">{projectNameFromPath(path)}</span>
|
|
|
|
|
{parts?.query && <span dir="rtl" className="block truncate text-left text-[11px] leading-4 text-muted-foreground"><bdi dir="ltr">{parts.path}</bdi></span>}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-07 16:25:40 +08:00
|
|
|
export function WorkspaceProjectPicker({ isHero, disabled, scope, defaultScope, controls, error, onLoadProjects, onResolveProject, onFavoriteProject, onBrowseDirectories, layoutAnchor, onChange }: {
|
2026-10-07 13:53:48 +08:00
|
|
|
layoutAnchor?: HTMLElement | null;
|
2026-05-29 03:42:53 +08:00
|
|
|
isHero: boolean;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
scope: WorkspaceScopePayload | null;
|
|
|
|
|
defaultScope: WorkspaceScopePayload | null;
|
|
|
|
|
controls: WorkspacesPayload["controls"] | null;
|
|
|
|
|
error?: string | null;
|
2026-10-08 10:06:31 +08:00
|
|
|
onResolveProject?: (path: string) => Promise<ProjectDirectory>;
|
|
|
|
|
onLoadProjects?: () => Promise<WorkspacesPayload>;
|
|
|
|
|
onFavoriteProject?: (path: string, pinned: boolean) => Promise<ProjectDirectory[]>;
|
|
|
|
|
onBrowseDirectories?: BrowseWorkspaceDirectories;
|
2026-05-29 03:42:53 +08:00
|
|
|
onChange?: (scope: WorkspaceScopePayload) => void;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
const [pathDraft, setPathDraft] = useState("");
|
2026-10-08 10:06:31 +08:00
|
|
|
const [editingPath, setEditingPath] = useState(false);
|
|
|
|
|
const [filterQuery, setFilterQuery] = useState("");
|
|
|
|
|
const [showLocations, setShowLocations] = useState(false);
|
|
|
|
|
const [history, setHistory] = useState<{ paths: string[]; index: number }>({ paths: [], index: 0 });
|
|
|
|
|
const compact = useMediaQuery("(max-width: 639px)");
|
2026-10-07 13:53:48 +08:00
|
|
|
const [basePath, setBasePath] = useState(scope?.project_path ?? defaultScope?.project_path ?? "");
|
|
|
|
|
const [catalog, setCatalog] = useState<WorkspacesPayload | null>(null);
|
|
|
|
|
const [directory, setDirectory] = useState<WorkspaceDirectoriesPayload | null>(null);
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const [showHidden, setShowHidden] = useState(false);
|
|
|
|
|
const [revision, setRevision] = useState(0);
|
|
|
|
|
const [directoryError, setDirectoryError] = useState<string | null>(null);
|
2026-05-29 03:42:53 +08:00
|
|
|
const [pathError, setPathError] = useState<string | null>(null);
|
|
|
|
|
const [pickingFolder, setPickingFolder] = useState(false);
|
2026-10-07 13:53:48 +08:00
|
|
|
const [favoriteBusy, setFavoriteBusy] = useState(false);
|
|
|
|
|
const [activeIndex, setActiveIndex] = useState(0);
|
|
|
|
|
const [activeColumn, setActiveColumn] = useState(0);
|
|
|
|
|
const [highlightActive, setHighlightActive] = useState(false);
|
|
|
|
|
const [previousColumns, setPreviousColumns] = useState<PickerColumn[]>([]);
|
|
|
|
|
const columnsRef = useRef<HTMLDivElement | null>(null);
|
2026-10-08 10:06:31 +08:00
|
|
|
const breadcrumbRef = useRef<HTMLElement | null>(null);
|
|
|
|
|
const attachBreadcrumb = useCallback((element: HTMLElement | null) => {
|
|
|
|
|
breadcrumbRef.current = element;
|
|
|
|
|
// Dialog portals mount after the parent's layout effect on first open.
|
|
|
|
|
if (element) window.requestAnimationFrame(() => { if (element.isConnected) element.scrollLeft = element.scrollWidth; });
|
|
|
|
|
}, []);
|
2026-10-07 13:53:48 +08:00
|
|
|
const [columnsElement, setColumnsElement] = useState<HTMLDivElement | null>(null);
|
|
|
|
|
const attachColumns = useCallback((element: HTMLDivElement | null) => {
|
|
|
|
|
columnsRef.current = element;
|
|
|
|
|
setColumnsElement(element);
|
|
|
|
|
}, []);
|
2026-10-07 14:39:14 +08:00
|
|
|
const [visibleColumns, setVisibleColumns] = useState(3);
|
2026-10-08 10:06:31 +08:00
|
|
|
const columnCount = Math.min(visibleColumns, previousColumns.length + 1);
|
2026-10-07 13:53:48 +08:00
|
|
|
const pickerSession = useRef(0);
|
|
|
|
|
const completionRequest = useRef(0);
|
2026-08-10 15:22:25 +08:00
|
|
|
const triggerRef = useRef<HTMLButtonElement>(null);
|
2026-10-07 13:53:48 +08:00
|
|
|
const keyboardInteraction = useRef(false);
|
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
const optionsId = useId();
|
|
|
|
|
const errorId = useId();
|
2026-05-29 03:42:53 +08:00
|
|
|
const currentProjectScope = selectedProjectScope(scope, defaultScope);
|
2026-10-07 13:53:48 +08:00
|
|
|
const displayedScope = scope ?? defaultScope;
|
2026-10-07 16:25:40 +08:00
|
|
|
const projectLabel = displayedScope ? displayedScope.project_name || projectNameFromPath(displayedScope.project_path) : "";
|
2026-10-07 13:53:48 +08:00
|
|
|
const visible = isHero && !!defaultScope && !!onChange && controls?.can_change_project !== false;
|
2026-10-08 10:06:31 +08:00
|
|
|
const canBrowse = controls?.can_browse_directories === true && !!onBrowseDirectories;
|
|
|
|
|
const directoryCache = useMemo(() => onBrowseDirectories ? createWorkspaceDirectoryCache(onBrowseDirectories) : null, [onBrowseDirectories]);
|
2026-10-07 13:53:48 +08:00
|
|
|
const absoluteDraft = isAbsoluteWorkspacePath(pathDraft);
|
2026-10-08 10:06:31 +08:00
|
|
|
const requestedPath = editingPath && absoluteDraft ? pathDraft.trim() : basePath;
|
|
|
|
|
const folderQuery = editingPath ? "" : filterQuery.trim();
|
2026-05-29 03:42:53 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-10-07 13:53:48 +08:00
|
|
|
pickerSession.current += 1;
|
|
|
|
|
completionRequest.current += 1;
|
2026-05-29 03:42:53 +08:00
|
|
|
if (!open) return;
|
2026-10-07 13:53:48 +08:00
|
|
|
setPreviousColumns([]);
|
|
|
|
|
setActiveColumn(0);
|
|
|
|
|
setHighlightActive(false);
|
|
|
|
|
const initialPath = scope?.project_path ?? defaultScope?.project_path ?? "";
|
2026-10-08 10:06:31 +08:00
|
|
|
setPathDraft(canBrowse ? workspaceDirectoryPrefix(initialPath) : currentProjectScope?.project_path ?? "");
|
2026-10-07 13:53:48 +08:00
|
|
|
setBasePath(initialPath);
|
2026-10-08 10:06:31 +08:00
|
|
|
setHistory({ paths: [initialPath], index: 0 });
|
|
|
|
|
setEditingPath(false);
|
|
|
|
|
setFilterQuery("");
|
|
|
|
|
setShowLocations(false);
|
2026-10-07 13:53:48 +08:00
|
|
|
setDirectory(null);
|
|
|
|
|
setActiveIndex(0);
|
2026-05-29 03:42:53 +08:00
|
|
|
setPathError(null);
|
2026-10-08 10:06:31 +08:00
|
|
|
}, [scope?.project_path, defaultScope?.project_path, open, canBrowse]);
|
2026-05-29 03:42:53 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-10-08 10:06:31 +08:00
|
|
|
if (!open || !onLoadProjects) return;
|
2026-10-07 13:53:48 +08:00
|
|
|
let active = true;
|
|
|
|
|
setCatalog(null);
|
|
|
|
|
onLoadProjects().then(payload => { if (active) setCatalog(payload); }).catch((err: Error) => { if (active) setPathError(err.message); });
|
|
|
|
|
return () => { active = false; };
|
|
|
|
|
}, [onLoadProjects, open]);
|
2026-08-06 18:29:04 +08:00
|
|
|
|
2026-10-07 13:53:48 +08:00
|
|
|
useEffect(() => {
|
2026-10-08 10:06:31 +08:00
|
|
|
if (!open || !canBrowse || !directoryCache) return;
|
|
|
|
|
if (editingPath && !absoluteDraft) { setDirectory(null); setLoading(false); return; }
|
2026-10-07 13:53:48 +08:00
|
|
|
let active = true;
|
|
|
|
|
const apply = (result: WorkspaceDirectoriesPayload) => {
|
|
|
|
|
if (!active) return;
|
|
|
|
|
setDirectory(result); setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
setDirectoryError(null);
|
|
|
|
|
const cached = directoryCache.peek(requestedPath, folderQuery, showHidden, true);
|
|
|
|
|
if (cached) { apply(cached); return; }
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setDirectory(null);
|
2026-10-08 10:06:31 +08:00
|
|
|
const typing = !!folderQuery || editingPath && !/[\\/]$/.test(pathDraft);
|
2026-10-07 13:53:48 +08:00
|
|
|
const timer = window.setTimeout(() => {
|
|
|
|
|
directoryCache.load(requestedPath, folderQuery, showHidden, true).then(apply).catch((err: Error) => {
|
|
|
|
|
if (active) { setDirectoryError(err.message); setLoading(false); }
|
|
|
|
|
});
|
|
|
|
|
}, typing ? 150 : 0);
|
|
|
|
|
return () => { active = false; window.clearTimeout(timer); };
|
2026-10-08 10:06:31 +08:00
|
|
|
}, [open, canBrowse, directoryCache, requestedPath, folderQuery, showHidden, revision, absoluteDraft, pathDraft, editingPath]);
|
2026-10-07 13:53:48 +08:00
|
|
|
|
|
|
|
|
useEffect(() => { if (disabled) setOpen(false); }, [disabled]);
|
2026-08-06 18:29:04 +08:00
|
|
|
useEffect(() => {
|
2026-08-10 15:22:25 +08:00
|
|
|
if (!error || !visible || disabled) return;
|
|
|
|
|
const frame = window.requestAnimationFrame(() => triggerRef.current?.focus());
|
|
|
|
|
return () => window.cancelAnimationFrame(frame);
|
2026-08-06 18:29:04 +08:00
|
|
|
}, [disabled, error, visible]);
|
2026-10-07 13:53:48 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!open || pickingFolder) return;
|
2026-10-08 10:06:31 +08:00
|
|
|
const frame = window.requestAnimationFrame(() => {
|
|
|
|
|
if (editingPath || !canBrowse) inputRef.current?.focus();
|
|
|
|
|
});
|
2026-10-07 13:53:48 +08:00
|
|
|
return () => window.cancelAnimationFrame(frame);
|
2026-10-08 10:06:31 +08:00
|
|
|
}, [open, pickingFolder, pathError, editingPath, canBrowse]);
|
2026-10-07 13:53:48 +08:00
|
|
|
|
|
|
|
|
const ancestorPaths = JSON.stringify(previousColumns.filter(column => column.hidden !== showHidden).map(column => column.path));
|
|
|
|
|
useEffect(() => {
|
2026-10-08 10:06:31 +08:00
|
|
|
if (!open || !canBrowse || !directoryCache) return;
|
2026-10-07 13:53:48 +08:00
|
|
|
const paths: string[] = JSON.parse(ancestorPaths);
|
|
|
|
|
if (!paths.length) return;
|
|
|
|
|
let active = true;
|
|
|
|
|
Promise.all(paths.map(path => directoryCache.load(path, "", showHidden, true))).then(results => {
|
|
|
|
|
if (!active) return;
|
|
|
|
|
setPreviousColumns(columns => columns.map(column => {
|
|
|
|
|
const result = results.find(result => sameWorkspacePath(column.path, result.path));
|
2026-10-07 14:39:14 +08:00
|
|
|
if (!result) return column;
|
2026-10-08 10:06:31 +08:00
|
|
|
return { ...column, hidden: showHidden, options: result.entries.map(entry => ({ ...entry, kind: "directory" as const })) };
|
2026-10-07 13:53:48 +08:00
|
|
|
}));
|
|
|
|
|
}).catch((err: Error) => { if (active) setPathError(err.message); });
|
|
|
|
|
return () => { active = false; };
|
2026-10-08 10:06:31 +08:00
|
|
|
}, [open, canBrowse, directoryCache, ancestorPaths, showHidden]);
|
2026-10-07 13:53:48 +08:00
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const viewport = columnsElement;
|
|
|
|
|
if (!open || !viewport) return;
|
2026-10-07 14:39:14 +08:00
|
|
|
const measure = () => setVisibleColumns(Math.max(1, Math.min(3, Math.floor(viewport.clientWidth / 240))));
|
2026-10-07 13:53:48 +08:00
|
|
|
measure();
|
|
|
|
|
const observer = new ResizeObserver(measure);
|
|
|
|
|
observer.observe(viewport);
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
}, [open, columnsElement]);
|
2026-05-29 03:42:53 +08:00
|
|
|
|
2026-08-10 15:22:25 +08:00
|
|
|
useEffect(() => {
|
2026-10-07 13:53:48 +08:00
|
|
|
const viewport = columnsElement;
|
|
|
|
|
if (!open || !viewport) return;
|
|
|
|
|
const onWheel = (event: WheelEvent) => {
|
|
|
|
|
if (!event.shiftKey || event.ctrlKey || event.metaKey || viewport.scrollWidth <= viewport.clientWidth) return;
|
|
|
|
|
const delta = event.deltaX || event.deltaY;
|
|
|
|
|
if (!delta) return;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const unit = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? viewport.clientWidth : 1;
|
|
|
|
|
viewport.scrollLeft = Math.max(0, Math.min(viewport.scrollWidth - viewport.clientWidth, viewport.scrollLeft + delta * unit));
|
|
|
|
|
};
|
|
|
|
|
viewport.addEventListener("wheel", onWheel, { passive: false });
|
|
|
|
|
return () => viewport.removeEventListener("wheel", onWheel);
|
|
|
|
|
}, [open, columnsElement]);
|
2026-08-10 15:22:25 +08:00
|
|
|
|
2026-10-07 13:53:48 +08:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const viewport = columnsRef.current;
|
2026-10-08 10:06:31 +08:00
|
|
|
if (viewport) viewport.scrollLeft = viewport.clientWidth * Math.max(0, previousColumns.length + 1 - columnCount) / columnCount;
|
|
|
|
|
}, [previousColumns.length, columnCount]);
|
2026-05-29 03:42:53 +08:00
|
|
|
|
2026-10-07 15:12:58 +08:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const viewport = columnsRef.current;
|
|
|
|
|
if (!viewport || !highlightActive) return;
|
2026-10-08 10:06:31 +08:00
|
|
|
const width = viewport.clientWidth / columnCount;
|
2026-10-07 15:12:58 +08:00
|
|
|
const left = activeColumn * width;
|
|
|
|
|
const right = left + width;
|
|
|
|
|
if (left < viewport.scrollLeft) viewport.scrollLeft = left;
|
|
|
|
|
else if (right > viewport.scrollLeft + viewport.clientWidth) viewport.scrollLeft = right - viewport.clientWidth;
|
2026-10-08 10:06:31 +08:00
|
|
|
}, [activeColumn, highlightActive, columnCount]);
|
2026-10-07 15:12:58 +08:00
|
|
|
|
2026-10-07 13:53:48 +08:00
|
|
|
function changeDraft(value: string, resetColumns = true) {
|
|
|
|
|
setHighlightActive(false);
|
|
|
|
|
if (resetColumns) { setPreviousColumns([]); setActiveColumn(0); }
|
|
|
|
|
completionRequest.current += 1;
|
|
|
|
|
setPathDraft(value);
|
|
|
|
|
const absolute = isAbsoluteWorkspacePath(value);
|
2026-10-08 10:06:31 +08:00
|
|
|
const cached = absolute ? directoryCache?.peek(value.trim(), "", showHidden, true) : null;
|
2026-10-07 13:53:48 +08:00
|
|
|
setDirectory(cached ?? null);
|
2026-10-08 10:06:31 +08:00
|
|
|
setLoading(canBrowse && absolute && !cached);
|
2026-10-07 13:53:48 +08:00
|
|
|
setDirectoryError(null);
|
|
|
|
|
setPathError(null);
|
|
|
|
|
setActiveIndex(0);
|
|
|
|
|
}
|
2026-10-08 10:06:31 +08:00
|
|
|
function focusDirectory() {
|
|
|
|
|
window.requestAnimationFrame(() => columnsRef.current?.querySelector<HTMLElement>("[data-current-directory]")?.focus());
|
|
|
|
|
}
|
|
|
|
|
function navigate(path: string, columnIndex?: number, keepEditing = false, historyIndex?: number) {
|
|
|
|
|
const source = columnIndex === undefined ? undefined : columns[columnIndex];
|
|
|
|
|
const ancestors = columnIndex === undefined ? [] : columns.slice(0, columnIndex);
|
|
|
|
|
if (source?.options.length && !sameWorkspacePath(source.path, path)) ancestors.push({ ...source, selectedPath: path });
|
2026-10-07 13:53:48 +08:00
|
|
|
setPreviousColumns(ancestors);
|
|
|
|
|
setActiveColumn(ancestors.length);
|
|
|
|
|
setBasePath(path);
|
2026-10-08 10:06:31 +08:00
|
|
|
setFilterQuery("");
|
|
|
|
|
setShowLocations(false);
|
|
|
|
|
setEditingPath(keepEditing);
|
|
|
|
|
setHistory(current => {
|
|
|
|
|
if (historyIndex !== undefined) return { ...current, index: historyIndex };
|
|
|
|
|
if (sameWorkspacePath(current.paths[current.index], path)) return current;
|
|
|
|
|
return { paths: [...current.paths.slice(0, current.index + 1), path], index: current.index + 1 };
|
|
|
|
|
});
|
2026-10-07 13:53:48 +08:00
|
|
|
changeDraft(workspaceDirectoryPrefix(path), false);
|
|
|
|
|
setRevision(value => value + 1);
|
2026-10-08 10:06:31 +08:00
|
|
|
if (keepEditing) inputRef.current?.focus();
|
|
|
|
|
else focusDirectory();
|
2026-05-29 03:42:53 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 10:06:31 +08:00
|
|
|
const applyProjectPath = useCallback((path: string, name?: string) => {
|
2026-10-07 13:53:48 +08:00
|
|
|
const base = scope ?? defaultScope;
|
|
|
|
|
const trimmed = path.trim();
|
|
|
|
|
if (!base || !onChange) return;
|
2026-10-08 10:06:31 +08:00
|
|
|
if (!trimmed || !isAbsoluteWorkspacePath(trimmed)) { setPathError(t("workspace.dialog.absolutePathRequired")); return; }
|
2026-10-07 13:53:48 +08:00
|
|
|
const accessMode = controls?.can_use_full_access === false && !sameWorkspacePath(trimmed, base.project_path) ? "restricted" : base.access_mode;
|
2026-10-08 10:06:31 +08:00
|
|
|
onChange({ ...base, project_path: trimmed, project_name: name || projectNameFromPath(trimmed), access_mode: accessMode, restrict_to_workspace: accessMode === "restricted" });
|
2026-10-07 13:53:48 +08:00
|
|
|
setPathError(null);
|
|
|
|
|
setOpen(false);
|
2026-10-08 10:06:31 +08:00
|
|
|
}, [controls?.can_use_full_access, defaultScope, onChange, scope, t]);
|
2026-10-07 13:53:48 +08:00
|
|
|
|
2026-10-08 10:06:31 +08:00
|
|
|
const chooseProject = useCallback(async (path: string, name?: string) => {
|
2026-10-07 13:53:48 +08:00
|
|
|
const session = pickerSession.current;
|
2026-10-08 10:06:31 +08:00
|
|
|
if (controls?.can_resolve_project !== true || !onResolveProject) { applyProjectPath(path, name); return; }
|
2026-10-07 13:53:48 +08:00
|
|
|
setPickingFolder(true); setPathError(null);
|
|
|
|
|
try {
|
|
|
|
|
const project = await onResolveProject(path);
|
|
|
|
|
if (session === pickerSession.current) applyProjectPath(project.path, project.name);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (session === pickerSession.current) setPathError((err as Error).message);
|
|
|
|
|
} finally { setPickingFolder(false); }
|
2026-10-08 10:06:31 +08:00
|
|
|
}, [applyProjectPath, controls?.can_resolve_project, onResolveProject]);
|
2026-10-07 13:53:48 +08:00
|
|
|
|
|
|
|
|
const favorites = catalog?.favorite_projects ?? [];
|
2026-10-08 10:06:31 +08:00
|
|
|
const canFavorite = catalog?.controls.can_manage_favorites === true && !!onFavoriteProject;
|
2026-10-07 13:53:48 +08:00
|
|
|
const isFavorite = (path: string) => favorites.some(item => sameWorkspacePath(item.path, path));
|
2026-10-07 14:47:16 +08:00
|
|
|
function favoriteButton(path: string) {
|
|
|
|
|
return canFavorite ? <WorkspaceFavoriteButton path={path} pinned={isFavorite(path)} busy={favoriteBusy || pickingFolder}
|
|
|
|
|
onToggle={() => { void toggleFavorite(path); inputRef.current?.focus(); }} /> : null;
|
2026-10-07 13:53:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const toggleFavorite = async (path: string) => {
|
2026-10-08 10:06:31 +08:00
|
|
|
if (!onFavoriteProject || favoriteBusy) return;
|
2026-10-07 13:53:48 +08:00
|
|
|
const session = pickerSession.current;
|
|
|
|
|
setFavoriteBusy(true); setPathError(null);
|
|
|
|
|
try {
|
|
|
|
|
const favorites = await onFavoriteProject(path, !isFavorite(path));
|
|
|
|
|
if (session === pickerSession.current) setCatalog(current => current ? { ...current, favorite_projects: favorites } : current);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (session === pickerSession.current) setPathError((err as Error).message);
|
|
|
|
|
} finally { setFavoriteBusy(false); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const recentProjects = [
|
|
|
|
|
...(catalog?.recent_projects ?? []),
|
2026-10-07 17:51:37 +08:00
|
|
|
...(defaultScope ? [{ name: projectNameFromPath(defaultScope.project_path), path: defaultScope.project_path }] : []),
|
2026-10-07 13:53:48 +08:00
|
|
|
...(currentProjectScope ? [{ name: projectLabel, path: currentProjectScope.project_path }] : []),
|
|
|
|
|
].filter((project, index, all) => all.findIndex(item => sameWorkspacePath(item.path, project.path)) === index && !isFavorite(project.path));
|
2026-10-08 10:06:31 +08:00
|
|
|
const options = useMemo<PickerOption[]>(() => {
|
|
|
|
|
if (directory && !loading) return directory.entries.map(entry => ({ ...entry, kind: "directory" }));
|
|
|
|
|
return !canBrowse && absoluteDraft ? [{ name: projectNameFromPath(pathDraft.trim()), path: pathDraft.trim(), kind: "manual" }] : [];
|
|
|
|
|
}, [directory, loading, canBrowse, absoluteDraft, pathDraft]);
|
2026-10-07 13:53:48 +08:00
|
|
|
function openShortcut(project: ProjectDirectory) {
|
2026-10-08 10:06:31 +08:00
|
|
|
if (!canBrowse) { void chooseProject(project.path, project.name); return; }
|
|
|
|
|
navigate(project.path);
|
2026-10-07 13:53:48 +08:00
|
|
|
}
|
|
|
|
|
const columns: PickerColumn[] = [...previousColumns, { path: directory?.path ?? requestedPath, options, selectedPath: null, hidden: showHidden }];
|
|
|
|
|
const activeOptions = columns[activeColumn]?.options ?? options;
|
|
|
|
|
const activeOption = Math.min(activeIndex, Math.max(activeOptions.length - 1, 0));
|
2026-10-08 10:06:31 +08:00
|
|
|
const selectionPath = canBrowse ? (!loading && directory && !directory.partial ? directory.path : null) : (pathDraft.trim() ? absoluteDraft ? pathDraft.trim() : null : scope?.project_path ?? defaultScope?.project_path);
|
2026-10-07 13:53:48 +08:00
|
|
|
const displayedError = pathError ?? error ?? directoryError;
|
2026-10-08 10:06:31 +08:00
|
|
|
const currentPath = editingPath ? basePath : directory?.path ?? basePath;
|
|
|
|
|
const breadcrumbs = workspaceBreadcrumbs(currentPath);
|
|
|
|
|
const parentPath = directory?.parent;
|
|
|
|
|
const hostName = catalog?.host?.name ?? directory?.host;
|
|
|
|
|
const hostPlatform = catalog?.host?.platform ?? directory?.platform;
|
2026-10-07 13:53:48 +08:00
|
|
|
|
2026-10-08 10:06:31 +08:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (breadcrumbRef.current) breadcrumbRef.current.scrollLeft = breadcrumbRef.current.scrollWidth;
|
|
|
|
|
}, [currentPath, editingPath, open]);
|
|
|
|
|
|
|
|
|
|
function editPath() {
|
|
|
|
|
setEditingPath(true);
|
|
|
|
|
setFilterQuery("");
|
|
|
|
|
changeDraft(workspaceDirectoryPrefix(currentPath));
|
|
|
|
|
window.requestAnimationFrame(() => inputRef.current?.select());
|
|
|
|
|
}
|
|
|
|
|
function cancelPathEditing() {
|
|
|
|
|
setEditingPath(false);
|
|
|
|
|
changeDraft(workspaceDirectoryPrefix(basePath));
|
|
|
|
|
focusDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function completePath(keepEditing = true) {
|
|
|
|
|
if (!absoluteDraft) { setPathError(t("workspace.dialog.absolutePathRequired")); return; }
|
|
|
|
|
const request = workspacePathCompletionQuery(pathDraft) ?? { path: basePath, query: "" };
|
|
|
|
|
// Complete an exact address before descending into its implicitly active child.
|
|
|
|
|
const completingAddress = absoluteDraft && !!request.query && !highlightActive;
|
2026-10-07 13:53:48 +08:00
|
|
|
const option = activeOptions[activeOption];
|
2026-10-08 10:06:31 +08:00
|
|
|
if (highlightActive && option && option.kind !== "manual" && !completingAddress) { navigate(option.path, undefined, keepEditing); return; }
|
|
|
|
|
if (!directoryCache || !canBrowse) return;
|
2026-10-07 13:53:48 +08:00
|
|
|
const id = ++completionRequest.current;
|
|
|
|
|
const session = pickerSession.current;
|
|
|
|
|
try {
|
2026-10-07 15:12:58 +08:00
|
|
|
const result = await directoryCache.load(requestedPath, folderQuery, showHidden || request.query.startsWith("."), true);
|
2026-10-07 13:53:48 +08:00
|
|
|
if (id !== completionRequest.current || session !== pickerSession.current) return;
|
2026-10-08 10:06:31 +08:00
|
|
|
if (!result.partial) { navigate(result.path, undefined, keepEditing); return; }
|
2026-10-07 14:39:14 +08:00
|
|
|
const prefix = request.query.toLocaleLowerCase();
|
|
|
|
|
const first = result.entries.find(entry => entry.name.toLocaleLowerCase().startsWith(prefix));
|
2026-10-08 10:06:31 +08:00
|
|
|
if (first) navigate(first.path, undefined, keepEditing);
|
|
|
|
|
else setPathError(t("workspace.picker.pathNotFound"));
|
2026-10-07 13:53:48 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
if (id === completionRequest.current && session === pickerSession.current) setPathError((err as Error).message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-08 10:06:31 +08:00
|
|
|
function activate(option: PickerOption, columnIndex = activeColumn) {
|
2026-10-07 13:53:48 +08:00
|
|
|
if (pickingFolder) return;
|
2026-10-08 10:06:31 +08:00
|
|
|
if (canBrowse && option.kind !== "manual") navigate(option.path, columnIndex);
|
|
|
|
|
else void chooseProject(option.path, option.kind === "manual" ? undefined : option.name);
|
2026-10-07 13:53:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 10:06:31 +08:00
|
|
|
function moveHistory(offset: number) {
|
|
|
|
|
const index = history.index + offset;
|
|
|
|
|
if (index >= 0 && index < history.paths.length) navigate(history.paths[index], undefined, false, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const filterToolbar = canBrowse && !(compact && editingPath) && <div className="flex shrink-0 items-center gap-2 bg-background px-3 py-2">
|
|
|
|
|
{compact && <Button variant="ghost" size="sm" aria-expanded={showLocations} onClick={() => setShowLocations(value => !value)} className="h-11 shrink-0 gap-1.5 rounded-control px-2 text-[12px]"><Star className="h-3.5 w-3.5" />{t("workspace.picker.shortcuts")}</Button>}
|
|
|
|
|
<div className="relative min-w-0 flex-1">
|
|
|
|
|
<Search className="pointer-events-none absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground" />
|
|
|
|
|
<Input aria-label={t("workspace.picker.filter")} placeholder={t("workspace.picker.filter")} value={filterQuery} disabled={pickingFolder || editingPath || compact && showLocations} onChange={event => { setFilterQuery(event.target.value); setActiveIndex(0); setActiveColumn(previousColumns.length); setHighlightActive(false); }} className="h-11 rounded-control border-transparent bg-muted/60 pl-8 text-[16px] shadow-none sm:h-9 sm:text-[12px]" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>;
|
|
|
|
|
|
2026-10-07 13:53:48 +08:00
|
|
|
if (!visible || !defaultScope || !onChange) return null;
|
2026-05-29 03:42:53 +08:00
|
|
|
return (
|
2026-10-07 13:53:48 +08:00
|
|
|
<div className="inline-flex min-w-0 max-w-[11rem] shrink items-center">
|
|
|
|
|
<TooltipProvider>
|
|
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
|
<WorkspacePickerTooltip label={`${t("workspace.picker.switchDirectory")}\n${displayedScope?.project_path ?? ""}`}>
|
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
|
<button ref={triggerRef} onPointerDown={() => { keyboardInteraction.current = false; }} onKeyDown={() => { keyboardInteraction.current = true; }} type="button" disabled={disabled} aria-label={t("workspace.picker.switchDirectory")} className="thread-composer-workspace touch-target inline-flex h-8 min-w-0 max-w-full items-center gap-1.5 rounded-control px-2 text-[12px] font-medium text-muted-foreground outline-none transition-colors hover:bg-foreground/[0.055] hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-55">
|
|
|
|
|
<WorkspaceIcon className="h-3.5 w-3.5 shrink-0" />
|
|
|
|
|
<span className="min-w-0 truncate">{projectLabel}</span>
|
|
|
|
|
</button>
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
</WorkspacePickerTooltip>
|
2026-10-08 10:06:31 +08:00
|
|
|
<DialogContent showCloseButton={false} layoutAnchor={layoutAnchor} centerInLayoutAnchor className="flex h-[min(37rem,calc(100%-2rem))] max-w-5xl flex-col gap-0 overflow-hidden p-0 dark:bg-background"
|
|
|
|
|
onOpenAutoFocus={event => { event.preventDefault(); if (canBrowse) focusDirectory(); else inputRef.current?.focus(); }}
|
|
|
|
|
onPointerDownCapture={() => { keyboardInteraction.current = false; }}
|
|
|
|
|
onKeyDownCapture={event => {
|
|
|
|
|
keyboardInteraction.current = true;
|
|
|
|
|
if (!canBrowse || pickingFolder || event.nativeEvent.isComposing) return;
|
|
|
|
|
if ((event.metaKey || event.ctrlKey) && event.shiftKey && event.key.toLowerCase() === "g") { event.preventDefault(); editPath(); }
|
|
|
|
|
else if (event.altKey && event.key === "ArrowLeft") { event.preventDefault(); moveHistory(-1); }
|
|
|
|
|
else if (event.altKey && event.key === "ArrowRight") { event.preventDefault(); moveHistory(1); }
|
|
|
|
|
else if (event.altKey && event.key === "ArrowUp" && parentPath) { event.preventDefault(); navigate(parentPath); }
|
|
|
|
|
}}
|
|
|
|
|
onEscapeKeyDown={event => { if (editingPath) { event.preventDefault(); cancelPathEditing(); } }}
|
|
|
|
|
onCloseAutoFocus={event => { if (!keyboardInteraction.current) event.preventDefault(); }}>
|
|
|
|
|
<div className="flex shrink-0 items-center justify-between gap-3 bg-muted px-4 pt-4 pb-2 sm:px-5">
|
|
|
|
|
<DialogTitle className="text-[15px] font-semibold">{t("workspace.picker.title")}</DialogTitle>
|
|
|
|
|
{hostName && <WorkspacePickerTooltip label={`${hostName}${hostPlatform ? ` · ${hostPlatform}` : ""}`}>
|
|
|
|
|
<span className="min-w-0 max-w-[55%] truncate text-[11px] text-muted-foreground">{t("workspace.picker.host", { name: hostName })}</span>
|
|
|
|
|
</WorkspacePickerTooltip>}
|
|
|
|
|
</div>
|
2026-10-07 13:53:48 +08:00
|
|
|
<DialogDescription className="sr-only">{t("workspace.picker.description")}</DialogDescription>
|
2026-10-08 10:06:31 +08:00
|
|
|
<div className="flex shrink-0 items-center gap-1 border-b border-border/70 bg-muted px-3 pb-3 sm:px-4">
|
|
|
|
|
{canBrowse && <div className="flex shrink-0 items-center">
|
|
|
|
|
<PickerToolButton label={t("workspace.picker.back")} disabled={pickingFolder || history.index === 0} onClick={() => moveHistory(-1)}><ArrowLeft className="h-4 w-4" /></PickerToolButton>
|
|
|
|
|
{!compact && <PickerToolButton label={t("workspace.picker.forward")} disabled={pickingFolder || history.index >= history.paths.length - 1} onClick={() => moveHistory(1)}><ArrowRight className="h-4 w-4" /></PickerToolButton>}
|
|
|
|
|
<PickerToolButton label={t("workspace.picker.up")} disabled={pickingFolder || !parentPath || loading} onClick={() => parentPath && navigate(parentPath)}><ArrowUp className="h-4 w-4" /></PickerToolButton>
|
|
|
|
|
</div>}
|
|
|
|
|
{editingPath || !canBrowse ? <Input ref={inputRef} role="combobox" aria-expanded={open} aria-controls={`${optionsId}-${activeColumn}`} aria-activedescendant={highlightActive && activeOptions.length ? `${optionsId}-${activeColumn}-${activeOption}` : undefined} aria-autocomplete="list" aria-busy={loading} value={pathDraft} disabled={disabled || pickingFolder} onChange={event => changeDraft(event.target.value)} placeholder={t("workspace.dialog.manual")} aria-label={t("workspace.dialog.manual")} aria-invalid={displayedError ? true : undefined} aria-describedby={displayedError ? errorId : undefined} className="h-9 min-w-0 flex-1 rounded-control text-[16px] sm:text-[13px]" onKeyDown={event => {
|
2026-10-07 13:53:48 +08:00
|
|
|
if (event.nativeEvent.isComposing) return;
|
|
|
|
|
if ((event.key === "ArrowDown" || event.key === "ArrowUp") && !event.altKey && activeOptions.length) {
|
2026-05-29 03:42:53 +08:00
|
|
|
event.preventDefault();
|
2026-10-08 10:06:31 +08:00
|
|
|
const next = highlightActive ? (activeOption + (event.key === "ArrowDown" ? 1 : -1) + activeOptions.length) % activeOptions.length : event.key === "ArrowDown" ? 0 : activeOptions.length - 1;
|
2026-10-07 15:12:58 +08:00
|
|
|
setHighlightActive(true); setActiveIndex(next);
|
2026-10-08 10:06:31 +08:00
|
|
|
} else if (event.key === "Tab" && !event.shiftKey && canBrowse && absoluteDraft && (highlightActive || !/[\\/]$/.test(pathDraft))) {
|
2026-10-07 13:53:48 +08:00
|
|
|
event.preventDefault(); void completePath();
|
|
|
|
|
} else if (event.key === "Enter") {
|
|
|
|
|
event.preventDefault();
|
2026-10-08 10:06:31 +08:00
|
|
|
if (!canBrowse) void chooseProject(pathDraft);
|
|
|
|
|
else if ((event.ctrlKey || event.metaKey) && selectionPath) void chooseProject(selectionPath);
|
|
|
|
|
else void completePath(false);
|
2026-10-07 13:53:48 +08:00
|
|
|
}
|
2026-10-08 10:06:31 +08:00
|
|
|
}} /> : <nav ref={attachBreadcrumb} aria-label={t("workspace.picker.location")} className="flex min-w-0 flex-1 items-center overflow-x-auto rounded-control bg-background/80 px-1 [scrollbar-width:none]">
|
|
|
|
|
{breadcrumbs.map((crumb, index) => <span key={crumb.path} className="flex shrink-0 items-center">
|
|
|
|
|
{index > 0 && <ChevronRight className="h-3 w-3 shrink-0 text-muted-foreground/60" />}
|
|
|
|
|
<WorkspacePickerTooltip label={crumb.path}>
|
|
|
|
|
<button type="button" disabled={pickingFolder} aria-current={index === breadcrumbs.length - 1 ? "location" : undefined} onClick={() => navigate(crumb.path)}
|
|
|
|
|
className="h-11 max-w-48 truncate rounded-control px-2 text-[12px] text-muted-foreground outline-none hover:bg-foreground/[0.055] focus-visible:ring-2 focus-visible:ring-ring aria-[current=location]:font-medium aria-[current=location]:text-foreground sm:h-9">{crumb.name}</button>
|
|
|
|
|
</WorkspacePickerTooltip>
|
|
|
|
|
</span>)}
|
|
|
|
|
</nav>}
|
|
|
|
|
{canBrowse && <PickerToolButton label={t(editingPath ? "workspace.picker.cancelPath" : "workspace.picker.editPath")} disabled={pickingFolder} onClick={editingPath ? cancelPathEditing : editPath}>
|
|
|
|
|
{editingPath ? <X className="h-4 w-4" /> : <Pencil className="h-3.5 w-3.5" />}
|
|
|
|
|
</PickerToolButton>}
|
2026-05-29 03:42:53 +08:00
|
|
|
</div>
|
2026-10-08 10:06:31 +08:00
|
|
|
{compact && filterToolbar}
|
|
|
|
|
<div className="flex min-h-0 flex-1 bg-background">
|
|
|
|
|
{(!compact || showLocations || !canBrowse) && <nav aria-label={t("workspace.picker.shortcuts")} className={cn("flex shrink-0 flex-col gap-4 overflow-auto bg-sidebar p-2", compact && canBrowse ? "w-full" : "w-60 border-r border-border/60", compact && !canBrowse && "w-40")}>
|
2026-10-07 13:53:48 +08:00
|
|
|
{[{ label: t("workspace.picker.favorites"), projects: favorites, favorite: true }, { label: t("workspace.picker.recent"), projects: recentProjects, favorite: false }].map(section => (
|
2026-10-08 10:06:31 +08:00
|
|
|
<section key={section.label} className="min-w-0 shrink-0">
|
2026-10-07 13:53:48 +08:00
|
|
|
<h3 className="px-3 py-2 text-[11px] font-medium text-muted-foreground">{section.label}</h3>
|
2026-10-07 14:47:16 +08:00
|
|
|
{section.projects.map(project => <div key={project.path} className="workspace-picker-row relative">
|
2026-10-08 10:06:31 +08:00
|
|
|
<WorkspacePickerTooltip label={project.path}>
|
|
|
|
|
<button type="button" aria-label={project.path} aria-current={sameWorkspacePath(currentPath, project.path) ? "location" : undefined}
|
|
|
|
|
disabled={pickingFolder} onMouseDown={event => event.preventDefault()} onClick={() => openShortcut(project)}
|
|
|
|
|
className={cn(floatingItemClassName, floatingItemFocusClassName, "flex min-h-12 w-full min-w-0 items-center gap-2 px-3 py-2 text-left hover:bg-foreground/[0.055] dark:hover:bg-white/[0.08]", canFavorite && "pr-12", sameWorkspacePath(currentPath, project.path) && "bg-primary/10 text-primary")}>
|
|
|
|
|
<WorkspaceIcon className="h-4 w-4 shrink-0 text-muted-foreground" />
|
|
|
|
|
<WorkspaceSavedProjectLabel path={project.path} />
|
|
|
|
|
{sameWorkspacePath(project.path, scope?.project_path ?? defaultScope.project_path) && <Check className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />}
|
|
|
|
|
</button>
|
|
|
|
|
</WorkspacePickerTooltip>
|
2026-10-07 14:47:16 +08:00
|
|
|
{favoriteButton(project.path)}
|
|
|
|
|
</div>)}
|
2026-10-07 13:53:48 +08:00
|
|
|
{section.favorite && !favorites.length && canFavorite && <p className="px-3 text-[11px] leading-relaxed text-muted-foreground">{t("workspace.picker.favoriteHint")}</p>}
|
|
|
|
|
</section>
|
|
|
|
|
))}
|
2026-10-08 10:06:31 +08:00
|
|
|
</nav>}
|
|
|
|
|
<div className={cn("flex min-h-0 min-w-0 flex-1 flex-col", compact && showLocations && canBrowse && "hidden")}>
|
|
|
|
|
{!compact && filterToolbar}
|
|
|
|
|
<div ref={attachColumns} data-workspace-columns className="flex flex-1 min-h-0 min-w-0 overflow-x-auto overscroll-x-contain px-1 pb-2">
|
|
|
|
|
{columns.map((column, columnIndex) => (!compact || columnIndex === previousColumns.length) && <WorkspaceDirectoryColumn key={columnIndex} options={column.options} activeIndex={highlightActive && columnIndex === activeColumn ? activeOption : null}
|
|
|
|
|
id={`${optionsId}-${columnIndex}`} data-current-directory={columnIndex === previousColumns.length ? "" : undefined} role="listbox" tabIndex={0} aria-activedescendant={highlightActive && columnIndex === activeColumn && column.options.length ? `${optionsId}-${columnIndex}-${activeOption}` : undefined} aria-label={column.path || t("thread.composer.workspace.projectAria")} aria-busy={columnIndex === previousColumns.length && loading} style={{ width: `${compact ? 100 : 100 / columnCount}%` }} className={cn("min-w-0 shrink-0 overflow-x-hidden overflow-y-auto border-r border-transparent px-1 py-1 outline-none focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-ring", !!columns[columnIndex + 1]?.options.length && !compact && "border-border/50")}
|
|
|
|
|
onFocus={event => { if (event.target === event.currentTarget) { setActiveColumn(columnIndex); setActiveIndex(0); } }}
|
|
|
|
|
onKeyDown={event => {
|
|
|
|
|
if (event.target !== event.currentTarget || event.altKey || event.metaKey || event.ctrlKey || pickingFolder) return;
|
|
|
|
|
const count = column.options.length;
|
|
|
|
|
if (count && ["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
|
|
|
|
|
event.preventDefault(); setHighlightActive(true); setActiveColumn(columnIndex);
|
|
|
|
|
setActiveIndex(event.key === "Home" ? 0 : event.key === "End" ? count - 1 : highlightActive ? (activeIndex + (event.key === "ArrowDown" ? 1 : -1) + count) % count : event.key === "ArrowDown" ? 0 : count - 1);
|
|
|
|
|
} else if (count && (event.key === "Enter" || event.key === "ArrowRight")) {
|
|
|
|
|
event.preventDefault(); activate(column.options[Math.min(activeIndex, count - 1)], columnIndex);
|
|
|
|
|
} else if (event.key === "ArrowLeft" && parentPath) { event.preventDefault(); navigate(parentPath); }
|
|
|
|
|
}}
|
2026-10-07 15:12:58 +08:00
|
|
|
renderOption={(option, index) => <div role="presentation" className="workspace-picker-row relative" data-keyboard-active={highlightActive && columnIndex === activeColumn && index === activeOption ? "" : undefined}>
|
2026-10-08 10:06:31 +08:00
|
|
|
<button id={`${optionsId}-${columnIndex}-${index}`} type="button" role="option" tabIndex={-1} aria-label={option.path} aria-selected={sameWorkspacePath(column.selectedPath, option.path)} aria-posinset={index + 1} aria-setsize={column.options.length} disabled={pickingFolder}
|
2026-10-07 13:53:48 +08:00
|
|
|
onPointerMove={() => { setHighlightActive(false); setActiveColumn(columnIndex); setActiveIndex(index); }}
|
|
|
|
|
onMouseDown={event => event.preventDefault()} onClick={() => activate(option, columnIndex)}
|
2026-10-08 10:06:31 +08:00
|
|
|
className={cn(floatingItemClassName, floatingItemFocusClassName, "flex min-h-11 w-full min-w-0 items-center gap-2 px-3 py-2 text-left hover:bg-foreground/[0.055] dark:hover:bg-white/[0.08] disabled:opacity-50", canFavorite && "pr-12",
|
2026-10-07 13:53:48 +08:00
|
|
|
sameWorkspacePath(column.selectedPath, option.path) && "bg-primary/10 text-primary font-medium hover:bg-primary/10",
|
|
|
|
|
highlightActive && columnIndex === activeColumn && index === activeOption && "ring-1 ring-inset ring-ring")}>
|
2026-10-08 10:06:31 +08:00
|
|
|
<WorkspaceIcon className="h-4 w-4 shrink-0 text-muted-foreground" />
|
|
|
|
|
{option.kind === "directory" ? <WorkspacePickerTooltip label={option.path}><span className="min-w-0 flex-1 truncate text-[13px]">{option.name}</span></WorkspacePickerTooltip> : <WorkspacePickerPath path={option.path} />}
|
2026-10-07 13:53:48 +08:00
|
|
|
{sameWorkspacePath(option.path, scope?.project_path ?? defaultScope.project_path) && <Check className="h-4 w-4 shrink-0 text-muted-foreground" />}
|
2026-10-07 14:47:16 +08:00
|
|
|
</button>
|
|
|
|
|
{favoriteButton(option.path)}
|
2026-10-07 15:12:58 +08:00
|
|
|
</div>}>
|
2026-10-07 13:53:48 +08:00
|
|
|
{columnIndex === previousColumns.length && loading && <WorkspaceDirectorySkeleton />}
|
2026-10-08 10:06:31 +08:00
|
|
|
{columnIndex === previousColumns.length && canBrowse && !loading && directory && !column.options.length && !displayedError && <div role="status" className="flex flex-col items-center gap-2 px-4 py-10 text-center text-[12px] text-muted-foreground"><FolderSearch className="h-6 w-6 opacity-50" /><p>{t(folderQuery || directory.partial ? "workspace.picker.noMatches" : "workspace.picker.empty")}</p></div>}
|
2026-10-07 13:53:48 +08:00
|
|
|
{columnIndex === previousColumns.length && directory?.truncated && <p className="px-3 py-2 text-[11px] text-muted-foreground">{t("workspace.picker.truncated")}</p>}
|
2026-10-08 10:06:31 +08:00
|
|
|
{columnIndex === previousColumns.length && displayedError && <div className="px-3 py-3"><p id={errorId} role="alert" className="text-[12px] text-destructive">{displayedError}</p>{directoryError && <Button variant="ghost" size="sm" onClick={() => setRevision(value => value + 1)} className="mt-2">{t("workspace.picker.retry")}</Button>}</div>}
|
2026-10-07 15:12:58 +08:00
|
|
|
</WorkspaceDirectoryColumn>)}
|
2026-10-07 13:53:48 +08:00
|
|
|
</div>
|
2026-10-08 10:06:31 +08:00
|
|
|
</div>
|
2026-10-07 13:53:48 +08:00
|
|
|
</div>
|
2026-10-08 10:06:31 +08:00
|
|
|
<div className="flex shrink-0 flex-wrap items-center justify-end gap-x-3 gap-y-3 border-t border-border/70 bg-muted px-4 py-3">
|
|
|
|
|
{canBrowse && <label className="mr-auto flex w-full items-center gap-2 text-[12px] text-muted-foreground sm:w-auto"><ToggleButton checked={showHidden} onChange={setShowHidden} label={t("workspace.picker.hidden")} />{t("workspace.picker.hidden")}</label>}
|
|
|
|
|
<Button variant="ghost" size="sm" onClick={() => setOpen(false)} className="h-11 rounded-control px-4 sm:h-10">{t("workspace.picker.cancel")}</Button>
|
|
|
|
|
<Button size="sm" className="h-11 rounded-control px-4 sm:h-10" disabled={!selectionPath || pickingFolder || (editingPath && !absoluteDraft)} onClick={() => selectionPath && void chooseProject(selectionPath)}>{t("workspace.picker.select")}</Button>
|
2026-10-07 17:51:37 +08:00
|
|
|
</div>
|
2026-10-07 13:53:48 +08:00
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</TooltipProvider>
|
|
|
|
|
{error && !open && <span role="alert" className="ml-2 min-w-0 truncate text-[11.5px] font-medium text-destructive">{error}</span>}
|
2026-05-29 03:42:53 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 00:46:38 +08:00
|
|
|
export function WorkspaceAccessToggle({
|
2026-05-29 03:42:53 +08:00
|
|
|
scope,
|
|
|
|
|
disabled,
|
|
|
|
|
canUseFullAccess,
|
|
|
|
|
isHero,
|
|
|
|
|
onChange,
|
|
|
|
|
}: {
|
|
|
|
|
scope: WorkspaceScopePayload;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
canUseFullAccess: boolean;
|
|
|
|
|
isHero: boolean;
|
|
|
|
|
onChange?: (scope: WorkspaceScopePayload) => void;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const mode = scope.access_mode;
|
|
|
|
|
const isFull = mode === "full";
|
2026-07-23 18:18:34 +08:00
|
|
|
const accessLabel = t(
|
|
|
|
|
isFull ? "thread.composer.workspace.full" : "thread.composer.workspace.default",
|
|
|
|
|
);
|
|
|
|
|
const shortAccessLabel = t(
|
|
|
|
|
isFull ? "thread.composer.workspace.fullShort" : "thread.composer.workspace.defaultShort",
|
|
|
|
|
);
|
|
|
|
|
const accessAriaLabel = `${t("thread.composer.workspace.accessAria")}: ${accessLabel}`;
|
2026-05-29 03:42:53 +08:00
|
|
|
|
|
|
|
|
return (
|
2026-10-07 13:53:48 +08:00
|
|
|
<TooltipProvider>
|
|
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant={null}
|
|
|
|
|
aria-label={accessAriaLabel}
|
|
|
|
|
aria-pressed={isFull}
|
|
|
|
|
disabled={disabled || !onChange || (!isFull && !canUseFullAccess)}
|
|
|
|
|
onClick={() => onChange?.(scopeWithAccessMode(scope, isFull ? "restricted" : "full"))}
|
|
|
|
|
className={cn(
|
|
|
|
|
"settings-hover thread-composer-access touch-target min-w-0 max-w-[min(12.5rem,42vw)] whitespace-nowrap rounded-control border border-transparent font-semibold shadow-none",
|
|
|
|
|
isHero ? "h-8 px-2.5 text-[12px]" : "h-9 px-3 text-[12.5px]",
|
|
|
|
|
isFull
|
|
|
|
|
? "bg-transparent text-orange-600 hover:text-orange-600 dark:text-orange-300 dark:hover:text-orange-300"
|
|
|
|
|
: "bg-transparent text-muted-foreground hover:text-foreground",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{isFull ? (
|
|
|
|
|
<FullAccessIcon className={cn("thread-composer-access-icon mr-1.5 shrink-0", isHero ? "h-3.5 w-3.5" : "h-3.5 w-3.5")} />
|
|
|
|
|
) : (
|
|
|
|
|
<RestrictedAccessIcon className={cn("thread-composer-access-icon mr-1.5 shrink-0", isHero ? "h-3.5 w-3.5" : "h-3.5 w-3.5")} />
|
|
|
|
|
)}
|
|
|
|
|
<span aria-hidden className="thread-composer-access-label-full min-w-0 truncate">
|
|
|
|
|
{accessLabel}
|
|
|
|
|
</span>
|
|
|
|
|
<span aria-hidden className="thread-composer-access-label-short hidden min-w-0 truncate">
|
|
|
|
|
{shortAccessLabel}
|
|
|
|
|
</span>
|
|
|
|
|
</Button>
|
|
|
|
|
</TooltipTrigger>
|
|
|
|
|
<TooltipContent side="top" className="w-max max-w-[calc(100vw-2rem)] text-left leading-relaxed">
|
|
|
|
|
{t(isFull ? "thread.composer.workspace.fullDescription" : "thread.composer.workspace.defaultDescription")}
|
|
|
|
|
</TooltipContent>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</TooltipProvider>
|
2026-05-29 03:42:53 +08:00
|
|
|
);
|
|
|
|
|
}
|