2026-10-23 19:56:38 -07:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { Button } from '@/components/ui/Button';
|
2026-11-16 21:58:18 +06:00
|
|
|
import { Input } from '@/components/ui/Input';
|
2026-10-23 19:56:38 -07:00
|
|
|
import { Modal } from '@/components/ui/Modal';
|
|
|
|
|
import { useToast } from '@/components/ui/Toast';
|
|
|
|
|
import { useState } from 'react';
|
2026-11-16 21:58:18 +06:00
|
|
|
import { CredentialsDisplay } from './ClientsTable';
|
2026-10-23 19:56:38 -07:00
|
|
|
import type { ClientCredentials } from './clients/types';
|
|
|
|
|
|
|
|
|
|
interface NewClientModalProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
onCreated: () => Promise<void>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<ClientCredentials | null>(null);
|
|
|
|
|
|
|
|
|
|
const createClient = async (event: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
|
event.preventDefault();
|
2026-11-16 21:58:18 +06:00
|
|
|
const trimmedId = clientId.trim();
|
|
|
|
|
const trimmedName = name.trim();
|
|
|
|
|
if (!trimmedId || !trimmedName) {
|
|
|
|
|
error('Validation error', 'Client ID and Name are required.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const clampedRateLimit = Math.max(1, Math.min(5000, Number(rateLimit) || 100));
|
2026-10-23 19:56:38 -07:00
|
|
|
setSubmitting(true);
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('/api/clients', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2026-11-16 21:58:18 +06:00
|
|
|
body: JSON.stringify({ clientId: trimmedId, name: trimmedName, rateLimit: clampedRateLimit }),
|
2026-10-23 19:56:38 -07:00
|
|
|
});
|
|
|
|
|
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 (
|
|
|
|
|
<>
|
|
|
|
|
<Modal
|
|
|
|
|
isOpen={open}
|
|
|
|
|
onClose={onClose}
|
2026-11-16 21:58:18 +06:00
|
|
|
title="New client"
|
2026-10-23 19:56:38 -07:00
|
|
|
maxWidth="xs"
|
|
|
|
|
footer={
|
|
|
|
|
<>
|
2026-11-16 21:58:18 +06:00
|
|
|
<Button type="button" variant="ghost" size="xs" onClick={onClose}>Cancel</Button>
|
|
|
|
|
<Button type="submit" form="new-client-form" variant="primary" size="xs" loading={submitting}>Create</Button>
|
2026-10-23 19:56:38 -07:00
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
>
|
2026-11-16 21:58:18 +06:00
|
|
|
<form id="new-client-form" onSubmit={createClient} className="flex flex-col gap-2.5">
|
2026-10-23 19:56:38 -07:00
|
|
|
<Input
|
|
|
|
|
compact
|
|
|
|
|
label="Client ID"
|
2026-11-16 21:58:18 +06:00
|
|
|
placeholder="py_order_service_04"
|
2026-10-23 19:56:38 -07:00
|
|
|
value={clientId}
|
|
|
|
|
onChange={(event) => setClientId(event.target.value)}
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
compact
|
2026-11-16 21:58:18 +06:00
|
|
|
label="Name"
|
|
|
|
|
placeholder="Order Service"
|
2026-10-23 19:56:38 -07:00
|
|
|
value={name}
|
|
|
|
|
onChange={(event) => setName(event.target.value)}
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
compact
|
2026-11-16 21:58:18 +06:00
|
|
|
label="Rate limit (req/s)"
|
2026-10-23 19:56:38 -07:00
|
|
|
type="number"
|
|
|
|
|
min="1"
|
|
|
|
|
max="5000"
|
|
|
|
|
value={rateLimit}
|
|
|
|
|
onChange={(event) => setRateLimit(event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</form>
|
|
|
|
|
</Modal>
|
|
|
|
|
|
|
|
|
|
<Modal
|
|
|
|
|
isOpen={Boolean(credentials)}
|
|
|
|
|
onClose={() => setCredentials(null)}
|
|
|
|
|
title={`Credentials: ${credentials?.clientId || ''}`}
|
2026-11-16 21:58:18 +06:00
|
|
|
footer={<Button type="button" size="xs" onClick={() => setCredentials(null)}>Close</Button>}
|
2026-10-23 19:56:38 -07:00
|
|
|
>
|
2026-11-16 21:58:18 +06:00
|
|
|
{credentials && <CredentialsDisplay credentials={credentials} />}
|
2026-10-23 19:56:38 -07:00
|
|
|
</Modal>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|