Hero: een gewijzigd focuspunt geldt pas bij het opslaan van de hero (Bryan); het focus-venster houdt de wijziging lokaal

Het eindpunt PUT /hero/images/{id}/focus is vervallen; de focus gaat mee in de opslag (imageFocus) en de varianten worden dan in nieuwe bestanden opnieuw gesneden.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 20:05:17 +01:00
co-authored by Claude Sonnet 5.5
parent 01703ccddf
commit 0b56fa6294
20 changed files with 318 additions and 197 deletions
@@ -1,25 +1,24 @@
import { useRef, useState, type KeyboardEvent, type PointerEvent } from 'react';
import { useTranslation } from 'react-i18next';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { FormErrorBanner } from '@/components/ui/FormErrorBanner';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { mediaUrl } from '../lib/media';
import { CENTRE, MAX_ZOOM, TALL, WIDE, cropFor, normalizeFocus, type Ratio } from '../lib/focusMath';
import { useSetImageFocus } from '../services/hooks';
import type { FocusDto, HeroImageDto } from '../services/types';
import type { FocusDto, HeroFocusRequest, HeroImageDto } from '../services/types';
type Target = 'general' | 'wide' | 'tall';
interface FocusDialogProps {
/** The image to edit; null = closed. */
/** The image to edit, with a not yet saved change of its focus already applied; null = closed. */
image: HeroImageDto | null;
/** Keeps the change locally: it is sent and made live when the hero is saved, and dropped when the page is left. */
onApply: (imageId: string, request: HeroFocusRequest) => void;
onClose: () => void;
}
export function FocusDialog({ image, onClose }: FocusDialogProps) {
export function FocusDialog({ image, onApply, onClose }: FocusDialogProps) {
const { t } = useTranslation();
return (
<Dialog open={image !== null} onOpenChange={(next) => { if (!next) onClose(); }}>
@@ -28,7 +27,7 @@ export function FocusDialog({ image, onClose }: FocusDialogProps) {
<DialogTitle>{t('hero.focus.title')}</DialogTitle>
</DialogHeader>
{/* Mounted only while open, so every opening starts from the stored values. */}
{image && <FocusEditor image={image} onClose={onClose} />}
{image && <FocusEditor image={image} onApply={onApply} onClose={onClose} />}
</DialogContent>
</Dialog>
);
@@ -36,11 +35,9 @@ export function FocusDialog({ image, onClose }: FocusDialogProps) {
const percent = (fraction: number) => Math.round(fraction * 1000) / 10;
function FocusEditor({ image, onClose }: { image: HeroImageDto; onClose: () => void }) {
function FocusEditor({ image, onApply, onClose }: { image: HeroImageDto; onApply: (imageId: string, request: HeroFocusRequest) => void; onClose: () => void }) {
const { t } = useTranslation();
const save = useSetImageFocus();
const area = useRef<HTMLDivElement>(null);
const [serverError, setServerError] = useState<string | null>(null);
const [target, setTarget] = useState<Target>('general');
const [general, setGeneral] = useState<FocusDto>(image.focus ?? CENTRE);
const [wide, setWide] = useState<FocusDto | null>(image.wideFocus);
@@ -79,16 +76,9 @@ function FocusEditor({ image, onClose }: { image: HeroImageDto; onClose: () => v
update({ ...active, x: active.x + move[0], y: active.y + move[1] });
};
const onSave = async () => {
setServerError(null);
try {
await save.mutateAsync({ imageId: image.id, body: { focus: general, wide, tall } });
toast.success(t('hero.focus.savedToast'));
onClose();
} catch (error) {
const message = (error as { problem?: { message?: unknown } }).problem?.message;
setServerError(typeof message === 'string' ? message : t('errors.generic'));
}
const onSave = () => {
onApply(image.id, { focus: general, wide, tall });
onClose();
};
const outline = (ratio: Ratio, focus: FocusDto) => {
@@ -103,8 +93,8 @@ function FocusEditor({ image, onClose }: { image: HeroImageDto; onClose: () => v
return (
<div className="space-y-4" data-testid="focus-dialog">
<FormErrorBanner error={serverError !== null ? { message: serverError } : null} onDismiss={() => setServerError(null)} />
<p className="text-sm text-muted-foreground">{t('hero.focus.intro')}</p>
<p className="text-sm font-medium" data-testid="focus-apply-hint">{t('hero.focus.applyHint')}</p>
<fieldset className="flex flex-wrap gap-4">
<legend className="sr-only">{t('hero.focus.targetLegend')}</legend>
@@ -244,8 +234,8 @@ function FocusEditor({ image, onClose }: { image: HeroImageDto; onClose: () => v
<Button type="button" variant="outline" onClick={onClose} data-testid="focus-cancel">
{t('common.cancel')}
</Button>
<Button type="button" onClick={onSave} disabled={save.isPending} data-testid="focus-save">
{save.isPending ? '…' : t('common.save')}
<Button type="button" onClick={onSave} data-testid="focus-save">
{t('hero.focus.apply')}
</Button>
</div>
</div>