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-10-23 19:56:38 -07:00
|
|
|
import type { ClientItem, ClientStatusFilter } from '@/components/sections/clients/types';
|
|
|
|
|
import { ClientsFilters } from '@/components/sections/ClientsFilters';
|
|
|
|
|
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-17 00:24:33 +06:00
|
|
|
import { Panel } from '@/components/ui/Panel';
|
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-10-23 19:56:38 -07:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
export default function ClientsPage() {
|
2026-11-16 21:58:18 +06:00
|
|
|
const { error } = useToast();
|
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('');
|
|
|
|
|
const [statusFilter, setStatusFilter] = useState<ClientStatusFilter>('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-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-18 05:18:55 +06:00
|
|
|
const revoked = client.status === 'revoked';
|
2026-11-17 00:24:33 +06:00
|
|
|
const online =
|
|
|
|
|
client.status === 'active' &&
|
|
|
|
|
Boolean(client.lastActive) &&
|
2026-10-23 19:56:38 -07:00
|
|
|
now - new Date(client.lastActive!).getTime() < 60_000;
|
|
|
|
|
const pending = client.status === 'active' && !client.lastActive;
|
2026-11-18 05:18:55 +06:00
|
|
|
const offline = client.status === 'active' && !online && !pending;
|
2026-11-17 00:24:33 +06:00
|
|
|
const matchesStatus =
|
|
|
|
|
statusFilter === 'all' ||
|
2026-10-23 19:56:38 -07:00
|
|
|
(statusFilter === 'online' && online) ||
|
2026-11-18 05:18:55 +06:00
|
|
|
(statusFilter === 'offline' && offline) ||
|
|
|
|
|
(statusFilter === 'pending' && pending) ||
|
|
|
|
|
(statusFilter === 'revoked' && revoked);
|
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-17 00:24:33 +06:00
|
|
|
const onlineCount = clients.filter(
|
|
|
|
|
(client) =>
|
|
|
|
|
client.status === 'active' &&
|
|
|
|
|
client.lastActive &&
|
|
|
|
|
now - new Date(client.lastActive).getTime() < 60_000
|
2026-10-23 19:56:38 -07:00
|
|
|
).length;
|
2026-11-17 00:24:33 +06:00
|
|
|
const pendingCount = clients.filter(
|
|
|
|
|
(client) => client.status === 'active' && !client.lastActive
|
2026-10-23 19:56:38 -07:00
|
|
|
).length;
|
2026-11-18 05:18:55 +06:00
|
|
|
const revokedCount = clients.filter(
|
|
|
|
|
(client) => client.status === 'revoked'
|
|
|
|
|
).length;
|
|
|
|
|
const offlineCount = clients.length - onlineCount - pendingCount - revokedCount;
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
return (
|
2026-11-17 00:24:33 +06:00
|
|
|
<PageShell
|
|
|
|
|
title="Clients"
|
|
|
|
|
actions={
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="primary"
|
|
|
|
|
size="xs"
|
|
|
|
|
icon="Plus"
|
|
|
|
|
onClick={() => setModalOpen(true)}
|
|
|
|
|
>
|
|
|
|
|
New client
|
|
|
|
|
</Button>
|
|
|
|
|
}
|
|
|
|
|
>
|
2026-10-23 19:56:38 -07:00
|
|
|
<StatStrip
|
2026-11-18 05:18:55 +06:00
|
|
|
className="md:grid-cols-5"
|
2026-10-23 19:56:38 -07:00
|
|
|
items={[
|
2026-11-16 21:58:18 +06:00
|
|
|
{ label: 'Total', value: clients.length },
|
|
|
|
|
{ label: 'Online', value: onlineCount, dot: 'ok' },
|
2026-10-23 19:56:38 -07:00
|
|
|
{ label: 'Offline', value: offlineCount, dot: 'off' },
|
2026-11-16 21:58:18 +06:00
|
|
|
{ label: 'Pending', value: pendingCount, dot: 'pending' },
|
2026-11-18 05:18:55 +06:00
|
|
|
{ label: 'Revoked', value: revokedCount, 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}`}>
|
|
|
|
|
<ClientsFilters
|
|
|
|
|
search={search}
|
|
|
|
|
onSearchChange={setSearch}
|
|
|
|
|
status={statusFilter}
|
|
|
|
|
onStatusChange={setStatusFilter}
|
|
|
|
|
counts={{
|
|
|
|
|
all: clients.length,
|
|
|
|
|
online: onlineCount,
|
|
|
|
|
offline: offlineCount,
|
|
|
|
|
pending: pendingCount,
|
2026-11-18 05:18:55 +06:00
|
|
|
revoked: revokedCount,
|
2026-11-17 00:24:33 +06:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<ClientsTable
|
|
|
|
|
clients={visibleClients}
|
|
|
|
|
now={now}
|
|
|
|
|
onStatusChange={fetchClients}
|
|
|
|
|
/>
|
|
|
|
|
</Panel>
|
|
|
|
|
|
2026-10-23 19:56:38 -07:00
|
|
|
<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
|
|
|
);
|
|
|
|
|
}
|