'use client'; import { Button } from '@/components/ui/Button'; import { CopyButton } from '@/components/ui/CopyButton'; import { Input, Textarea } from '@/components/ui/Input'; import { Modal } from '@/components/ui/Modal'; import { useToast } from '@/components/ui/Toast'; import { useState } from 'react'; 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 [submitting, setSubmitting] = useState(false); const [credentials, setCredentials] = useState(null); const createClient = async (event: React.FormEvent) => { event.preventDefault(); setSubmitting(true); try { const response = await fetch('/api/clients', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ clientId: clientId.trim(), name: name.trim(), rateLimit: Number(rateLimit) || 100 }), }); 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'); 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)} className="h-8 border-[var(--border)] bg-[var(--bg)] px-2.5 text-[12.5px] focus:border-[var(--border-strong)]" required /> setName(event.target.value)} className="h-8 border-[var(--border)] bg-[var(--bg)] px-2.5 text-[12.5px] focus:border-[var(--border-strong)]" required /> setRateLimit(event.target.value)} className="h-8 border-[var(--border)] bg-[var(--bg)] px-2.5 text-[12.5px] focus:border-[var(--border-strong)]" />
setCredentials(null)} title={`Credentials: ${credentials?.clientId || ''}`} description="Copy these credentials now. The secret is only shown after creation." footer={} > {credentials && (