WIP: bulkbalk boven de lijst, definitief verwijderen en prullenbak legen, uitsnede direct zonder opslaan, teller per batch

This commit is contained in:
2026-11-17 16:58:23 +01:00
parent 8ad071da1c
commit 1384cafa7b
28 changed files with 816 additions and 134 deletions
+36 -23
View File
@@ -24,7 +24,7 @@ import { UnsavedChangesGuard } from '../components/UnsavedChangesGuard';
import { UploadProgress, type UploadItem } from '../components/UploadProgress';
import { mediaUrl } from '../lib/media';
import { heroFormSchema, type HeroFormData } from '../schemas/hero';
import { heroImageQuery, useHero, useHeroUploadLimits, useSaveHero, useUploadHeroImage } from '../services/hooks';
import { heroImageQuery, useHero, useHeroUploadLimits, useSaveHero, useSetHeroImageFocus, useUploadHeroImage } from '../services/hooks';
import type { HeroAdminDto, HeroFocusRequest, HeroImageDto, HeroSaveRequest } from '../services/types';
const textareaClass =
@@ -42,7 +42,7 @@ function toForm(hero: HeroAdminDto): HeroFormData {
};
}
function toRequest(values: HeroFormData, focusEdits: Record<string, HeroFocusRequest>): HeroSaveRequest {
function toRequest(values: HeroFormData): HeroSaveRequest {
return {
title: values.title,
tagline: values.tagline || null,
@@ -50,7 +50,6 @@ function toRequest(values: HeroFormData, focusEdits: Record<string, HeroFocusReq
display: values.display,
buttons: values.buttons.filter((b) => b.label !== '' && b.target !== ''),
slides: values.slides.map((s) => ({ id: s.id, imageId: s.imageId, alt: s.decorative ? null : s.alt, decorative: s.decorative, isActive: s.isActive })),
imageFocus: Object.entries(focusEdits).map(([imageId, edit]) => ({ imageId, ...edit })),
};
}
@@ -146,8 +145,12 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
const limits = useHeroUploadLimits().data;
const [localImages, setLocalImages] = useState<Record<string, HeroImageDto>>({});
const [focusImageId, setFocusImageId] = useState<string | null>(null);
// Changed focus points are kept here until the hero is saved: they are sent with the save and dropped when the page is left.
const [focusEdits, setFocusEdits] = useState<Record<string, HeroFocusRequest>>({});
// A new crop is applied at once (no save needed): the server cuts the new variants in the background.
const setImageFocus = useSetHeroImageFocus();
// The photos of the current upload batch, for the counter "x of y processed": only the photos uploaded together, not
// every photo of the hero. A new batch starts when the previous one is finished.
const [batch, setBatch] = useState<string[]>([]);
const [recutIds, setRecutIds] = useState<string[]>([]);
const fileInput = useRef<HTMLInputElement>(null);
const {
@@ -241,20 +244,19 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
}
}, [images, slides.fields]);
// A focus change counts as unsaved only while it differs from what is saved (applying the old point again is no change).
const samePoint = (a: unknown, b: unknown) => JSON.stringify(a ?? null) === JSON.stringify(b ?? null);
const hasPendingFocus = Object.entries(focusEdits).some(([imageId, edit]) => {
const saved = images[imageId];
return !saved || !samePoint(saved.focus, edit.focus) || !samePoint(saved.wideFocus, edit.wide) || !samePoint(saved.tallFocus, edit.tall);
});
// The form is compared with the last saved state (react-hook-form keeps it), so a change that is undone no longer counts.
// Photos still being prepared on the server and the background refresh are not changes of the administrator.
const hasUnsaved = isDirty || hasPendingFocus;
const hasUnsaved = isDirty;
const contentChanged = Object.keys(dirtyFields).some((key) => key !== 'slides');
const imagesChanged = Boolean(dirtyFields.slides?.some?.(Boolean)) || hasPendingFocus;
const imagesChanged = Boolean(dirtyFields.slides?.some?.(Boolean));
const contentInvalid = Object.keys(errors).some((key) => key !== 'slides');
const imagesInvalid = Boolean(errors.slides);
const busyCount = slides.fields.filter((f) => images[f.imageId]?.processing).length;
// The counter above the form covers the current upload batch only. After a reload (no batch known) it covers the photos that
// are still being prepared. A new crop is shown on its own photo, not in this counter.
const statusImages = (batch.length > 0 ? batch.map((id) => images[id]) : slides.fields.map((f) => images[f.imageId]).filter((i) => i?.processing && !recutIds.includes(i.id))).filter(
(image): image is HeroImageDto => image !== undefined,
);
useEffect(() => {
if (!hasUnsaved) return undefined;
const warn = (event: BeforeUnloadEvent) => {
@@ -264,10 +266,19 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
return () => window.removeEventListener('beforeunload', warn);
}, [hasUnsaved]);
// The image as the dialog shows it: the saved one with a not yet saved change of its focus applied.
const withPendingFocus = (image: HeroImageDto | null): HeroImageDto | null => {
const edit = image ? focusEdits[image.id] : undefined;
return image && edit ? { ...image, focus: edit.focus, wideFocus: edit.wide, tallFocus: edit.tall } : image;
// A new crop goes to the server at once; the page then shows the photo as processing until the new variants are ready.
const applyFocus = (imageId: string, request: HeroFocusRequest) => {
setServerError(null);
setImageFocus.mutate(
{ imageId, request },
{
onSuccess: (image) => {
setLocalImages((current) => ({ ...current, [image.id]: image }));
setRecutIds((current) => [...new Set([...current, image.id])]);
},
onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))),
},
);
};
// Up to this many files go to the host at the same time; the variants are cut in the background, so an upload itself
@@ -290,11 +301,15 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
const results: (HeroImageDto | 'failed' | undefined)[] = new Array<HeroImageDto | 'failed' | undefined>(files.length).fill(undefined);
let flushed = 0;
let cursor = 0;
let inBatch = batch.some((id) => images[id]?.processing);
const flush = () => {
while (flushed < files.length && results[flushed] !== undefined) {
const result = results[flushed];
if (result && result !== 'failed') {
setLocalImages((current) => ({ ...current, [result.id]: result }));
const joins = inBatch;
setBatch((current) => (joins ? [...current, result.id] : [result.id]));
inBatch = true;
// Ready at once (the same photo was already in the CMS): the same note as after a photo that took a while.
if (!result.processing) markDone(result.id);
slides.append({ id: null, imageId: result.id, alt: '', decorative: false, isActive: true }, { shouldFocus: false });
@@ -356,8 +371,7 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
const onSubmit = async (values: HeroFormData) => {
setServerError(null);
try {
const savedHero = await save.mutateAsync(toRequest(values, focusEdits));
setFocusEdits({});
const savedHero = await save.mutateAsync(toRequest(values));
// What was just saved is the new starting point: nothing is unsaved any more.
reset(toForm(savedHero));
toast.success(t('hero.savedToast'));
@@ -387,7 +401,7 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
return (
<form onSubmit={(event) => void handleSubmit(onSubmit, onInvalid)(event)} className="space-y-8" noValidate data-testid="hero-form">
<ProcessingStatus images={slides.fields.map((f) => images[f.imageId]).filter((image): image is HeroImageDto => image !== undefined)} />
<ProcessingStatus images={statusImages} />
<FormErrorBanner error={serverError !== null ? { message: serverError } : null} onDismiss={() => setServerError(null)} />
<HeroTabs
@@ -531,7 +545,6 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
<CropThumbnail
image={image}
savedSrc={thumbnail(image)}
edit={focusEdits[field.imageId]}
onClick={() => setFocusImageId(field.imageId)}
testId={`hero-slide-${index}-focus`}
/>
@@ -624,8 +637,8 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
</div>
<FocusDialog
image={withPendingFocus(focusImageId ? images[focusImageId] ?? null : null)}
onApply={(imageId, edit) => setFocusEdits((current) => ({ ...current, [imageId]: edit }))}
image={focusImageId ? images[focusImageId] ?? null : null}
onApply={applyFocus}
onClose={() => setFocusImageId(null)}
/>
</form>