'use client'; import { PageShell } from '@/components/layout/PageShell'; import { useVconRole } from '@/components/layout/VconShell'; 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 { getVconRoleCapabilities } from '@/lib/vconUiHelpers'; import { useCallback, useEffect, useState } from 'react'; interface StorageStatsSummary { totalMB?: string; totalFiles?: number; totalRows?: number; rotationRule?: string; rotationMb?: number; } interface StoragePartitionFile { clientId: string; filename: string; sizeBytes: number; linesCount: number; modifiedAt: string; compressed?: boolean; } export default function StoragePage() { const { success, warning, error } = useToast(); const currentRole = useVconRole(); const { canOperate } = getVconRoleCapabilities(currentRole); const [stats, setStats] = useState(() => getCached('vcon:storage-stats', null) ); const [files, setFiles] = useState(() => getCached('vcon:storage-files', []) ); const [refreshing, setRefreshing] = useState(false); const [flushing, setFlushing] = useState(false); const fetchStorageData = useCallback(async () => { setRefreshing(true); try { const res = await fetch('/api/storage', { cache: 'no-store' }); if (res.status === 401) { window.location.assign('/vcon/login'); return; } const data = await res.json().catch(() => ({})); if (!res.ok) { throw new Error(data.error || 'Unable to load storage partitions'); } setStats(setCached('vcon:storage-stats', data.stats ?? null)); setFiles(setCached('vcon:storage-files', data.files || [])); } catch (loadErr) { error( 'Storage load failed', loadErr instanceof Error ? loadErr.message : 'Could not load storage partitions.' ); } finally { setRefreshing(false); } }, [error]); useEffect(() => { void fetchStorageData(); }, [fetchStorageData]); const handleManualFlush = async () => { if (!canOperate) { error('Read-only role', 'Buffer flush requires operator or owner permissions.'); return; } setFlushing(true); try { const res = await fetch('/api/storage', { method: 'POST', cache: 'no-store', }); if (res.status === 401) { window.location.assign('/vcon/login'); return; } const data = await res.json().catch(() => ({})); if (res.ok && data.success) { const count = Number(data.flushedCount) || 0; if (data.warning) { warning( 'Partial Flush (Web Only)', `Flushed ${count} event${count === 1 ? '' : 's'}. ${String(data.warning)}` ); } else { success( 'Buffer Flushed', `Flushed ${count} event${count === 1 ? '' : 's'} to CSV.` ); } await fetchStorageData(); } else if (data.flushedCount !== undefined && Number(data.flushedCount) > 0) { warning( 'Partial Flush', `Flushed ${data.flushedCount} local events. Ingestion server: ${data.error || 'flush failed'}` ); await 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 ( {canOperate ? ( ) : ( Read-only ({currentRole}) )} } > Client Filename Size Rows Modified {files.length === 0 ? ( ) : ( files.map((file) => { const isCompressed = Boolean( file.compressed || file.filename.toLowerCase().endsWith('.csv.gz') ); return ( {file.clientId}
{isCompressed ? 'CSV.GZ' : 'CSV'} {file.filename}
{(file.sizeBytes / 1024).toFixed(1)} KB {file.linesCount.toLocaleString()} {new Date(file.modifiedAt).toLocaleString()}
); }) )}
); }