import { useRef, useState } from 'react'; import { Link, router } from '@inertiajs/react'; import CatalogueChoiceSelect from './catalogue-choice-select'; import { contextApi, type PromoterEntity } from '@/lib/investor-context'; import { createOperationKey } from '@/lib/operation-key'; type Options = { version: number; selected: PromoterEntity; can_write: boolean; companies: { id: string; name: string; version: number }[]; organizations: { id: string; name: string; version: number }[]; }; const valueOf = (entity?: PromoterEntity) => entity && ['company', 'organization'].includes(entity.kind) ? entity.kind + ':' + entity.id : (entity?.kind ?? 'undetermined'); export default function InvestmentPromoter({ caseId, selected, canEdit, }: { caseId: string; selected?: PromoterEntity; canEdit: boolean; }) { const [options, setOptions] = useState(null); const [choice, setChoice] = useState(valueOf(selected)); const [busy, setBusy] = useState(false); const [error, setError] = useState(''); const [uncertain, setUncertain] = useState(false); const command = useRef | null>(null); const current = options?.selected ?? selected; const url = `/investimentos/${caseId}/contexto/entidade`; const recover = async () => { setBusy(true); setError(''); try { setOptions(await contextApi(url)); command.current = null; setUncertain(false); } catch (error) { setError( error instanceof Error ? error.message : 'Não foi possível consultar a associação.', ); } finally { setBusy(false); } }; return (

Entidade promotora

{current?.snapshot?.name || (current?.kind === 'being_formed' ? 'Entidade por constituir' : 'Entidade por definir')}

{current?.kind === 'company' || current?.kind === 'organization' ? 'Cadastro associado no portal. A associação não comprova constituição legal nem altera a propriedade ou o acesso ao dossier.' : 'Pode continuar a ideia e a viabilidade. Os actos que dependam da entidade serão identificados na respectiva etapa.'}

{canEdit && (
{ if (event.currentTarget.open && !options && !busy) void recover(); }} > Associar ou alterar a entidade promotora {options && (
({ value: 'company:' + company.id, label: company.name + ' — empresa declarada', })), ...options.organizations.map( (organization) => ({ value: 'organization:' + organization.id, label: organization.name + ' — organização com acesso autorizado', }), ), ]} onChange={(value) => { setChoice(value || 'undetermined'); command.current = null; }} /> Registar uma empresa existente no cadastro habitual e regressar
)} {error &&

{error}

} {(error || uncertain) && ( )}
)}
); }