'use client'; import React, { useState, useEffect, useCallback } from 'react'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/Card'; import { Button } from '@/components/ui/Button'; import { Select } from '@/components/ui/Input'; import { Icon } from '@/components/ui/Icon'; import { TableWrap, Table, TableHeader, TableBody, TableRow, TableHead, TableCell, TableEmpty, } from '@/components/ui/Table'; import { useToast } from '@/components/ui/Toast'; export default function ExportPage() { const { success, info } = useToast(); const [clients, setClients] = useState<{ clientId: string; name: string }[]>([]); const [selectedClient, setSelectedClient] = useState('all'); const [files, setFiles] = useState([]); const [loading, setLoading] = useState(true); const fetchData = useCallback(async () => { try { const clientRes = await fetch('/api/clients'); if (clientRes.ok) { const clientData = await clientRes.json(); setClients(clientData.clients || []); } const storageRes = await fetch(`/api/storage?clientId=${encodeURIComponent(selectedClient)}`); if (storageRes.ok) { const storageData = await storageRes.json(); setFiles(storageData.files || []); } } catch { // ignore } finally { setLoading(false); } }, [selectedClient]); useEffect(() => { fetchData(); }, [fetchData]); const handleExportAll = () => { const url = `/api/export?clientId=${encodeURIComponent(selectedClient)}`; const link = document.createElement('a'); link.href = url; link.setAttribute('download', `${selectedClient}_export.csv`); document.body.appendChild(link); link.click(); document.body.removeChild(link); info('Export Initiated', `Streaming CSV download for client ${selectedClient}`); }; const handleDownloadFile = (clientId: string, filename: string) => { const url = `/api/export?clientId=${encodeURIComponent(clientId)}&filename=${encodeURIComponent(filename)}`; const link = document.createElement('a'); link.href = url; link.setAttribute('download', filename); document.body.appendChild(link); link.click(); document.body.removeChild(link); success('File Download Started', filename); }; return (
{/* Header */}

CSV Exporter

{/* Filter and Endpoint Specs */}
Filter Scope
Files: {files.length}
API Export Endpoint
GET /api/export?clientId={selectedClient} Stream
{/* Partitioned Files List */}

Stored CSV Partitions

/storage/data/{clientId}/{YYYY-MM-DD}.csv
File Name Client ID Date Partition Approx. Rows Size (KB) Last Modified Action {files.length === 0 ? ( ) : ( files.map((file, idx) => ( {file.filename} {file.clientId} {file.date} {file.linesCount.toLocaleString()} {(file.sizeBytes / 1024).toFixed(1)} KB {new Date(file.modifiedAt).toLocaleString()} )) )}
); }