refactor: replace AdminPageHeader with PageShell
Migrate pages to PageShell, add global autofill styles, and update SDK/simulator page logic for server URL and state consistency.
This commit is contained in:
1 parent
fa4516b078
commit
81c5e4e2fd
24 files changed
+1238
-595
No files matched your search
@@ -1,18 +1,18 @@
|
||||
'use client';
|
||||
|
||||
import { AdminPageHeader } from '@/components/layout/AdminPageHeader';
|
||||
import { PageShell } from '@/components/layout/PageShell';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { ClientList, type ClientListItem } from '@/components/ui/ClientList';
|
||||
import { Panel } from '@/components/ui/Panel';
|
||||
import { StatStrip } from '@/components/ui/StatStrip';
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
TableWrap,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
TableWrap,
|
||||
} from '@/components/ui/Table';
|
||||
import { getCached, setCached } from '@/lib/vconCache';
|
||||
import Link from 'next/link';
|
||||
@@ -49,21 +49,30 @@ type StorageFile = {
|
||||
};
|
||||
|
||||
export const DashboardOverview: React.FC = () => {
|
||||
const [metrics, setMetrics] = useState<DashboardMetrics | null>(() => getCached<DashboardMetrics | null>('vcon:metrics', null));
|
||||
const [events, setEvents] = useState<EventItem[]>(() => getCached<EventItem[]>('vcon:dashboard-events', []));
|
||||
const [clients, setClients] = useState<ClientListItem[]>(() => getCached<ClientListItem[]>('vcon:clients', []));
|
||||
const [files, setFiles] = useState<StorageFile[]>(() => getCached<StorageFile[]>('vcon:storage-files', []));
|
||||
const [metrics, setMetrics] = useState<DashboardMetrics | null>(() =>
|
||||
getCached<DashboardMetrics | null>('vcon:metrics', null)
|
||||
);
|
||||
const [events, setEvents] = useState<EventItem[]>(() =>
|
||||
getCached<EventItem[]>('vcon:dashboard-events', [])
|
||||
);
|
||||
const [clients, setClients] = useState<ClientListItem[]>(() =>
|
||||
getCached<ClientListItem[]>('vcon:clients', [])
|
||||
);
|
||||
const [files, setFiles] = useState<StorageFile[]>(() =>
|
||||
getCached<StorageFile[]>('vcon:storage-files', [])
|
||||
);
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
setIsRefreshing(true);
|
||||
try {
|
||||
const [metricsResponse, eventsResponse, storageResponse, clientsResponse] = await Promise.all([
|
||||
fetch('/api/metrics', { cache: 'no-store' }),
|
||||
fetch('/api/events?limit=8', { cache: 'no-store' }),
|
||||
fetch('/api/storage', { cache: 'no-store' }),
|
||||
fetch('/api/clients', { cache: 'no-store' }),
|
||||
]);
|
||||
const [metricsResponse, eventsResponse, storageResponse, clientsResponse] =
|
||||
await Promise.all([
|
||||
fetch('/api/metrics', { cache: 'no-store' }),
|
||||
fetch('/api/events?limit=8', { cache: 'no-store' }),
|
||||
fetch('/api/storage', { cache: 'no-store' }),
|
||||
fetch('/api/clients', { cache: 'no-store' }),
|
||||
]);
|
||||
|
||||
if (metricsResponse.ok) {
|
||||
const nextMetrics = await metricsResponse.json();
|
||||
@@ -99,60 +108,79 @@ export const DashboardOverview: React.FC = () => {
|
||||
const storedRecordsToday = files
|
||||
.filter((file) => file.date === today)
|
||||
.reduce((total, file) => total + file.linesCount, 0);
|
||||
const eventsToday = storedRecordsToday + (metrics?.bufferStats.currentBufferSize ?? 0);
|
||||
const activeClients = metrics?.activeClients ?? clients.filter((client) => client.status === 'active').length;
|
||||
const eventsToday =
|
||||
storedRecordsToday + (metrics?.bufferStats.currentBufferSize ?? 0);
|
||||
const activeClients =
|
||||
metrics?.activeClients ??
|
||||
clients.filter((client) => client.status === 'active').length;
|
||||
const totalClients = metrics?.totalClients ?? clients.length;
|
||||
const totalStorage = metrics?.storageStats.totalMB ?? '0.00';
|
||||
const totalStorageBytes = metrics?.storageStats.totalBytes ?? 0;
|
||||
const storageValue = totalStorageBytes < 1024 * 1024
|
||||
? String(Math.max(1, Math.round(totalStorageBytes / 1024)))
|
||||
: totalStorage;
|
||||
const storageValue =
|
||||
totalStorageBytes < 1024 * 1024
|
||||
? String(Math.max(1, Math.round(totalStorageBytes / 1024)))
|
||||
: totalStorage;
|
||||
const storageUnit = totalStorageBytes < 1024 * 1024 ? 'KB' : 'MB';
|
||||
const totalFiles = metrics?.storageStats.totalFiles ?? files.length;
|
||||
const totalRows = metrics?.storageStats.totalRows ?? files.reduce((sum, file) => sum + file.linesCount, 0);
|
||||
const totalRows =
|
||||
metrics?.storageStats.totalRows ??
|
||||
files.reduce((sum, file) => sum + file.linesCount, 0);
|
||||
|
||||
return (
|
||||
<section className="w-full">
|
||||
<AdminPageHeader
|
||||
title="Dashboard"
|
||||
actions={
|
||||
<>
|
||||
<Button
|
||||
size="icon"
|
||||
variant="secondary"
|
||||
icon="RefreshCw"
|
||||
aria-label="Refresh dashboard"
|
||||
title="Refresh dashboard"
|
||||
loading={isRefreshing}
|
||||
onClick={() => void refresh()}
|
||||
/>
|
||||
<a href="/api/export?clientId=all" download>
|
||||
<Button size="xs" variant="secondary" icon="Download">Export</Button>
|
||||
</a>
|
||||
<Link href="/vcon/clients">
|
||||
<Button size="xs" variant="primary" icon="Plus">New client</Button>
|
||||
</Link>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
<PageShell
|
||||
title="Dashboard"
|
||||
actions={
|
||||
<>
|
||||
<Button
|
||||
size="icon"
|
||||
variant="secondary"
|
||||
icon="RefreshCw"
|
||||
aria-label="Refresh dashboard"
|
||||
title="Refresh dashboard"
|
||||
loading={isRefreshing}
|
||||
onClick={() => void refresh()}
|
||||
/>
|
||||
<a href="/api/export?clientId=all" download>
|
||||
<Button size="xs" variant="secondary" icon="Download">
|
||||
Export
|
||||
</Button>
|
||||
</a>
|
||||
<Link href="/vcon/clients">
|
||||
<Button size="xs" variant="primary" icon="Plus">
|
||||
New client
|
||||
</Button>
|
||||
</Link>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<StatStrip
|
||||
items={[
|
||||
{ label: 'Events today', value: eventsToday.toLocaleString() },
|
||||
{ label: 'Active clients', value: `${activeClients} / ${totalClients}`, dot: activeClients > 0 ? 'ok' : 'off' },
|
||||
{
|
||||
label: 'Active clients',
|
||||
value: `${activeClients} / ${totalClients}`,
|
||||
dot: activeClients > 0 ? 'ok' : 'off',
|
||||
},
|
||||
{ label: 'Storage used', value: `${storageValue} ${storageUnit}` },
|
||||
{ label: 'Stored rows', value: totalRows.toLocaleString() },
|
||||
]}
|
||||
/>
|
||||
|
||||
<div className="grid min-w-0 grid-cols-1 gap-2.5 lg:grid-cols-[minmax(0,1.85fr)_minmax(0,1fr)]">
|
||||
<div className="grid min-w-0 grid-cols-1 gap-3 lg:grid-cols-[minmax(0,1.85fr)_minmax(0,1fr)]">
|
||||
<Panel
|
||||
title="Recent events"
|
||||
meta={`${events.length}`}
|
||||
action={<Link href="/vcon/data" className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]">View all →</Link>}
|
||||
action={
|
||||
<Link
|
||||
href="/vcon/data"
|
||||
className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]"
|
||||
>
|
||||
View all →
|
||||
</Link>
|
||||
}
|
||||
>
|
||||
<TableWrap className="border-0 bg-transparent">
|
||||
<Table className="min-w-[560px] text-[11px]">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Time</TableHead>
|
||||
@@ -164,16 +192,18 @@ export const DashboardOverview: React.FC = () => {
|
||||
<TableBody>
|
||||
{events.map((event) => (
|
||||
<TableRow key={event.eventId}>
|
||||
<TableCell className="font-mono text-[10.5px] tabular-nums text-[var(--text-faint)]">
|
||||
{event.timestamp.includes('T') ? new Date(event.timestamp).toLocaleTimeString() : event.timestamp}
|
||||
<TableCell className="font-mono tabular-nums text-[var(--text-faint)]">
|
||||
{event.timestamp.includes('T')
|
||||
? new Date(event.timestamp).toLocaleTimeString()
|
||||
: event.timestamp}
|
||||
</TableCell>
|
||||
<TableCell className="font-mono text-[11px] text-[var(--accent)]">
|
||||
<TableCell className="font-mono text-[var(--accent)]">
|
||||
{event.clientId}
|
||||
</TableCell>
|
||||
<TableCell className="max-w-[380px] truncate font-mono text-[11px] text-[var(--text)]">
|
||||
<TableCell className="max-w-[380px] truncate font-mono text-[var(--text)]">
|
||||
{event.payload}
|
||||
</TableCell>
|
||||
<TableCell className="text-right font-mono text-[10.5px] tabular-nums text-[var(--text-faint)]">
|
||||
<TableCell className="text-right font-mono tabular-nums text-[var(--text-faint)]">
|
||||
{new TextEncoder().encode(event.payload).length} B
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
@@ -183,10 +213,17 @@ export const DashboardOverview: React.FC = () => {
|
||||
</TableWrap>
|
||||
</Panel>
|
||||
|
||||
<div className="flex min-w-0 flex-col gap-2.5">
|
||||
<div className="flex min-w-0 flex-col gap-3">
|
||||
<Panel
|
||||
title="Clients"
|
||||
action={<Link href="/vcon/clients" className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]">All →</Link>}
|
||||
action={
|
||||
<Link
|
||||
href="/vcon/clients"
|
||||
className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]"
|
||||
>
|
||||
All →
|
||||
</Link>
|
||||
}
|
||||
>
|
||||
<ClientList clients={clients} limit={4} />
|
||||
</Panel>
|
||||
@@ -194,14 +231,29 @@ export const DashboardOverview: React.FC = () => {
|
||||
<Panel
|
||||
title="Storage"
|
||||
meta={`${totalFiles}`}
|
||||
action={<a href="/api/export?clientId=all" download className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]">Export ↓</a>}
|
||||
action={
|
||||
<a
|
||||
href="/api/export?clientId=all"
|
||||
download
|
||||
className="text-[11px] text-[var(--text-muted)] hover:text-[var(--text)]"
|
||||
>
|
||||
Export ↓
|
||||
</a>
|
||||
}
|
||||
>
|
||||
<div className="divide-y divide-[var(--border)]">
|
||||
{files.slice(0, 3).map((file) => (
|
||||
<div key={`${file.clientId}/${file.filename}`} className="flex items-center justify-between gap-2.5 px-3 py-1.5">
|
||||
<div
|
||||
key={`${file.clientId}/${file.filename}`}
|
||||
className="flex items-center justify-between gap-2.5 px-3 py-1.5"
|
||||
>
|
||||
<div className="min-w-0">
|
||||
<p className="truncate text-[11.5px] text-[var(--text)]">{file.filename}</p>
|
||||
<p className="truncate font-mono text-[10px] text-[var(--text-faint)]">{file.clientId}</p>
|
||||
<p className="truncate text-[11.5px] text-[var(--text)]">
|
||||
{file.filename}
|
||||
</p>
|
||||
<p className="truncate font-mono text-[10px] text-[var(--text-faint)]">
|
||||
{file.clientId}
|
||||
</p>
|
||||
</div>
|
||||
<span className="shrink-0 font-mono text-[10.5px] tabular-nums text-[var(--text-muted)]">
|
||||
{(file.sizeBytes / 1024).toFixed(1)} KB
|
||||
@@ -209,13 +261,15 @@ export const DashboardOverview: React.FC = () => {
|
||||
</div>
|
||||
))}
|
||||
{files.length === 0 && (
|
||||
<p className="px-3 py-4 text-[11px] text-[var(--text-faint)]">No CSV files</p>
|
||||
<p className="px-3 py-4 text-[11px] text-[var(--text-faint)]">
|
||||
No CSV files
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</Panel>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</PageShell>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user