Files
DataStack/components/sections/NewClientModal.tsx
T
Team Dark Devil fde2c86b1e refactor: improve auth defaults, update UI, and docs
- Provide fallback values for auth environment variables
- Refine global CSS styles and typography
- Update application metadata descriptions
- Add toast error handling to clients page
- Formalize phase implementation guidelines in AGENTS.md
2026-11-16 21:58:18 +06:00

112 lines
3.6 KiB
TypeScript

'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<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();
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));
setSubmitting(true);
try {
const response = await fetch('/api/clients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ clientId: trimmedId, name: trimmedName, rateLimit: clampedRateLimit }),
});
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}
title="New client"
maxWidth="xs"
footer={
<>
<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>
</>
}
>
<form id="new-client-form" onSubmit={createClient} className="flex flex-col gap-2.5">
<Input
compact
label="Client ID"
placeholder="py_order_service_04"
value={clientId}
onChange={(event) => setClientId(event.target.value)}
required
/>
<Input
compact
label="Name"
placeholder="Order Service"
value={name}
onChange={(event) => setName(event.target.value)}
required
/>
<Input
compact
label="Rate limit (req/s)"
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 || ''}`}
footer={<Button type="button" size="xs" onClick={() => setCredentials(null)}>Close</Button>}
>
{credentials && <CredentialsDisplay credentials={credentials} />}
</Modal>
</>
);
}