import { Management, Pagination } from './shared'; import type { Paginated } from './shared'; type Event = { id: string; entity_type: string; action: string; occurred_at: string; actor: string | null; before_data: { values: Record }; after_data: { record_id: number | string; changed_fields: string[]; values: Record; }; }; const entities: Record = { investment_accounts: 'Perfil de investimento', investment_company_declarations: 'Declaração de empresa', investment_submissions: 'Manifestação de interesse', investment_messages: 'Mensagem de acompanhamento', investment_reference_options: 'Opção de formulário', opportunity_context: 'Contexto da oportunidade', sectors: 'Sector', opportunities: 'Oportunidade', interest_requests: 'Pedido', investors: 'Investidor', contacts: 'Contacto', leads: 'Lead', tasks: 'Tarefa', projects: 'Projecto', economic_clusters: 'Cluster', cluster_projects: 'Associação projecto–cluster', value_chains: 'Etapa da cadeia de valor', value_chain_links: 'Ligação entre etapas', investment_gaps: 'Lacuna de investimento', }; const fields: Record = { version: 'Versão', active: 'Activo', name: 'Nome', slug: 'Endereço', description: 'Descrição', short_description: 'Descrição curta', status: 'Estado', is_active: 'Activo', is_featured: 'Destaque', sort_order: 'Ordem', sector_id: 'Sector', maturity_level: 'Maturidade', maturity_label: 'Descrição da maturidade', estimated_investment_amount: 'Investimento', estimated_investment_currency: 'Moeda', reference: 'Referência', title: 'Título', summary: 'Resumo', province: 'Província', district: 'Distrito', promoter_name: 'Promotor', promoter_type: 'Tipo de promotor', financing_modality: 'Financiamento', expected_impact: 'Impacto', published_at: 'Publicação', reviewed_at: 'Data de revisão', reviewed_by: 'Responsável pela revisão', }; function display(value: unknown): string { if (value === null || value === undefined) return '—'; if (typeof value === 'boolean') return value ? 'Sim' : 'Não'; if (typeof value === 'object') return JSON.stringify(value); if (typeof value === 'string') return value; if (typeof value === 'number') return value.toString(); return '—'; } export default function History({ events }: { events: Paginated }) { return (

Alterações efectuadas na gestão do portal. Os textos e contactos pessoais não são copiados para o histórico.

{events.data.map((event) => (

{entities[event.entity_type] ?? event.entity_type} # {event.after_data.record_id} ·{' '} {event.action === 'created' ? 'Criado' : 'Actualizado'}

{event.actor ?? 'Utilizador indisponível'} ·{' '} {new Intl.DateTimeFormat('pt-MZ', { dateStyle: 'medium', timeStyle: 'short', timeZone: 'Africa/Maputo', }).format(new Date(event.occurred_at))}{' '} (Maputo)

Campos:{' '} {event.after_data.changed_fields .map((key) => fields[key] ?? key) .join(', ')}

{Object.entries(event.after_data.values) .filter( ([key, value]) => JSON.stringify( event.before_data.values[key], ) !== JSON.stringify(value), ) .map(([key, value]) => (

{fields[key] ?? key}:{' '} {display( event.before_data.values[key] ?? '—', )}{' '} → {display(value)}

))}
))}
{!events.data.length && (

Ainda não existem alterações registadas.

)}
); }