'use client'; import { PageShell } from '@/components/layout/PageShell'; import { Button } from '@/components/ui/Button'; import { Panel } from '@/components/ui/Panel'; import { StatStrip } from '@/components/ui/StatStrip'; import { Table, TableBody, TableCell, TableEmpty, TableHead, TableHeader, TableRow, TableWrap, } from '@/components/ui/Table'; import { useToast } from '@/components/ui/Toast'; import { getCached, setCached } from '@/lib/vconCache'; import { useCallback, useEffect, useState } from 'react'; export default function StoragePage() { const { success, warning, error } = useToast(); const [stats, setStats] = useState(() => getCached('vcon:storage-stats', null) ); const [files, setFiles] = useState(() => getCached('vcon:storage-files', []) ); const [flushing, setFlushing] = useState(false); const fetchStorageData = useCallback(async () => { try { const res = await fetch('/api/storage'); if (!res.ok) throw new Error('Unable to load storage partitions'); const data = await res.json(); setStats(setCached('vcon:storage-stats', data.stats)); setFiles(setCached('vcon:storage-files', data.files || [])); } catch { error('Storage load failed', 'Could not load storage partitions.'); } }, [error]); useEffect(() => { fetchStorageData(); }, [fetchStorageData]); const handleManualFlush = async () => { setFlushing(true); try { const res = await fetch('/api/storage', { method: 'POST' }); const data = await res.json().catch(() => ({})); if (res.ok && data.success) { const count = Number(data.flushedCount) || 0; success( 'Buffer Flushed', `Flushed ${count} event${count === 1 ? '' : 's'} to CSV${data.warning ? ` (${data.warning})` : ''}` ); fetchStorageData(); } else if (data.flushedCount !== undefined && data.flushedCount > 0) { // Partial flush: local events persisted, remote Port 4000 flush returned error warning( 'Partial Flush', `Flushed ${data.flushedCount} local events. Ingestion server: ${data.error || 'flush failed'}` ); fetchStorageData(); } else { error('Flush Failed', data.error || 'Could not flush buffer'); } } catch (err: unknown) { const msg = err instanceof Error ? err.message : 'Unknown network error'; error('Flush Error', msg); } finally { setFlushing(false); } }; return ( } > Client Filename Size Rows Modified {files.length === 0 ? ( ) : ( files.map((file, idx) => ( {file.clientId} {file.filename} {(file.sizeBytes / 1024).toFixed(1)} KB {file.linesCount.toLocaleString()} {new Date(file.modifiedAt).toLocaleString()} )) )}
); }