feat(admin): offer background removal where an item's photos are edited (#293)

One button per item in the inventory editor, beside the per-thumbnail delete buttons rather than on them, because an upload is one item and its photos are views of one thing.

Its label is derived from the images rather than stored: Restore originals when every photo already carries an original, Remove backgrounds otherwise. The otherwise deliberately covers the mixed state a partial failure leaves behind — with two of four cut out it reads Remove backgrounds, which is the action that finishes the job, and pressing it skips the two that already worked.

Rendering is gated on the feature being configured or every photo already being cut out, not on the flag alone. Gating on the flag would hide Restore originals the moment REMBG_URL is unset, stranding cut-out photos with no way back — the same reasoning the review queue's control already uses.

The editor is a modal and this changes files on the server while it is open, so the item is re-read afterwards and the open modal updated. Without that the thumbnails keep showing the previous files and the button looks like it did nothing, which is the bug this was most likely to ship with.

frontend/src/api.ts gains original_image_path on the shared Item type's images, since ADMIN_ITEM_SELECT's images aggregate carries it and the public catalogue's does not. It is added as optional rather than required because Item is the same type fetchItems() uses for the public storefront, and a required field the public response never sends would be a type that lies about what is actually there.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-04 10:36:00 -05:00
co-authored by Claude Opus 5
parent 8f35204995
commit 385d5b89bf
3 changed files with 134 additions and 2 deletions
@@ -0,0 +1,54 @@
import { test, expect, createAdminContext, uniqueSuffix } from './fixtures';
/**
* The per-item background control in the inventory editor (#293).
*
* Asserts the control is offered, not that a cut-out happens. Pressing it needs
* a live rembg sidecar, which takes forty seconds to start and which no test
* should depend on — the swap itself is covered in the integration suite
* against a stub.
*
* "Does not appear when the feature is unconfigured" is not written here as an
* e2e case: unsetting REMBG_URL means restarting the backend mid-suite, which
* this run has no way to do and should not gain one. GET /api/admin/config
* answering `backgroundRemoval: false` is covered by Task 2's integration
* tests, and the render condition that gates the button on it is plain enough
* to read in Admin.tsx.
*/
const RUN = uniqueSuffix();
const NAME = `Vase ${RUN}`;
// The 1x1 PNG the other upload specs use, so this goes through the real
// validated upload path rather than a buffer that merely starts correctly.
const PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
'base64'
);
test.beforeAll(async ({ playwright }) => {
const api = await createAdminContext(playwright);
// Seeded with an image directly: createItem cannot attach one, and the
// control does not render for an item with no photos.
const res = await api.post('/api/admin/items', {
multipart: {
name: NAME,
description: '',
price: '50',
category_id: '',
tags: '[]',
images: { name: `${RUN}.png`, mimeType: 'image/png', buffer: PNG }
}
});
expect(res.ok(), `seeding ${NAME}`).toBeTruthy();
await api.dispose();
});
test.describe('Removing backgrounds from the item editor', () => {
test('offers the control on an item that has photos', async ({ page, admin }) => {
await admin.open('Inventory');
await page.getByRole('row', { name: new RegExp(NAME) }).getByRole('button', { name: 'Edit' }).click();
await expect(page.getByRole('button', { name: 'Remove backgrounds' })).toBeVisible();
});
});