2026-10-23 11:43:49 -05:00
|
|
|
'use client';
|
|
|
|
|
|
2026-11-17 00:24:33 +06:00
|
|
|
import { PageShell } from '@/components/layout/PageShell';
|
2026-11-21 00:06:55 +06:00
|
|
|
import { useVconRole } from '@/components/layout/VconShell';
|
|
|
|
|
import type { ClientItem } from '@/components/sections/clients/types';
|
2026-10-23 19:56:38 -07:00
|
|
|
import { ClientsTable } from '@/components/sections/ClientsTable';
|
|
|
|
|
import { NewClientModal } from '@/components/sections/NewClientModal';
|
2026-10-23 11:43:49 -05:00
|
|
|
import { Button } from '@/components/ui/Button';
|
2026-11-21 00:06:55 +06:00
|
|
|
import { Input } from '@/components/ui/Input';
|
2026-11-17 00:24:33 +06:00
|
|
|
import { Panel } from '@/components/ui/Panel';
|
2026-11-21 00:06:55 +06:00
|
|
|
import { Pill } from '@/components/ui/Pill';
|
2026-10-23 19:56:38 -07:00
|
|
|
import { StatStrip } from '@/components/ui/StatStrip';
|
2026-11-16 21:58:18 +06:00
|
|
|
import { useToast } from '@/components/ui/Toast';
|
2026-11-16 23:10:44 +06:00
|
|
|
import { getCached, setCached } from '@/lib/vconCache';
|
2026-11-21 00:06:55 +06:00
|
|
|
import {
|
|
|
|
|
classifyUiClientStatus,
|
|
|
|
|
getVconRoleCapabilities,
|
|
|
|
|
summarizeUiClientCounts,
|
|
|
|
|
type UiClientComputedStatus,
|
|
|
|
|
} from '@/lib/vconUiHelpers';
|
2026-10-23 19:56:38 -07:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-21 00:06:55 +06:00
|
|
|
type ExtendedClientFilter = 'all' | UiClientComputedStatus;
|
|
|
|
|
|
|
|
|
|
const CLIENT_FILTER_TABS: { key: ExtendedClientFilter; label: string }[] = [
|
|
|
|
|
{ key: 'all', label: 'All' },
|
|
|
|
|
{ key: 'online', label: 'Online' },
|
|
|
|
|
{ key: 'offline', label: 'Offline' },
|
|
|
|
|
{ key: 'pending', label: 'Pending' },
|
|
|
|
|
{ key: 'expired', label: 'Expired' },
|
|
|
|
|
{ key: 'revoked', label: 'Revoked' },
|
|
|
|
|
];
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
export default function ClientsPage() {
|
2026-11-16 21:58:18 +06:00
|
|
|
const { error } = useToast();
|
2026-11-21 00:06:55 +06:00
|
|
|
const currentRole = useVconRole();
|
|
|
|
|
const { canOperate } = getVconRoleCapabilities(currentRole);
|
2026-10-23 19:56:38 -07:00
|
|
|
const [modalOpen, setModalOpen] = useState(false);
|
2026-11-17 00:24:33 +06:00
|
|
|
const [clients, setClients] = useState<ClientItem[]>(() =>
|
|
|
|
|
getCached<ClientItem[]>('vcon:clients', [])
|
|
|
|
|
);
|
2026-10-23 19:56:38 -07:00
|
|
|
const [search, setSearch] = useState('');
|
2026-11-21 00:06:55 +06:00
|
|
|
const [statusFilter, setStatusFilter] = useState<ExtendedClientFilter>('all');
|
2026-11-16 23:10:44 +06:00
|
|
|
const [now, setNow] = useState(() => Date.now());
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
const fetchClients = useCallback(async () => {
|
2026-11-18 05:18:55 +06:00
|
|
|
const response = await fetch('/api/clients', { cache: 'no-store' });
|
|
|
|
|
if (response.status === 401) {
|
|
|
|
|
window.location.assign('/vcon/login');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-10-23 19:56:38 -07:00
|
|
|
if (!response.ok) throw new Error('Unable to load clients');
|
|
|
|
|
const data = await response.json();
|
2026-11-16 23:10:44 +06:00
|
|
|
setClients(setCached('vcon:clients', data.clients || []));
|
2026-10-23 11:43:49 -05:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-11-16 21:58:18 +06:00
|
|
|
fetchClients().catch(() => {
|
|
|
|
|
error('Unable to load clients', 'Could not fetch registered clients.');
|
|
|
|
|
});
|
|
|
|
|
}, [error, fetchClients]);
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-10-23 19:56:38 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
const updateClock = () => setNow(Date.now());
|
|
|
|
|
updateClock();
|
|
|
|
|
const interval = setInterval(updateClock, 15_000);
|
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
|
}, []);
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-21 00:06:55 +06:00
|
|
|
const counts = useMemo(
|
|
|
|
|
() => summarizeUiClientCounts(clients, now),
|
|
|
|
|
[clients, now],
|
|
|
|
|
);
|
|
|
|
|
|
2026-10-23 19:56:38 -07:00
|
|
|
const visibleClients = useMemo(() => {
|
|
|
|
|
const query = search.trim().toLowerCase();
|
|
|
|
|
return clients.filter((client) => {
|
2026-11-17 00:24:33 +06:00
|
|
|
const matchesSearch =
|
|
|
|
|
!query ||
|
2026-10-23 19:56:38 -07:00
|
|
|
client.clientId.toLowerCase().includes(query) ||
|
|
|
|
|
client.name.toLowerCase().includes(query);
|
2026-11-21 00:06:55 +06:00
|
|
|
const computedStatus = classifyUiClientStatus(client, now);
|
2026-11-17 00:24:33 +06:00
|
|
|
const matchesStatus =
|
2026-11-21 00:06:55 +06:00
|
|
|
statusFilter === 'all' || computedStatus === statusFilter;
|
2026-10-23 19:56:38 -07:00
|
|
|
return matchesSearch && matchesStatus;
|
|
|
|
|
});
|
|
|
|
|
}, [clients, now, search, statusFilter]);
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-21 00:06:55 +06:00
|
|
|
const filterCounts: Record<ExtendedClientFilter, number> = {
|
|
|
|
|
all: counts.total,
|
|
|
|
|
online: counts.online,
|
|
|
|
|
offline: counts.offline,
|
|
|
|
|
pending: counts.pending,
|
|
|
|
|
expired: counts.expired,
|
|
|
|
|
revoked: counts.revoked,
|
|
|
|
|
};
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
return (
|
2026-11-17 00:24:33 +06:00
|
|
|
<PageShell
|
|
|
|
|
title="Clients"
|
|
|
|
|
actions={
|
2026-11-21 00:06:55 +06:00
|
|
|
canOperate ? (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="primary"
|
|
|
|
|
size="xs"
|
|
|
|
|
icon="Plus"
|
|
|
|
|
onClick={() => setModalOpen(true)}
|
|
|
|
|
>
|
|
|
|
|
New client
|
|
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<span className="border border-[var(--border)] bg-[var(--surface-2)] px-2 py-1 font-mono text-[10.5px] uppercase tracking-wider text-[var(--text-muted)]">
|
|
|
|
|
Read-only ({currentRole})
|
|
|
|
|
</span>
|
|
|
|
|
)
|
2026-11-17 00:24:33 +06:00
|
|
|
}
|
|
|
|
|
>
|
2026-10-23 19:56:38 -07:00
|
|
|
<StatStrip
|
2026-11-21 00:06:55 +06:00
|
|
|
className="md:grid-cols-6"
|
2026-10-23 19:56:38 -07:00
|
|
|
items={[
|
2026-11-21 00:06:55 +06:00
|
|
|
{ label: 'Total', value: counts.total },
|
|
|
|
|
{ label: 'Online', value: counts.online, dot: 'ok' },
|
|
|
|
|
{ label: 'Offline', value: counts.offline, dot: 'off' },
|
|
|
|
|
{ label: 'Pending', value: counts.pending, dot: 'pending' },
|
|
|
|
|
{ label: 'Expired', value: counts.expired, dot: 'off' },
|
|
|
|
|
{ label: 'Revoked', value: counts.revoked, dot: 'off' },
|
2026-10-23 19:56:38 -07:00
|
|
|
]}
|
|
|
|
|
/>
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-17 00:24:33 +06:00
|
|
|
<Panel title="Registered clients" meta={`${visibleClients.length}`}>
|
2026-11-21 00:06:55 +06:00
|
|
|
<div className="flex flex-wrap items-center justify-between gap-2 border-b border-[var(--border)] bg-[var(--surface)] px-3 py-2">
|
|
|
|
|
<div className="w-full flex-1 sm:max-w-xs">
|
|
|
|
|
<Input
|
|
|
|
|
compact
|
|
|
|
|
icon="Search"
|
|
|
|
|
type="search"
|
|
|
|
|
aria-label="Search clients"
|
|
|
|
|
value={search}
|
|
|
|
|
onChange={(event) => setSearch(event.target.value)}
|
|
|
|
|
placeholder="Search client..."
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className="ml-auto flex min-w-0 gap-1 overflow-x-auto"
|
|
|
|
|
role="group"
|
|
|
|
|
aria-label="Filter clients by status"
|
|
|
|
|
>
|
|
|
|
|
{CLIENT_FILTER_TABS.map((filter) => (
|
|
|
|
|
<Pill
|
|
|
|
|
key={filter.key}
|
|
|
|
|
active={statusFilter === filter.key}
|
|
|
|
|
onClick={() => setStatusFilter(filter.key)}
|
|
|
|
|
>
|
|
|
|
|
{filter.label}
|
|
|
|
|
<span className="font-mono text-[9.5px] tabular-nums text-[var(--text-faint)]">
|
|
|
|
|
{filterCounts[filter.key].toLocaleString()}
|
|
|
|
|
</span>
|
|
|
|
|
</Pill>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-11-17 00:24:33 +06:00
|
|
|
<ClientsTable
|
|
|
|
|
clients={visibleClients}
|
|
|
|
|
now={now}
|
2026-11-21 00:06:55 +06:00
|
|
|
canManage={canOperate}
|
2026-11-17 00:24:33 +06:00
|
|
|
onStatusChange={fetchClients}
|
|
|
|
|
/>
|
|
|
|
|
</Panel>
|
|
|
|
|
|
2026-11-21 00:06:55 +06:00
|
|
|
{canOperate && (
|
|
|
|
|
<NewClientModal
|
|
|
|
|
open={modalOpen}
|
|
|
|
|
onClose={() => setModalOpen(false)}
|
|
|
|
|
onCreated={fetchClients}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-11-17 00:24:33 +06:00
|
|
|
</PageShell>
|
2026-10-23 11:43:49 -05:00
|
|
|
);
|
|
|
|
|
}
|