89 propostas, segundo os critérios aprovados pelo proprietário a partir da simulação do PII, ficam «por decidir» com fonte e evidência. A página «Ligações da carteira» aprova ou rejeita uma a uma ou por grupo; só a aprovação escreve a ligação, com registo de quem e quando, e anular retira apenas a ligação que a aprovação criou. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
239 lines
8.3 KiB
TypeScript
239 lines
8.3 KiB
TypeScript
import { Link, router } from '@inertiajs/react';
|
|
import { useState } from 'react';
|
|
import { Management, button, control } from './shared';
|
|
import { api } from '@/pages/investment/shared';
|
|
|
|
type Item = {
|
|
id: string;
|
|
target_type: 'project' | 'corridor' | 'cluster';
|
|
target: string;
|
|
relation_label: string;
|
|
source: string;
|
|
evidence: string;
|
|
status: 'pending' | 'approved' | 'rejected';
|
|
decided_at: string | null;
|
|
decided_by: string | null;
|
|
decision_note: string | null;
|
|
reference: string;
|
|
slug: string;
|
|
title: string;
|
|
province: string | null;
|
|
};
|
|
|
|
const targetLabel = {
|
|
project: 'Projecto âncora',
|
|
corridor: 'Corredor',
|
|
cluster: 'Cluster',
|
|
};
|
|
|
|
const statusLabel = {
|
|
pending: 'Por decidir',
|
|
approved: 'Aprovada',
|
|
rejected: 'Rejeitada',
|
|
};
|
|
|
|
/** Carteira 4.3: a proposed link only reaches the portal after a person approves it here. */
|
|
export default function PortfolioLinkReviews({
|
|
items,
|
|
status,
|
|
statuses,
|
|
installed,
|
|
}: {
|
|
items: Item[];
|
|
status: string;
|
|
statuses: Record<string, string>;
|
|
installed: boolean;
|
|
}) {
|
|
const groups = new Map<string, Item[]>();
|
|
for (const item of items) {
|
|
const key = `${targetLabel[item.target_type]}: ${item.target}`;
|
|
groups.set(key, [...(groups.get(key) ?? []), item]);
|
|
}
|
|
return (
|
|
<Management title="Ligações da carteira">
|
|
<p>
|
|
Propostas de ligação entre oportunidades publicadas e projectos
|
|
âncora, corredores e clusters. Cada proposta mostra a fonte e a
|
|
razão. A ligação só aparece no portal depois de aprovada aqui, e
|
|
fica registado quem aprovou e quando. Os projectos integrados do
|
|
PII são investimentos planeados e não são oportunidades.
|
|
</p>
|
|
{!installed && (
|
|
<p role="status">
|
|
A revisão de ligações ainda não está instalada neste
|
|
ambiente (migração por executar).
|
|
</p>
|
|
)}
|
|
<nav className="my-3 flex flex-wrap gap-3" aria-label="Estado">
|
|
{[...Object.entries(statuses), ['all', 'Todas']].map(
|
|
([key, label]) => (
|
|
<Link
|
|
key={key}
|
|
className={
|
|
key === status ? 'font-bold' : 'underline'
|
|
}
|
|
href={`/management/ligacoes-carteira?status=${key}`}
|
|
>
|
|
{label}
|
|
</Link>
|
|
),
|
|
)}
|
|
</nav>
|
|
{items.length === 0 ? (
|
|
<p>Sem propostas neste estado.</p>
|
|
) : (
|
|
[...groups.entries()].map(([group, rows]) => (
|
|
<Group key={group} title={group} rows={rows} />
|
|
))
|
|
)}
|
|
</Management>
|
|
);
|
|
}
|
|
|
|
async function decide(
|
|
ids: string[],
|
|
decision: 'approve' | 'reject' | 'undo',
|
|
note: string,
|
|
) {
|
|
await api('/management/ligacoes-carteira', { ids, decision, note });
|
|
router.reload();
|
|
}
|
|
|
|
function Group({ title, rows }: { title: string; rows: Item[] }) {
|
|
const pending = rows.filter((row) => row.status === 'pending');
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState('');
|
|
return (
|
|
<section className="mt-6" aria-label={title}>
|
|
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
<h2 className="text-lg font-semibold">
|
|
{title} ({rows.length})
|
|
</h2>
|
|
{pending.length > 1 && (
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
disabled={busy}
|
|
onClick={async () => {
|
|
setBusy(true);
|
|
setError('');
|
|
try {
|
|
await decide(
|
|
pending.map((row) => row.id),
|
|
'approve',
|
|
'Aprovação do grupo',
|
|
);
|
|
} catch (cause) {
|
|
setError(
|
|
cause instanceof Error
|
|
? cause.message
|
|
: 'Não foi possível guardar.',
|
|
);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}}
|
|
>
|
|
Aprovar as {pending.length} por decidir
|
|
</button>
|
|
)}
|
|
</div>
|
|
{error && <p role="alert">{error}</p>}
|
|
<ul className="mt-2 space-y-3">
|
|
{rows.map((row) => (
|
|
<Row key={row.id} item={row} />
|
|
))}
|
|
</ul>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function Row({ item }: { item: Item }) {
|
|
const [note, setNote] = useState('');
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState('');
|
|
const act = async (decision: 'approve' | 'reject' | 'undo') => {
|
|
setBusy(true);
|
|
setError('');
|
|
try {
|
|
await decide([item.id], decision, note);
|
|
} catch (cause) {
|
|
setError(
|
|
cause instanceof Error
|
|
? cause.message
|
|
: 'Não foi possível guardar.',
|
|
);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
return (
|
|
<li className="rounded border p-3">
|
|
<p>
|
|
<Link
|
|
className="font-semibold underline"
|
|
href={`/oportunidades/${item.slug}`}
|
|
>
|
|
{item.title}
|
|
</Link>{' '}
|
|
· {item.province ?? 'sem província'} · {item.reference}
|
|
</p>
|
|
<p className="text-sm">
|
|
<strong>{item.relation_label}.</strong> {item.evidence}
|
|
</p>
|
|
<p className="text-muted-foreground text-xs">
|
|
Fonte: {item.source}
|
|
</p>
|
|
<p className="text-sm">
|
|
Estado: <strong>{statusLabel[item.status]}</strong>
|
|
{item.decided_at &&
|
|
` · ${item.decided_by ?? 'utilizador'} em ${new Date(item.decided_at).toLocaleString('pt-PT')}`}
|
|
{item.decision_note && ` · «${item.decision_note}»`}
|
|
</p>
|
|
<div className="mt-2 flex flex-wrap items-end gap-2">
|
|
{item.status === 'pending' && (
|
|
<label className="grid min-w-64 flex-1 gap-1 text-sm">
|
|
Nota (opcional)
|
|
<input
|
|
className={control}
|
|
value={note}
|
|
maxLength={2000}
|
|
onChange={(e) => setNote(e.target.value)}
|
|
/>
|
|
</label>
|
|
)}
|
|
{item.status === 'pending' ? (
|
|
<>
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
disabled={busy}
|
|
onClick={() => act('approve')}
|
|
>
|
|
Aprovar
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
disabled={busy}
|
|
onClick={() => act('reject')}
|
|
>
|
|
Rejeitar
|
|
</button>
|
|
</>
|
|
) : (
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
disabled={busy}
|
|
onClick={() => act('undo')}
|
|
>
|
|
Voltar a «por decidir»
|
|
</button>
|
|
)}
|
|
</div>
|
|
{error && <p role="alert">{error}</p>}
|
|
</li>
|
|
);
|
|
}
|