import { FullAccessIcon, WorkspaceIcon, RestrictedAccessIcon } from "@/components/icons/product-icons"; import type { HTMLAttributes, ReactElement } from "react"; import { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react"; import { Check, Search, Star } from "lucide-react"; import { useTranslation } from "react-i18next"; import { ToggleButton } from "@/components/settings/ToggleButton"; import { Button } from "@/components/ui/button"; import { floatingItemClassName, floatingItemFocusClassName } from "@/components/ui/floating-surface"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; import { Input } from "@/components/ui/input"; 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"; import { cn } from "@/lib/utils"; import { isAbsoluteWorkspacePath, projectNameFromPath, sameWorkspacePath, scopeWithAccessMode, workspacePathCompletionQuery, workspaceDirectoryPrefix, selectedProjectScope, type BrowseWorkspaceDirectories } from "@/lib/workspace"; function WorkspaceFavoriteButton({ path, pinned, busy, onToggle }: { path: string; pinned: boolean; busy: boolean; onToggle: () => void; }) { const { t } = useTranslation(); const label = t(pinned ? "workspace.picker.unpin" : "workspace.picker.pin", { name: projectNameFromPath(path) }); return ( ); } type PickerColumn = { path: string; options: ProjectDirectory[]; selectedPath: string | null; hidden: boolean }; function WorkspacePickerTooltip({ label, children }: { label: string; children: ReactElement }) { return ( {children} {label} ); } const DIRECTORY_ROW_HEIGHT = 44; const DIRECTORY_OVERSCAN = 4; function WorkspaceDirectoryColumn({ options, activeIndex, renderOption, children, ...props }: { options: ProjectDirectory[]; activeIndex: number | null; renderOption: (option: ProjectDirectory, index: number) => ReactElement; } & HTMLAttributes) { const viewportRef = useRef(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 (
setFirstRow(Math.floor(event.currentTarget.scrollTop / DIRECTORY_ROW_HEIGHT))}>
{options.slice(start, end).map((option, offset) => { const index = start + offset; return
{renderOption(option, index)}
; })}
{children}
); } function WorkspaceDirectorySkeleton() { const { t } = useTranslation(); const viewportRef = useRef(null); const [rows, setRows] = useState(1); useLayoutEffect(() => { const viewport = viewportRef.current; if (!viewport) return; const measure = () => setRows(Math.max(1, Math.ceil(viewport.clientHeight / DIRECTORY_ROW_HEIGHT))); measure(); const observer = new ResizeObserver(measure); observer.observe(viewport); return () => observer.disconnect(); }, []); const widths = ["64%", "78%", "52%", "70%", "60%", "74%"]; return (
{t("workspace.picker.loading")}
); } function WorkspacePickerPath({ path }: { path: string }) { const parts = workspacePathCompletionQuery(path); return ( {parts?.path ?? path} {parts?.query && {parts.query}} ); } export function WorkspaceProjectPicker({ isHero, disabled, scope, defaultScope, controls, error, onLoadProjects, onResolveProject, onFavoriteProject, onBrowseDirectories, layoutAnchor, onChange }: { layoutAnchor?: HTMLElement | null; isHero: boolean; disabled?: boolean; scope: WorkspaceScopePayload | null; defaultScope: WorkspaceScopePayload | null; controls: WorkspacesPayload["controls"] | null; error?: string | null; onResolveProject: (path: string) => Promise; onLoadProjects: () => Promise; onFavoriteProject: (path: string, pinned: boolean) => Promise; onBrowseDirectories: BrowseWorkspaceDirectories; onChange?: (scope: WorkspaceScopePayload) => void; }) { const { t } = useTranslation(); const [open, setOpen] = useState(false); const [pathDraft, setPathDraft] = useState(""); const [basePath, setBasePath] = useState(scope?.project_path ?? defaultScope?.project_path ?? ""); const [catalog, setCatalog] = useState(null); const [directory, setDirectory] = useState(null); const [loading, setLoading] = useState(false); const [showHidden, setShowHidden] = useState(false); const [revision, setRevision] = useState(0); const [directoryError, setDirectoryError] = useState(null); const [pathError, setPathError] = useState(null); const [pickingFolder, setPickingFolder] = useState(false); const [favoriteBusy, setFavoriteBusy] = useState(false); const [activeIndex, setActiveIndex] = useState(0); const [activeColumn, setActiveColumn] = useState(0); const [highlightActive, setHighlightActive] = useState(false); const [previousColumns, setPreviousColumns] = useState([]); const columnsRef = useRef(null); const [columnsElement, setColumnsElement] = useState(null); const attachColumns = useCallback((element: HTMLDivElement | null) => { columnsRef.current = element; setColumnsElement(element); }, []); const [visibleColumns, setVisibleColumns] = useState(3); const pickerSession = useRef(0); const completionRequest = useRef(0); const triggerRef = useRef(null); const keyboardInteraction = useRef(false); const inputRef = useRef(null); const optionsId = useId(); const errorId = useId(); const currentProjectScope = selectedProjectScope(scope, defaultScope); const displayedScope = scope ?? defaultScope; const projectLabel = displayedScope ? displayedScope.project_name || projectNameFromPath(displayedScope.project_path) : ""; const visible = isHero && !!defaultScope && !!onChange && controls?.can_change_project !== false; const directoryCache = useMemo(() => createWorkspaceDirectoryCache(onBrowseDirectories), [onBrowseDirectories]); const absoluteDraft = isAbsoluteWorkspacePath(pathDraft); const requestedPath = absoluteDraft ? pathDraft.trim() : basePath; const folderQuery = absoluteDraft ? "" : pathDraft.trim(); useEffect(() => { pickerSession.current += 1; completionRequest.current += 1; if (!open) return; setPreviousColumns([]); setActiveColumn(0); setHighlightActive(false); const initialPath = scope?.project_path ?? defaultScope?.project_path ?? ""; setPathDraft(workspaceDirectoryPrefix(initialPath)); setBasePath(initialPath); setDirectory(null); setActiveIndex(0); setPathError(null); }, [scope?.project_path, defaultScope?.project_path, open]); useEffect(() => { if (!open) return; 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]); useEffect(() => { if (!open) return; let active = true; const apply = (result: WorkspaceDirectoriesPayload) => { if (!active) return; setDirectory(result); setLoading(false); if (absoluteDraft) setBasePath(result.path); }; setDirectoryError(null); const cached = directoryCache.peek(requestedPath, folderQuery, showHidden, true); if (cached) { apply(cached); return; } setLoading(true); setDirectory(null); const typing = !!folderQuery || absoluteDraft && !/[\\/]$/.test(pathDraft); 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); }; }, [open, directoryCache, requestedPath, folderQuery, showHidden, revision, absoluteDraft, pathDraft]); useEffect(() => { if (disabled) setOpen(false); }, [disabled]); useEffect(() => { if (!error || !visible || disabled) return; const frame = window.requestAnimationFrame(() => triggerRef.current?.focus()); return () => window.cancelAnimationFrame(frame); }, [disabled, error, visible]); useEffect(() => { if (!open || pickingFolder) return; const frame = window.requestAnimationFrame(() => inputRef.current?.focus()); return () => window.cancelAnimationFrame(frame); }, [open, pickingFolder, pathError]); const ancestorPaths = JSON.stringify(previousColumns.filter(column => column.hidden !== showHidden).map(column => column.path)); useEffect(() => { if (!open) return; 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)); if (!result) return column; return { ...column, hidden: showHidden, options: result.entries }; })); }).catch((err: Error) => { if (active) setPathError(err.message); }); return () => { active = false; }; }, [open, directoryCache, ancestorPaths, showHidden]); useLayoutEffect(() => { const viewport = columnsElement; if (!open || !viewport) return; const measure = () => setVisibleColumns(Math.max(1, Math.min(3, Math.floor(viewport.clientWidth / 240)))); measure(); const observer = new ResizeObserver(measure); observer.observe(viewport); return () => observer.disconnect(); }, [open, columnsElement]); useEffect(() => { 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]); useLayoutEffect(() => { const viewport = columnsRef.current; if (viewport) viewport.scrollLeft = viewport.clientWidth * Math.max(0, previousColumns.length + 1 - visibleColumns) / visibleColumns; }, [previousColumns.length, visibleColumns]); useLayoutEffect(() => { const viewport = columnsRef.current; if (!viewport || !highlightActive) return; const width = viewport.clientWidth / visibleColumns; 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; }, [activeColumn, highlightActive, visibleColumns]); function changeDraft(value: string, resetColumns = true) { setHighlightActive(false); if (resetColumns) { setPreviousColumns([]); setActiveColumn(0); } completionRequest.current += 1; setPathDraft(value); const absolute = isAbsoluteWorkspacePath(value); const cached = directoryCache.peek(absolute ? value.trim() : basePath, absolute ? "" : value.trim(), showHidden, true); setDirectory(cached ?? null); setLoading(!cached); setDirectoryError(null); setPathError(null); setActiveIndex(0); } function navigate(path: string, columnIndex = activeColumn) { const source = columns[columnIndex]; const ancestors = columns.slice(0, columnIndex); if (source?.options.length) ancestors.push({ ...source, selectedPath: path }); setPreviousColumns(ancestors); setActiveColumn(ancestors.length); setBasePath(path); changeDraft(workspaceDirectoryPrefix(path), false); setRevision(value => value + 1); inputRef.current?.focus(); } const applyProjectPath = useCallback((path: string, name: string) => { const base = scope ?? defaultScope; const trimmed = path.trim(); if (!base || !onChange) return; const accessMode = controls?.can_use_full_access === false && !sameWorkspacePath(trimmed, base.project_path) ? "restricted" : base.access_mode; onChange({ ...base, project_path: trimmed, project_name: name, access_mode: accessMode, restrict_to_workspace: accessMode === "restricted" }); setPathError(null); setOpen(false); }, [controls?.can_use_full_access, defaultScope, onChange, scope]); const chooseProject = useCallback(async (path: string) => { const session = pickerSession.current; 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); } }, [applyProjectPath, onResolveProject]); const favorites = catalog?.favorite_projects ?? []; const canFavorite = catalog !== null; const isFavorite = (path: string) => favorites.some(item => sameWorkspacePath(item.path, path)); function favoriteButton(path: string) { return canFavorite ? { void toggleFavorite(path); inputRef.current?.focus(); }} /> : null; } const toggleFavorite = async (path: string) => { if (favoriteBusy) return; 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 ?? []), ...(defaultScope ? [{ name: projectNameFromPath(defaultScope.project_path), path: defaultScope.project_path }] : []), ...(currentProjectScope ? [{ name: projectLabel, path: currentProjectScope.project_path }] : []), ].filter((project, index, all) => all.findIndex(item => sameWorkspacePath(item.path, project.path)) === index && !isFavorite(project.path)); const options = useMemo(() => directory && !loading ? directory.entries : [], [directory, loading]); function openShortcut(project: ProjectDirectory) { setBasePath(project.path); changeDraft(workspaceDirectoryPrefix(project.path)); setRevision(value => value + 1); inputRef.current?.focus(); } 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)); const selectionPath = !loading && directory && !directory.partial ? directory.path : null; const displayedError = pathError ?? error ?? directoryError; async function completePath() { const option = activeOptions[activeOption]; if (option) { navigate(option.path); return; } const request = workspacePathCompletionQuery(pathDraft) ?? { path: basePath, query: pathDraft.trim() }; const id = ++completionRequest.current; const session = pickerSession.current; try { const result = await directoryCache.load(requestedPath, folderQuery, showHidden || request.query.startsWith("."), true); if (id !== completionRequest.current || session !== pickerSession.current) return; if (absoluteDraft && !result.partial && request.query) { navigate(result.path); return; } const prefix = request.query.toLocaleLowerCase(); const first = result.entries.find(entry => entry.name.toLocaleLowerCase().startsWith(prefix)); if (first) navigate(first.path); } catch (err) { if (id === completionRequest.current && session === pickerSession.current) setPathError((err as Error).message); } } function activate(option: ProjectDirectory, columnIndex = activeColumn) { if (pickingFolder) return; navigate(option.path, columnIndex); } if (!visible || !defaultScope || !onChange) return null; return (
{ event.preventDefault(); inputRef.current?.focus(); }} onPointerDownCapture={() => { keyboardInteraction.current = false; }} onKeyDownCapture={() => { keyboardInteraction.current = true; }} onCloseAutoFocus={event => { if (!keyboardInteraction.current) event.preventDefault(); }}> {t("thread.composer.workspace.projectAria")} {t("workspace.picker.description")}
changeDraft(event.target.value)} placeholder={t("workspace.picker.search")} aria-label={t("workspace.picker.search")} aria-invalid={displayedError || directoryError ? true : undefined} aria-describedby={displayedError || directoryError ? errorId : undefined} className="h-10 min-w-0 flex-1 border-0 bg-transparent pl-10 pr-3 text-[14px] shadow-none focus-visible:ring-0" onKeyDown={event => { if (event.nativeEvent.isComposing) return; if ((event.key === "ArrowDown" || event.key === "ArrowUp") && !event.altKey && activeOptions.length) { event.preventDefault(); const next = (activeOption + (event.key === "ArrowDown" ? 1 : -1) + activeOptions.length) % activeOptions.length; setHighlightActive(true); setActiveIndex(next); } else if (event.key === "ArrowLeft" && activeColumn > 0 && event.currentTarget.selectionStart === 0 && event.currentTarget.selectionEnd === 0) { event.preventDefault(); const parent = columns[activeColumn - 1]; setHighlightActive(true); setActiveColumn(activeColumn - 1); setActiveIndex(Math.max(0, parent.options.findIndex(option => sameWorkspacePath(option.path, parent.selectedPath)))); } else if (event.key === "Tab" && !event.shiftKey && !!pathDraft.trim() || event.key === "ArrowRight" && !event.shiftKey && event.currentTarget.selectionStart === pathDraft.length && event.currentTarget.selectionEnd === pathDraft.length) { event.preventDefault(); void completePath(); } else if (event.key === "Enter") { event.preventDefault(); if ((event.ctrlKey || event.metaKey) && selectionPath) void chooseProject(selectionPath); else if (activeOptions[activeOption]) activate(activeOptions[activeOption]); else if (selectionPath) void chooseProject(selectionPath); } }} />
{columns.map((column, columnIndex) =>
{favoriteButton(option.path)}
}> {columnIndex === previousColumns.length && loading && } {columnIndex === previousColumns.length && directory?.truncated &&

{t("workspace.picker.truncated")}

} {columnIndex === previousColumns.length && displayedError && }
)}
{error && !open && {error}}
); } export function WorkspaceAccessToggle({ 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"; 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}`; return ( {t(isFull ? "thread.composer.workspace.fullDescription" : "thread.composer.workspace.defaultDescription")} ); }