'use client' import { useEffect, useState } from 'react' import { createPortal } from 'react-dom' import { ArrowUp, ChevronRight, Folder } from 'lucide-react' import { useTranslation } from 'react-i18next' import { Dialog } from '@/shared/ui/Dialog' import { browseServerFolders, type ServerFolder, type ServerFolderListing, } from '@/lib/workspaces-api' /** Browse the backend filesystem without requiring a manually entered path. */ export function ServerFolderPicker({ onClose, onSelect, }: { onClose: () => void onSelect: (folder: ServerFolder) => void | Promise }) { const { t } = useTranslation() const [path, setPath] = useState('') const [listing, setListing] = useState(null) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [error, setError] = useState('') const [filter, setFilter] = useState('') const [attempt, setAttempt] = useState(0) useEffect(() => { let active = true browseServerFolders(path) .then(data => { if (active) { setListing(data) setLoading(false) } }) .catch(err => { if (active) { setError(String(err.message)) setLoading(false) } }) return () => { active = false } }, [path, attempt]) const navigate = (next: string) => { if (next === path) return setLoading(true) setError('') setFilter('') setPath(next) } const choose = async () => { if (!listing || loading || saving) return setSaving(true) setError('') try { await onSelect({ name: listing.name, path: listing.path }) onClose() } catch (err) { setError(err instanceof Error ? err.message : String(err)) } finally { setSaving(false) } } const currentRoot = listing?.roots .filter(root => { const current = listing.path.replaceAll('\\', '/') const prefix = root.path.replaceAll('\\', '/').replace(/\/$/, '') return current === prefix || current.startsWith(prefix + '/') }) .sort((a, b) => b.path.length - a.path.length)[0] return createPortal( } >
{listing && ( <>

{listing.path}

{listing.roots.length > 1 && ( )} )} setFilter(event.target.value)} className="w-full rounded-lg border border-[var(--border)] bg-[var(--background)] px-3 py-2 text-sm" /> {error && (

{error}

)}
{loading ? (

{t('Loading folders…')}

) : ( <> {listing?.folders .filter(folder => folder.name.toLocaleLowerCase().includes(filter.toLocaleLowerCase()) ) .map(folder => ( ))} {!listing?.folders.some(folder => folder.name.toLocaleLowerCase().includes(filter.toLocaleLowerCase()) ) && !error && (

{t('No subfolders')}

)} )}
{listing?.truncated && (

{t('Showing the first 500 folders. Open a subfolder to narrow the list.')}

)}
, document.body ) }