'use client'; import { Button } from '@/components/ui/Button'; import { CopyButton } from '@/components/ui/CopyButton'; import { Modal } from '@/components/ui/Modal'; import { Table, TableBody, TableCell, TableEmpty, TableHead, TableHeader, TableRow, TableWrap, } from '@/components/ui/Table'; import { useToast } from '@/components/ui/Toast'; import { useState } from 'react'; import type { ClientCredentials, ClientItem } from './clients/types'; interface ClientsTableProps { clients: ClientItem[]; now: number; onStatusChange: () => Promise; } function relativeTime(timestamp: string | undefined, now: number) { if (!timestamp) return 'Never'; const seconds = Math.max(0, Math.floor((now - new Date(timestamp).getTime()) / 1000)); if (seconds < 10) return 'Just now'; if (seconds < 60) return `${seconds}s ago`; if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`; if (seconds < 86_400) return `${Math.floor(seconds / 3600)}h ago`; return `${Math.floor(seconds / 86_400)}d ago`; } export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps) { const { success, error } = useToast(); const [credentials, setCredentials] = useState(null); const rotateKey = async (client: ClientItem) => { if (!window.confirm(`Rotate credentials for ${client.clientId}? Existing credentials will stop working.`)) return; try { const response = await fetch(`/api/clients/${encodeURIComponent(client.clientId)}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'regenerate_token' }), }); const data = await response.json(); if (!response.ok) throw new Error(data.error || 'Unable to rotate credentials'); setCredentials(data.client); success('Credentials rotated', `New credentials issued for ${client.clientId}.`); await onStatusChange(); } catch (rotateError) { error('Credential rotation failed', rotateError instanceof Error ? rotateError.message : 'Unknown error'); } }; const toggleStatus = async (client: ClientItem) => { const status = client.status === 'active' ? 'revoked' : 'active'; if (status === 'revoked' && !window.confirm(`Revoke access for ${client.clientId}?`)) return; try { const response = await fetch(`/api/clients/${encodeURIComponent(client.clientId)}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ status }), }); const data = await response.json(); if (!response.ok) throw new Error(data.error || 'Unable to update client status'); success('Client status updated', `${client.clientId} is now ${status}.`); await onStatusChange(); } catch (statusError) { error('Status update failed', statusError instanceof Error ? statusError.message : 'Unknown error'); } }; return ( <> Client API key Events Last seen Actions {clients.length === 0 ? ( ) : ( clients.map((client) => { const online = client.status === 'active' && Boolean(client.lastActive) && now - new Date(client.lastActive!).getTime() < 60_000; const pending = client.status === 'active' && !client.lastActive; const label = pending ? 'pending' : online ? 'online' : 'offline'; const dotClass = online ? 'bg-[var(--accent)]' : pending ? 'bg-[var(--warning)]' : 'bg-[var(--text-faint)]'; return (
{client.name}
{client.clientId}
sec_•••••••• {client.totalEvents.toLocaleString()} {client.lastActive ? new Date(client.lastActive).toLocaleTimeString([], { hour12: false }) : '—'} {relativeTime(client.lastActive, now)}
); }) )}
setCredentials(null)} title={`Credentials: ${credentials?.clientId || ''}`} description="Copy the new credentials now. The secret is only shown after rotation." footer={} > {credentials && (
)}
); } function Credential({ label, value }: { label: string; value: string }) { return (
{label}
{value}
); }