import { useParams, useNavigate } from 'react-router-dom' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { ArrowLeft, Star, Archive, Download, Reply, Forward, Paperclip, Mail, Send, } from 'lucide-react' import toast from 'react-hot-toast' import { Button } from '@/components/ui/Button' import { PecStateBadge, PecTypeBadge } from '@/components/PecBadge/PecBadge' import { ReceiptTree } from '@/components/ReceiptTree/ReceiptTree' import { messagesApi } from '@/api/messages.api' import { formatDate, formatBytes, MAILBOX_STATUS_LABELS } from '@/lib/utils' import { getErrorMessage } from '@/api/client' export function MessageDetailPage() { const { id } = useParams<{ id: string }>() const navigate = useNavigate() const queryClient = useQueryClient() // Carica messaggio const { data: message, isLoading, error, } = useQuery({ queryKey: ['message', id], queryFn: () => messagesApi.get(id!), enabled: !!id, }) // Carica allegati const { data: attachments = [] } = useQuery({ queryKey: ['attachments', id], queryFn: () => messagesApi.getAttachments(id!), enabled: !!id && !!message?.has_attachments, }) // Carica ricevute (solo per messaggi outbound) const { data: receipts = [] } = useQuery({ queryKey: ['receipts', id], queryFn: () => messagesApi.getReceipts(id!), enabled: !!id && message?.direction === 'outbound', }) // Toggle stella const toggleStarMutation = useMutation({ mutationFn: (starred: boolean) => messagesApi.toggleStar(id!, starred), onSuccess: (updated) => { queryClient.setQueryData(['message', id], updated) toast.success(updated.is_starred ? 'Aggiunto ai preferiti' : 'Rimosso dai preferiti') }, onError: (error) => toast.error(getErrorMessage(error)), }) // Archivia const archiveMutation = useMutation({ mutationFn: () => messagesApi.archive(id!), onSuccess: () => { toast.success('Messaggio archiviato') navigate('/inbox') }, onError: (error) => toast.error(getErrorMessage(error)), }) if (isLoading) { return (
) } if (error || !message) { return (

Messaggio non trovato

) } return (
{/* Toolbar */}
{message.direction === 'inbound' ? 'Posta in arrivo' : 'Posta inviata'}
{/* Stella */} {/* Archivia */} {!message.is_archived && ( )} {/* Rispondi (solo per messaggi inbound PEC certificata) */} {message.direction === 'inbound' && message.pec_type === 'posta_certificata' && ( )}
{/* Contenuto */}
{/* Intestazione messaggio */}

{message.subject || '(nessun oggetto)'}

{message.pec_type !== 'posta_certificata' && ( )}
{/* Dettagli busta */}
{message.direction === 'inbound' ? 'Da:' : 'A:'} {message.direction === 'inbound' ? message.from_address : message.to_addresses?.join(', ')} {message.direction === 'outbound' && message.from_address && ( <> Da: {message.from_address} )} {message.direction === 'inbound' && message.to_addresses?.length > 0 && ( <> A: {message.to_addresses.join(', ')} )} {message.cc_addresses?.length > 0 && ( <> Cc: {message.cc_addresses.join(', ')} )} Data: {formatDate(message.received_at || message.sent_at || message.created_at)} {message.size_bytes && ( <> Dimensione: {formatBytes(message.size_bytes)} )} Cartella: {message.imap_folder}
{/* Corpo del messaggio */} {(message.body_html || message.body_text) && (

Contenuto

{message.body_html ? (
) : (
                    {message.body_text}
                  
)}
)} {/* Allegati */} {attachments.length > 0 && (

Allegati ({attachments.length})

)} {/* Ricevute (solo per outbound) */} {message.direction === 'outbound' && (

Tracciamento invio

)} {/* Messaggio originale per ricevute */} {message.direction === 'inbound' && message.pec_type !== 'posta_certificata' && (
Questo è un messaggio automatico di tipo{' '}
)}
) }