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.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { rm } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import {
|
||||
addToCollection,
|
||||
collectionImages,
|
||||
createCollection,
|
||||
deleteCollection,
|
||||
deleteGenerations,
|
||||
getCollection,
|
||||
getCollectionByName,
|
||||
listCollections,
|
||||
@@ -10,6 +13,7 @@ import {
|
||||
renameCollection,
|
||||
} from '../db';
|
||||
import { createRouter } from '../create-router';
|
||||
import { IMAGES_DIR } from '../env';
|
||||
import { openaiError, UUID_PATTERN } from '../errors';
|
||||
import { serializeCollection, serializeCollectionSummary, serializeGeneration } from '../serialize';
|
||||
|
||||
@@ -89,13 +93,44 @@ collectionsRouter.patch('/:id', async (ctx) => {
|
||||
return ctx.json({ data: updated ? serializeCollection(updated) : null });
|
||||
});
|
||||
|
||||
/** Removes the collection and its memberships. The images are untouched and stay in the gallery. */
|
||||
/**
|
||||
* Delete a collection. `?images=delete` deletes what was in it as well; the default keeps them.
|
||||
*
|
||||
* The two are genuinely different intentions and only the caller knows which one this is. "I filed
|
||||
* these wrongly" wants the folder gone and the images kept; "this whole afternoon was a dead end"
|
||||
* wants the images gone too, and doing that by hand is one delete per image. Which is why the
|
||||
* destructive one has to be asked for by name rather than inferred.
|
||||
*
|
||||
* Order matters: read the members BEFORE the collection goes, because deleting it takes the
|
||||
* memberships with it and there is then nothing left that says which images were in it.
|
||||
*
|
||||
* A file that will not unlink is not an error the caller can act on — the row is already gone and
|
||||
* retrying deletes nothing. It is left on disk and logged rather than failing a delete that, from
|
||||
* every side the user can see, succeeded.
|
||||
*/
|
||||
collectionsRouter.delete('/:id', async (ctx) => {
|
||||
const id = ctx.req.param('id');
|
||||
if (!UUID_PATTERN.test(id) || !(await getCollection(id))) return ctx.json(notFound(id), 404);
|
||||
|
||||
const alsoImages = ctx.req.query('images') === 'delete';
|
||||
const members = alsoImages ? await collectionImages(id) : [];
|
||||
|
||||
await deleteCollection(id);
|
||||
return ctx.json({ data: { id, deleted: true } });
|
||||
|
||||
let deletedImages = 0;
|
||||
if (alsoImages && members.length > 0) {
|
||||
const removed = await deleteGenerations(members.map((row) => row.id));
|
||||
deletedImages = removed.length;
|
||||
|
||||
for (const row of removed) {
|
||||
if (!row.imagePath) continue;
|
||||
await rm(join(IMAGES_DIR, row.imagePath), { force: true }).catch((err: unknown) => {
|
||||
console.error(`[atelier] could not unlink ${row.imagePath}:`, err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.json({ data: { id, deleted: true, deletedImages } });
|
||||
});
|
||||
|
||||
/** Add images. Ones already in the collection are counted as added and change nothing. */
|
||||
|
||||
@@ -20,6 +20,18 @@ export async function listGenerations(limit = 50): Promise<Generation[]> {
|
||||
return db.select().from(generations).orderBy(desc(generations.createdAt)).limit(limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete generations outright, returning the rows that went — the caller needs `image_path` to unlink
|
||||
* the files, and after the delete there is nowhere left to read it from.
|
||||
*
|
||||
* `collection_items` rows go with them by foreign key, so an image deleted while filed in three
|
||||
* collections leaves none of the three holding a dangling membership.
|
||||
*/
|
||||
export async function deleteGenerations(ids: string[]): Promise<Generation[]> {
|
||||
if (ids.length === 0) return [];
|
||||
return db.delete(generations).where(inArray(generations.id, ids)).returning();
|
||||
}
|
||||
|
||||
/** Every image of one request, in the order it was asked for. This is what polling a job reads. */
|
||||
export async function getRequest(requestId: string): Promise<Generation[]> {
|
||||
return db.select().from(generations).where(eq(generations.requestId, requestId)).orderBy(asc(generations.index));
|
||||
|
||||
Reference in New Issue
Block a user