'use client'; import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { Download, FileText } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Skeleton } from '@/components/ui/skeleton'; import { ReportStatusBadge } from '@/components/reports/report-status-badge'; import { apiFetch } from '@/lib/api/client'; import { toastError } from '@/lib/api/toast-error'; interface GeneratedReport { id: string; name: string; reportType: string; status: 'queued' | 'processing' | 'ready' | 'failed'; requestedBy: string; createdAt: string; completedAt: string | null; errorMessage: string | null; fileId: string | null; } interface ReportsResponse { data: GeneratedReport[]; pagination: { page: number; pageSize: number; total: number; totalPages: number; }; } const REPORT_TYPE_LABELS: Record = { pipeline: 'Pipeline Summary', revenue: 'Revenue', activity: 'Activity Log', occupancy: 'Berth Occupancy', }; export function ReportsList() { const [downloadingId, setDownloadingId] = useState(null); const { data, isLoading } = useQuery({ queryKey: ['reports'], queryFn: () => apiFetch('/api/v1/reports?limit=50'), refetchInterval: (query) => { const rows = query.state.data?.data ?? []; const hasPending = rows.some((r) => r.status === 'queued' || r.status === 'processing'); return hasPending ? 5000 : false; }, }); const handleDownload = async (reportId: string) => { setDownloadingId(reportId); try { const result = await apiFetch<{ url: string }>(`/api/v1/reports/${reportId}/download`); window.open(result.url, '_blank'); } catch (err) { // Surface the failure to the user — was previously console-only, // so the rep clicked Download and nothing happened (auditor-H §35). toastError(err, 'Download failed'); } finally { setDownloadingId(null); } }; return ( Generated Reports {isLoading ? (
{Array.from({ length: 3 }).map((_, i) => ( ))}
) : !data?.data.length ? (

No reports generated yet

Use the form above to generate your first report.

) : ( Name Type Status Requested Completed Actions {data.data.map((report) => ( {report.name} {REPORT_TYPE_LABELS[report.reportType] ?? report.reportType} {new Date(report.createdAt).toLocaleString('en-GB', { dateStyle: 'short', timeStyle: 'short', })} {report.completedAt ? ( new Date(report.completedAt).toLocaleString('en-GB', { dateStyle: 'short', timeStyle: 'short', }) ) : report.status === 'failed' && report.errorMessage ? ( Failed ) : ( '-' )} {report.status === 'ready' && report.fileId && ( )} ))}
)}
); }