'use client'; import { useQuery } from '@tanstack/react-query'; import { formatDistanceToNow } from 'date-fns'; import { Mail } from 'lucide-react'; import { apiFetch } from '@/lib/api/client'; import { EmptyState } from '@/components/shared/empty-state'; import { Skeleton } from '@/components/ui/skeleton'; interface Thread { id: string; subject: string; snippet: string | null; lastMessageAt: string; participants: string[]; unreadCount: number; } interface ThreadsResponse { data: Thread[]; total: number; } export function EmailThreadsList() { const { data, isLoading } = useQuery({ queryKey: ['email', 'threads'], queryFn: () => apiFetch('/api/v1/email/threads'), }); if (isLoading) { // Skeleton rows shaped like the real list so the layout doesn't pop. return (
{Array.from({ length: 4 }).map((_, i) => (
))}
); } const threads = data?.data ?? []; if (threads.length === 0) { return ( ); } return (
{threads.map((t) => (
{t.subject || '(no subject)'}
{formatDistanceToNow(new Date(t.lastMessageAt), { addSuffix: true })}
{t.participants.join(', ')}
{t.snippet && (
{t.snippet}
)} {t.unreadCount > 0 && ( {t.unreadCount} unread )}
))}
); }