import { useCallback, useEffect, useRef, useState } from 'react'; import { Check, FolderOpen, Loader2 } from 'lucide-react'; import { useClient } from 'hooks/useClient'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { readableError } from '../errors'; import { useFlash } from '../useFlash'; import { RichText } from '../RichText'; import { MediaPicker } from '../MediaPicker'; import { useAutosave } from '../useAutosave'; /** * The Home page: a hero, an intro, and an ordered gallery. * * ── It edits DEVELOPMENT, and that is the whole model ── * * Every request here goes through this plugin to the development server's `/api/admin`. Production is * a directory of static files with no API at all, so there is nothing to write to there — an edit * lands on dev immediately and becomes public when somebody presses Build. Saying that once, here, is * better than a person discovering it when a change does not appear on the live site. * * ── The server owns the shape, and every write returns it ── * * `PATCH /home` and every gallery route answer with the WHOLE payload, so this never patches its own * state from what it sent. That matters more than it looks: the server clears the hero crop when the * hero changes, and a client that kept its own copy would go on showing a framing the server had * already discarded. * * ── Ordering is a list, not a field ── * * `PUT /gallery/order` takes every id in the order they should be. So moving one item sends all of * them, which is what makes it impossible to end up with two things claiming the same position. */ type GalleryItem = { id: number; kind: string; url: string; crop: string | null; fit: string | null; sort_order: number; alt: string | null; }; type Home = { hero: { url: string | null; crop: string | null; fit: string | null }; intro_text: string | null; /** The OLD per-page list. Still returned by the API, and only rendered while no gallery is chosen. */ gallery: GalleryItem[]; /** Which gallery the carousel shows. Null means the old list above still applies. */ gallery_id: number | null; }; type Gallery = { id: number; name: string; slug: string; items: unknown[] }; export const HomePanel = () => { const client = useClient(); const clientRef = useRef(client); clientRef.current = client; const [home, setHome] = useState(null); const [heroUrl, setHeroUrl] = useState(''); const [intro, setIntro] = useState(''); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [saved, flashSaved] = useFlash(); const [picking, setPicking] = useState(false); const [galleries, setGalleries] = useState([]); /** Every write answers with the whole payload, so this is the only place state is set. */ const apply = useCallback((data: Home) => { setHome(data); setHeroUrl(data.hero.url ?? ''); setIntro(data.intro_text ?? ''); }, []); const load = useCallback(async () => { try { const response = await clientRef.current.get<{ data: Home }>('/fofos-admin/home'); apply(response.data); } catch (failure) { setError(readableError(failure)); } }, [apply]); useEffect(() => { void load(); }, [load]); // The galleries to choose between, read from the section that owns them rather than embedded in the // home payload — a copy there would go stale the moment somebody edited one. useEffect(() => { void clientRef.current .get<{ data: Gallery[] }>('/fofos-admin/galleries') .then((response) => setGalleries(response.data)) .catch(() => setGalleries([])); }, []); const send = async (label: string, call: () => Promise<{ data: Home }>) => { setBusy(label); setError(null); try { apply((await call()).data); flashSaved(); } catch (failure) { setError(readableError(failure)); } finally { setBusy(null); } }; /* One PATCH per card, carrying only that card's field. `take()` on the server treats an absent key as "leave it alone", so sending the whole object from either card would have each one saving the other's unsaved edits. Two cards, two independent saves, and a half-finished intro cannot ride along with a hero somebody meant to change. */ /* One Save for everything this page holds in a field. The gallery choice is NOT part of it: picking a gallery from the list writes immediately, because a void send('gallery', () => clientRef.current.patch<{ data: Home }>('/fofos-admin/home', { gallery_id: event.target.value ? Number(event.target.value) : null, }), ) } disabled={busy !== null} className="w-full rounded-md border border-border bg-background px-3 py-2 text-xs" > {galleries.map((gallery) => ( ))} {/* Only while it is true: the old rows are what renders until a gallery is chosen, and saying so beats a carousel that appears to come from nowhere. */} {home.gallery_id === null && home.gallery.length > 0 && (

Showing {home.gallery.length} item{home.gallery.length === 1 ? '' : 's'} from the old per-page list. Choosing a gallery replaces it.

)} ); };