'use client'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { Modal } from '@/components/ui/Modal'; import { useToast } from '@/components/ui/Toast'; import { useState } from 'react'; import { CredentialsDisplay } from './ClientsTable'; import type { ClientCredentials } from './clients/types'; interface NewClientModalProps { open: boolean; onClose: () => void; onCreated: () => Promise; } export function NewClientModal({ open, onClose, onCreated }: NewClientModalProps) { const { success, error } = useToast(); const [clientId, setClientId] = useState(''); const [name, setName] = useState(''); const [rateLimit, setRateLimit] = useState('100'); const [storageQuotaMb, setStorageQuotaMb] = useState('0'); const [allowedIps, setAllowedIps] = useState(''); const [expiresAt, setExpiresAt] = useState(''); const [submitting, setSubmitting] = useState(false); const [credentials, setCredentials] = useState(null); const createClient = async (event: React.FormEvent) => { event.preventDefault(); const trimmedId = clientId.trim(); const trimmedName = name.trim(); if (!trimmedId || !trimmedName) { error('Validation error', 'Client ID and Name are required.'); return; } const numericRate = Number(rateLimit); if (!Number.isInteger(numericRate) || numericRate < 1 || numericRate > 100000) { error('Validation error', 'Rate limit must be an integer between 1 and 100,000 events/s.'); return; } const numericQuotaMb = Number(storageQuotaMb.trim() === '' ? '0' : storageQuotaMb); if (!Number.isInteger(numericQuotaMb) || numericQuotaMb < 0 || numericQuotaMb > 1048576) { error('Validation error', 'Storage quota must be an integer between 0 and 1,048,576 MB (0 = unlimited).'); return; } let normalizedExpiresAt: string | null = null; if (expiresAt.trim()) { const parsedDate = new Date(expiresAt.trim()); if (Number.isNaN(parsedDate.getTime())) { error('Validation error', 'Token expiration must be a valid date/time.'); return; } normalizedExpiresAt = parsedDate.toISOString(); } setSubmitting(true); try { const response = await fetch('/api/clients', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ clientId: trimmedId, name: trimmedName, rateLimit: numericRate, storageQuotaMb: numericQuotaMb, allowedIps: allowedIps.trim(), expiresAt: normalizedExpiresAt, }), }); if (response.status === 401) { window.location.assign('/vcon/login'); return; } const data = await response.json(); if (!response.ok) throw new Error(data.error || 'Client registration failed'); setCredentials(data.client as ClientCredentials); setClientId(''); setName(''); setRateLimit('100'); setStorageQuotaMb('0'); setAllowedIps(''); setExpiresAt(''); onClose(); success('Client registered', `${data.client.clientId} is ready to connect.`); await onCreated(); } catch (createError) { error('Registration failed', createError instanceof Error ? createError.message : 'Unknown error'); } finally { setSubmitting(false); } }; return ( <> } >
setClientId(event.target.value)} required /> setName(event.target.value)} required /> setRateLimit(event.target.value)} /> setStorageQuotaMb(event.target.value)} /> setAllowedIps(event.target.value)} className="font-mono text-[11px]" /> setExpiresAt(event.target.value)} className="font-mono text-[11px]" />
setCredentials(null)} title={`Credentials: ${credentials?.clientId || ''}`} footer={} > {credentials && } ); }