'use client' import { useState, useTransition } from 'react' import { de } from '@/lib/i18n/de' import { editEntryAction, restoreEntryAction, retractEntryAction } from '../actions' /** * Ändern und Zurückziehen eines Verlaufseintrags. * * Nicht optimistisch: die Begründung gehört erst abgeschickt und bestätigt, * bevor der neue Text steht. Siehe docs/09-performance-und-ux.md. */ export function EntryActions({ entryId, caseNumber, body, retracted, canEdit, canRetract, }: { entryId: string caseNumber: string body: string retracted: boolean canEdit: boolean canRetract: boolean }) { const [mode, setMode] = useState<'idle' | 'edit' | 'retract'>('idle') const [draft, setDraft] = useState(body) const [reason, setReason] = useState('') const [error, setError] = useState(null) const [pending, startTransition] = useTransition() function run(action: () => Promise<{ ok: boolean; error?: string }>) { setError(null) startTransition(async () => { const result = await action() if (result.ok) { setMode('idle') setReason('') } else { setError(result.error ?? de.actionErrors.default!) } }) } if (retracted) { if (!canRetract) return null return (
setReason(event.target.value)} placeholder={de.entry.retractReason} className="w-64 border border-line bg-raised px-2 py-1" /> {error ? {error} : null}
) } if (mode === 'idle') { if (!canEdit && !canRetract) return null return (
{canEdit ? ( ) : null} {canRetract ? ( ) : null}
) } return (
{mode === 'edit' ? (