173 lines
7.3 KiB
TypeScript
173 lines
7.3 KiB
TypeScript
import { useRef, useState } from 'react';
|
||
import { router } from '@inertiajs/react';
|
||
import { contextApi, type SupplyAggregate } from '@/lib/investor-context';
|
||
import { createOperationKey } from '@/lib/operation-key';
|
||
|
||
export default function InvestmentSupplyAggregate({
|
||
caseId,
|
||
version,
|
||
revision,
|
||
history,
|
||
disabled = false,
|
||
}: {
|
||
caseId: string;
|
||
version: number;
|
||
revision: number;
|
||
history: SupplyAggregate[];
|
||
disabled?: boolean;
|
||
}) {
|
||
const [busy, setBusy] = useState(false);
|
||
const [message, setMessage] = useState('');
|
||
const command = useRef<{ version: number; operation_key: string } | null>(
|
||
null,
|
||
);
|
||
const latest = history.at(-1);
|
||
return (
|
||
<section
|
||
className="space-y-3 rounded border p-4"
|
||
aria-label="Oferta empresarial agregada"
|
||
>
|
||
<h2 className="font-semibold">
|
||
Actividades existentes na localização
|
||
</h2>
|
||
<p>
|
||
Consulte uma análise agregada no seu dossier. As listas, nomes e
|
||
contactos internos permanecem reservados.
|
||
</p>
|
||
<button
|
||
type="button"
|
||
className="min-h-11 rounded border px-3 py-2 underline"
|
||
disabled={disabled || busy}
|
||
onClick={async () => {
|
||
if (busy) return;
|
||
setBusy(true);
|
||
setMessage('');
|
||
command.current ??= {
|
||
version,
|
||
operation_key: createOperationKey(),
|
||
};
|
||
try {
|
||
await contextApi(
|
||
`/investimentos/${caseId}/contexto/oferta-agregada`,
|
||
command.current,
|
||
);
|
||
command.current = null;
|
||
setMessage('Análise agregada guardada no dossier.');
|
||
router.reload({
|
||
only: [
|
||
'case',
|
||
'supply_aggregates',
|
||
'supply_aggregate_enabled',
|
||
],
|
||
});
|
||
} catch (error) {
|
||
setMessage(
|
||
error instanceof Error
|
||
? error.message
|
||
: 'Não foi possível confirmar a consulta. A repetição conserva o mesmo comando.',
|
||
);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}}
|
||
>
|
||
{busy
|
||
? 'A consultar…'
|
||
: command.current
|
||
? 'Repetir a mesma consulta'
|
||
: 'Consultar e guardar análise agregada'}
|
||
</button>
|
||
{message && <p role="status">{message}</p>}
|
||
{latest && (
|
||
<>
|
||
<h3 className="font-semibold">
|
||
{latest.scope.level === 'district'
|
||
? 'Contexto distrital'
|
||
: latest.scope.level === 'province'
|
||
? 'Contexto provincial'
|
||
: 'Contexto local'}{' '}
|
||
— {latest.scope.name}
|
||
</h3>
|
||
{latest.broader_context && (
|
||
<p>
|
||
Local escolhido: {latest.requested_territory.name}.
|
||
Os totais referem-se ao território mais amplo
|
||
indicado acima.
|
||
</p>
|
||
)}
|
||
<p>
|
||
{latest.entities} entidades identificadas nos registos
|
||
administrativos; {latest.repeated} observações repetidas
|
||
e {latest.uncertain} identificações incertas, separadas
|
||
dos totais.
|
||
</p>
|
||
{latest.context_revision !== revision && (
|
||
<p role="status">
|
||
O contexto mudou depois desta análise. Reveja-a; a
|
||
consulta anterior mantém-se no histórico.
|
||
</p>
|
||
)}
|
||
<div className="overflow-x-auto">
|
||
<table className="w-full">
|
||
<caption className="text-left">
|
||
Grupos para investigar actividades similares e
|
||
complementares
|
||
</caption>
|
||
<thead>
|
||
<tr>
|
||
<th className="p-2 text-left">
|
||
Actividade
|
||
</th>
|
||
<th className="p-2">Entidades</th>
|
||
<th className="p-2">Período conhecido</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{latest.groups.map((group) => (
|
||
<tr key={group.code}>
|
||
<td className="p-2">{group.label}</td>
|
||
<td className="p-2 text-center">
|
||
{group.total}
|
||
</td>
|
||
<td className="p-2">
|
||
{group.first_year ??
|
||
'Por confirmar'}
|
||
{group.last_year &&
|
||
group.last_year !== group.first_year
|
||
? '–' + group.last_year
|
||
: ''}
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
{!latest.groups.length && (
|
||
<p>
|
||
Não existem registos aplicáveis nesta consulta. A
|
||
oferta empresarial permanece por determinar; não
|
||
significa ausência de empresas.
|
||
</p>
|
||
)}
|
||
{latest.truncated && (
|
||
<p>
|
||
Apresentação limitada aos primeiros 100 grupos; não
|
||
representa cobertura completa.
|
||
</p>
|
||
)}
|
||
<p>{latest.interpretation}</p>
|
||
<p>{latest.limitations}</p>
|
||
<details>
|
||
<summary>Consulta e histórico</summary>
|
||
<p>
|
||
{history.length} análises preservadas. Última
|
||
consulta: {latest.recorded_at}. Cobertura
|
||
territorial e actividade actual por confirmar.
|
||
</p>
|
||
</details>
|
||
</>
|
||
)}
|
||
</section>
|
||
);
|
||
}
|