import { Link, useForm } from '@inertiajs/react'; import { useEffect, useState } from 'react'; import { Management, Field, button, control } from '../management/shared'; import EditorialReading, { type EditorialArticle, } from '@/components/editorial-reading'; type Content = { body: string; image_resource_id: number | null; image_mode?: string; }; export default function ArticleEditor({ resource, content, source, images, revisions, }: { resource: { id: number; title: string; metadata_version: number; published: boolean; }; content: Content | null; source?: { source_url?: string; image_source_url?: string; published_at?: string; modified_at?: string; legacy_id?: number; } | null; images: { id: number; title: string }[]; revisions: { revision_no: number; change_type: string; created_at: string; }[]; }) { const initial = () => ({ body: content?.body ?? '', image_resource_id: content?.image_resource_id ? String(content.image_resource_id) : '', image_mode: content?.image_mode ?? (content?.image_resource_id ? 'selected' : 'none'), metadata_version: resource.metadata_version, }); const form = useForm(initial()); const [preview, setPreview] = useState(null); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); useEffect(() => { form.setData(initial()); setPreview(null); setError(''); }, [resource.id, resource.metadata_version]); useEffect(() => { setPreview(null); }, [form.data.body, form.data.image_mode, form.data.image_resource_id]); const showPreview = async () => { setBusy(true); setError(''); try { const token = document.cookie .split('; ') .find((v) => v.startsWith('XSRF-TOKEN=')) ?.slice(11); const response = await fetch( `/management/portal-services/resources/${resource.id}/article/preview`, { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-XSRF-TOKEN': decodeURIComponent(token ?? ''), }, body: JSON.stringify(form.data), }, ); const data = await response.json().catch(() => ({ message: 'Não foi possível abrir a prévia. Recupere o editor e confirme a sessão e o MFA existentes.', })); if (!response.ok) throw new Error( Object.values(data.errors ?? {}) .flat() .join(' ') || data.message || 'Não foi possível preparar a prévia.', ); setPreview(data.article); } catch (e) { setError( e instanceof Error ? e.message : 'Não foi possível preparar a prévia.', ); } finally { setBusy(false); } }; return (

O original, a origem e as versões permanecem na biblioteca. Guardar cria uma revisão editorial e retira a publicação até à sua acção expressa no acervo.

Revisão guardada {resource.metadata_version} ·{' '} {resource.published ? 'Publicado' : 'Rascunho privado'}

{source && (

Original {source.legacy_id} · publicado em{' '} {source.published_at} · modificado em {source.modified_at}. A imagem de origem mantém a sua referência mesmo quando não existe exemplar disponível.

)}
{ e.preventDefault(); setError(''); form.put( `/management/portal-services/resources/${resource.id}/article`, ); }} > {!!Object.keys(form.errors).length && (
{Object.values(form.errors).join(' ')}
)}