Files
atelier/web/Gallery/useGalleryManager.ts
pastilhas 82ca696ccf refactor(web): two panels on the left, the work on the right
Four panels became three, and the arrangement carries the change: everything
that organises or configures is stacked in one narrow column, and the whole of
the rest is one surface for looking at images.

Collections is a LIST, not a grid of covers — it sits in a narrow column beside
the gallery, where two cards per row meant four collections visible. A row is a
name, a count, and rename/delete on hover. Create is a dialog rather than an
always-present input, and a filter field replaces it in that space, since
finding one is the frequent act and making one is not.

Selecting a collection now shows it in the GALLERY. It used to replace the list
inside its own panel, which meant the selector disappeared the moment it was
used, and both panels kept their own copy of one collection's images and
disagreed after any edit. `?collection=` is read by the gallery now; the
collections hook stopped fetching detail entirely.

Deleting asks the question that cannot be undone: keep the images, or delete
them too. That needed a server route — DELETE /collections/:id?images=delete —
which reads the members BEFORE the collection goes, since deleting it takes the
memberships with it. Unchecked by default: the recoverable answer is the one
you get by pressing Enter without reading.

Providers stopped being a panel. It was a card per provider, each with a key
field and three buttons, filling a quarter of the screen for something set once
— it is a dropdown at the top of Generate now, listing "adapter · model" with a
delete on each, and adding one opens a dialog. The form and the dropdown share
one provider list (useProviders); they were two fetches, so adding a provider
left the form showing the old models until a reload.
2026-08-20 21:34:34 +01:00

64 lines
2.5 KiB
TypeScript

import { useCallback, useEffect, useState } from 'react';
import { useAtelierRefresh } from 'officerdev';
import { useAtelierClient } from '../lib/api';
import type { CollectionDetail, GenerationRow } from '../lib/api';
/**
* What the main panel is showing: everything, or one collection.
*
* One hook for both, because the panel below it is the same grid either way and the only difference is
* where the rows came from. The collection case used to live in the Collections panel, which meant two
* panels each holding their own copy of a collection's images and disagreeing after any edit.
*/
export const useGalleryManager = (collectionId?: string) => {
const client = useAtelierClient();
const [rows, setRows] = useState<GenerationRow[]>([]);
const [collection, setCollection] = useState<CollectionDetail | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [refreshSignal] = useAtelierRefresh();
const refresh = useCallback(async () => {
try {
if (collectionId) {
const detail = await client.getCollection(collectionId);
setCollection(detail);
setRows(detail.images);
} else {
setCollection(null);
setRows(await client.listGenerations());
}
setError(null);
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
} finally {
setLoading(false);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [collectionId]);
useEffect(() => {
setLoading(true);
void refresh();
}, [refresh]);
// A finished generation elsewhere on the dashboard (the Generate panel) bumps this — re-read.
useEffect(() => {
if (refreshSignal > 0) void refresh();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [refreshSignal]);
/** Only meaningful in collection mode: takes the image out of this collection, not out of Atelier. */
const removeFromCollection = async (generationId: string) => {
if (!collectionId) return;
const { images } = await client.removeFromCollection(collectionId, generationId);
setRows(images);
setCollection((current) => (current ? { ...current, images } : current));
};
const succeeded = rows.filter((row) => row.status === 'succeeded' && row.image_path);
const totalCost = rows.reduce((sum, row) => sum + Number(row.cost_usd ?? 0), 0);
return { rows, succeeded, totalCost, collection, loading, error, refresh, removeFromCollection };
};