import { ChevronDown, ChevronRight, Mail, ExternalLink } from 'lucide-react' import { useState } from 'react' import { useNavigate } from 'react-router-dom' import { PecTypeBadge, PecStateBadge } from '@/components/PecBadge/PecBadge' import { formatDate } from '@/lib/utils' import type { MessageResponse } from '@/types/api.types' interface ReceiptTreeProps { message: MessageResponse receipts: MessageResponse[] } /** * Visualizza la gerarchia delle ricevute PEC collegate a un messaggio. * Mostra in ordine cronologico: accettazione → consegna (o anomalia). * Le ricevute sono cliccabili e navigano al dettaglio del messaggio ricevuta. */ export function ReceiptTree({ message, receipts }: ReceiptTreeProps) { const [expanded, setExpanded] = useState(true) const navigate = useNavigate() if (receipts.length === 0) { if (message.direction === 'outbound') { return (
Nessuna ricevuta ancora ricevuta per questo messaggio.
) } return null } return (
{expanded && (
{/* Messaggio originale */} {/* Ricevute in ordine cronologico */} {[...receipts] .sort( (a, b) => new Date(a.received_at || a.created_at).getTime() - new Date(b.received_at || b.created_at).getTime(), ) .map((receipt) => ( navigate(`/messages/${receipt.id}`)} /> ))}
)}
) } interface ReceiptNodeProps { label: string date: string | null state?: MessageResponse['state'] type?: MessageResponse['pec_type'] messageId?: string isRoot?: boolean onClick?: () => void } function ReceiptNode({ label, date, state, type, isRoot, onClick }: ReceiptNodeProps) { const isClickable = !!onClick const content = (
{/* Indicatore timeline */}
{label} {state && !isRoot && } {type && type !== 'posta_certificata' && } {isClickable && ( )}
{date && (

{formatDate(date)}

)}
) if (isClickable) { return ( ) } return
{content}
}