Feature/293 remove backgrounds from inventory #296

Merged
bermudalamb merged 9 commits from feature/293-remove-backgrounds-from-inventory into main 2026-09-04 12:20:01 -05:00
Showing only changes of commit dbb63bc3d2 - Show all commits
+16 -9
View File
@@ -85,11 +85,15 @@ function Inventory() {
return fetchAdminItems(active)
.then(rows => {
if (seq === latestRequest.current) setItems(rows);
// Handed back so a caller that needs one fresh row (handleBackgrounds)
// can pick it out of this filtered fetch instead of issuing its own
// second, unfiltered one.
return rows;
})
// Without this the table simply keeps showing whatever it had, so a
// failed refetch after a save looks identical to a save that did not
// change anything.
.catch(() => message.error('Could not load items'));
.catch(() => { message.error('Could not load items'); return undefined; });
}, [filters]);
// The item form needs the current category tree and tag list; both change
@@ -213,6 +217,7 @@ function Inventory() {
* showing the previous files and the button looks like it did nothing.
*/
async function handleBackgrounds(itemId: number, action: 'remove-backgrounds' | 'restore-originals') {
const label = action === 'remove-backgrounds' ? 'remove backgrounds' : 'restore originals';
setBusyBackgrounds(true);
try {
const res = await fetch(`/api/admin/items/${itemId}/${action}`, { method: 'POST' });
@@ -231,14 +236,16 @@ function Inventory() {
message.success('Done.');
}
// Re-read the item so the thumbnails match what is now on the server.
const fresh = await fetch('/api/admin/items');
if (fresh.ok) {
const all: Item[] = await fresh.json();
const updated = all.find((candidate) => candidate.id === itemId);
if (updated) setEditingItem(prev => (prev && prev.id === itemId ? updated : prev));
setItems(all);
}
// Re-read the list through load() — same as every other mutation here —
// so a filtered view survives this, then pick this item's fresh images
// back out of it for the open modal. The item's own filtered fields
// (category, tags, status, search) are untouched by a background swap,
// so it stays in the result whenever it was in it before.
const rows = await load();
const updated = rows?.find(candidate => candidate.id === itemId);
if (updated) setEditingItem(prev => (prev && prev.id === itemId ? updated : prev));
} catch (err) {
message.error(`Couldn't ${label}${(err as Error).message}`);
} finally {
setBusyBackgrounds(false);
}