import { createPortal } from 'react-dom' import { useState } from 'react' import { FileBrowser, useFsListing, type Listing } from './FileBrowser' /** * Pick a FOLDER, by browsing. * * The browsing itself is `FileBrowser` — the same one the collection picker * uses. All this adds is the modal frame and the confirm row, which is the part * that genuinely differs: here you are choosing somewhere to PUT files, so the * target can be a folder that does not exist yet. */ interface Props { value: string onChange: (path: string) => void onClose: () => void /** Whose folders: this computer's, or the server's holding a remote library. */ listing?: Listing } export function FolderPicker({ value, onChange, onClose, listing = 'host' }: Props) { // `undefined` means "wherever the server thinks home is" — the picker does not // need to know that path to open on it. const [dir, setDir] = useState(value || undefined) const [newFolder, setNewFolder] = useState('') // Shares FileBrowser's query, so "where am I" cannot disagree with what is // on screen — and the confirm row below needs it to name its target. const here = useFsListing(dir, undefined, listing).data?.path ?? '' const confirm = () => { onChange(newFolder.trim() ? `${here}/${newFolder.trim()}` : here) onClose() } return createPortal(

Choose a folder

New folder setNewFolder(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && newFolder.trim() && here) confirm() }} className="min-w-0 flex-1 rounded-md well px-2 py-1 text-xs text-text outline-none focus:ring-1 focus:ring-accent" />
} />
{/* Always show where the files will actually land, so the effect of typing a new-folder name never has to be inferred. */} {here ? (newFolder.trim() ? `${here}/${newFolder.trim()}` : here) : ''}
, document.body, ) }