import { useCallback, useEffect, useRef, useState } from 'react'; import { useSearchParams } from 'react-router'; import { Boxes, Images, Loader2, Plus, Trash2 } from 'lucide-react'; import { useClient } from 'hooks/useClient'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { GalleryPicker } from '../GalleryPicker'; import { readableError } from '../errors'; import { useEditorSlot } from '../useEditorSlot'; import { thumbUrl } from '../thumbnail'; import { CollectionFields } from './CollectionFields'; import type { Collection, CollectionPhoto } from './collections'; /** * Collections — one folder of photographs each, and what is said about it. * * ── The list is the DISK ── * * Which collections exist is a question about `originals/COLLECTIONS/`, not about a table. The API * registers every folder as it reads them, so a collection appears here the moment its folder does and * there is no "create" button: making one means making a folder, in Media or in Finder, and both work. * * Deleting is the same in reverse and is deliberately not offered. A button here would either remove a * row and leave the photographs — so the collection reappears on the next read — or delete somebody's * pictures. Neither is what a Delete button in an admin should mean. * * ── The grid is for choosing the COVER ── * * The photographs are already ordered by their filenames and the gallery is the folder, so there is * nothing to reorder and nothing to add. The one decision is which picture leads, and that is a click. */ export const CollectionsPanel = () => { const client = useClient(); const clientRef = useRef(client); clientRef.current = client; const [collections, setCollections] = useState(null); const [photos, setPhotos] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [name, setName] = useState(''); const [picking, setPicking] = useState(false); // Selection in the URL, like every other screen here — see docs/navigation-audit.md. const [params, setParams] = useSearchParams(); const selectedId = Number(params.get('collection')) || null; const selected = collections?.find((one) => one.id === selectedId) ?? null; const load = useCallback(async () => { try { const response = await clientRef.current.get<{ data: Collection[] }>('/fofos-admin/collections'); setCollections(response.data); } catch (failure) { setError(readableError(failure)); setCollections([]); } }, []); useEffect(() => { void load(); }, [load]); /* A selection naming something that is not there any more. The id lives in the URL, so it outlives the row: delete a collection with it open, reload, and the address still asks for it. Left alone that became a 404 from the media fetch, rendered as "Not found" over a panel that was otherwise working — an error about the URL, phrased as an error about the server. Dropped rather than reported. There is nothing for anybody to do about it and the correct state is simply "nothing selected". */ useEffect(() => { if (!collections || !selectedId) return; if (collections.some((one) => one.id === selectedId)) return; setParams( (current) => { const next = new URLSearchParams(current); next.delete('collection'); return next; }, { replace: true }, ); }, [collections, selectedId, setParams]); // The gallery's photographs, re-read when the selection moves. Null while nothing is chosen, so the // empty state and "still loading" are not the same thing. useEffect(() => { if (!selectedId || !selected) { setPhotos(null); return; } let alive = true; setPhotos(null); // Cleared here rather than only on success: an error from the last selection has nothing to say // about this one, and leaving it up reads as the new one having failed too. setError(null); void clientRef.current .get<{ data: CollectionPhoto[] }>(`/fofos-admin/collections/${selectedId}/media`) .then((response) => alive && setPhotos(response.data)) .catch((failure: unknown) => alive && setError(readableError(failure))); return () => { alive = false; }; }, [selectedId, selected]); /* The fields, in the editor column — a SIBLING panel, so they are announced rather than rendered. See useEditorSlot. */ const [, setEditor] = useEditorSlot(); const setEditorRef = useRef(setEditor); setEditorRef.current = setEditor; useEffect(() => { setEditorRef.current( selected ? void load()} /> : null, ); }, [selected, load]); useEffect(() => () => setEditorRef.current(null), []); const select = (id: number) => setParams( (current) => { const next = new URLSearchParams(current); next.set('collection', String(id)); return next; }, { replace: true }, ); const send = async (call: () => Promise) => { setBusy(true); setError(null); try { await call(); await load(); } catch (failure) { setError(readableError(failure)); } finally { setBusy(false); } }; const create = () => send(async () => { const created = await clientRef.current.post<{ data: Collection }>('/fofos-admin/collections', { title: name.trim(), }); setName(''); // Straight to it: making one and then having to find it in the list is a step nobody wants. select(created.data.id); }); const remove = () => send(async () => { if (!selected) return; await clientRef.current.delete(`/fofos-admin/collections/${selected.id}`); setParams( (current) => { const next = new URLSearchParams(current); next.delete('collection'); return next; }, { replace: true }, ); }); const chooseGallery = (galleryId: number) => send(async () => { if (!selected) return; setPicking(false); await clientRef.current.patch(`/fofos-admin/collections/${selected.id}`, { gallery_id: galleryId }); }); return (
    {collections === null ? (
  • Reading the folders…
  • ) : collections.length === 0 ? (
  • No collections yet.
  • ) : ( collections.map((collection) => (
  • )) )}
{/* Creating is a name and a button, at the bottom of the list it adds to. A dialog for one field is a dialog nobody thanks you for. */}
setName(event.target.value)} onKeyDown={(event) => { if (event.key === 'Enter' && name.trim()) void create(); }} />
{error &&

{error}

} {selected && (
{/* Deleting removes the COLLECTION, never the photographs — they belong to a gallery, which belongs to Galleries. Said on the button so nobody has to find out. */}
)} {!selected ? (

Choose a collection, or make one.

) : !selected.gallery_id ? (

No gallery yet. Choose one and its photographs appear here.

) : photos === null ? (

Reading the gallery…

) : photos.length === 0 ? (

That gallery is empty. Add photographs to it in Galleries.

) : ( <>

{photos.length} photographs, in the gallery’s order — the first one leads. Reorder them in Galleries.

{/* Not buttons. Nothing here is a decision — the order is the gallery's and so is which one leads. Making them look pressable would promise a control that lives elsewhere. */}
    {photos.map((photo) => (
  • {photo.name}

  • ))}
)}
{/* `selected` guarded: the picker writes to whatever is open, and there is nothing to write to when nothing is. */} {selected && ( setPicking(false)} onPick={(galleryId) => void chooseGallery(galleryId)} selected={selected.gallery_id} /> )}
); };