import { useEffect, useState } from 'react'; import { profileJson } from '@/lib/profile-json'; import { createOperationKey } from '@/lib/operation-key'; type Resource = { id: string; version: number; deleted: boolean; caption: string | null; digitally_edited: boolean | null; edit_description: string | null; scan: string; }; export default function OfferPrivateResources({ id, etag, editable, property, disabled, onChanged, }: { id: number; etag: string; editable: boolean; property: boolean; disabled: boolean; onChanged: (etag: string) => void; }) { const [resources, setResources] = useState<{ images: Resource[]; documents: Resource[]; }>({ images: [], documents: [] }); const [internal, setInternal] = useState(false); const [file, setFile] = useState(null); const [kind, setKind] = useState('images'); const [caption, setCaption] = useState(''); const [edited, setEdited] = useState(false); const [editDescription, setEditDescription] = useState(''); const [message, setMessage] = useState(''); const [busy, setBusy] = useState(false); const [editing, setEditing] = useState(null); const base = `/minhas-oportunidades/ofertas/${id}/recursos`; async function refresh() { const result = await profileJson(base, {}, 'GET'); setResources(result.resources); setInternal(result.internalProofAccess); onChanged(result.etag); } useEffect(() => { let active = true; profileJson(base, {}, 'GET') .then((result) => { if (active) { setResources(result.resources); setInternal(result.internalProofAccess); } }) .catch((error) => { if (active) setMessage(error.message); }); return () => { active = false; }; }, [base]); async function upload() { if ((!file && !editing) || busy || disabled) return; setBusy(true); setMessage(''); const form = new FormData(); if (file) form.append('file', file); form.append('etag', etag); if (editing) form.append('version', String(editing.version)); else form.append('creation_key', createOperationKey()); form.append( 'reason', 'Envio privado de suporte da oferta pelo promotor.', ); form.append( 'payload', JSON.stringify( kind === 'images' ? { visibility: 'internal', caption, alt_text: caption, digitally_edited: edited, edit_description: edited ? editDescription : null, } : { visibility: 'internal', classification: 'restricted', category: 'real_estate_proof', }, ), ); try { await profileJson( editing ? `${base}/images/${editing.id}/guardar` : `${base}/${kind}`, form, ); await refresh(); setFile(null); setEditing(null); setMessage( 'Original guardado em privado. A análise de segurança pode ainda estar pendente.', ); } catch (error) { setMessage( error instanceof Error ? error.message : 'Não foi possível guardar.', ); } finally { setBusy(false); } } async function lifecycle(resource: Resource, resourceKind: string) { setBusy(true); try { await profileJson(`${base}/${resourceKind}/${resource.id}`, { etag, version: resource.version, action: resource.deleted ? 'restore' : 'delete', confirm: true, reason: 'Alteração explícita dos suportes privados da oferta.', }); await refresh(); } catch (error) { setMessage( error instanceof Error ? error.message : 'Não foi possível alterar.', ); } finally { setBusy(false); } } return (

Fotografias e suportes privados

Até seis fotografias. JPEG, PNG ou WebP, até 10 MB e pelo menos 400 × 400 píxeis. Os comprovativos são reservados à validação interna; o envio não certifica titularidade.

{disabled &&

Guarde os dados antes de alterar os ficheiros.

} {editable && (
{kind === 'images' && ( <> {edited && (