import { useState } from 'react' import { useMutation, useQueryClient } from '@tanstack/react-query' import { useTranslation } from 'react-i18next' import { Plus } from 'lucide-react' import { toast } from '@/shared/ui/toast-store' import { Button } from '@/shared/ui/button' import { Input } from '@/shared/ui/input' import { Label } from '@/shared/ui/label' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/shared/ui/dialog' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/shared/ui/select' import { HttpError } from '@/shared/api/client' import type { AvailableInboundDto } from '@/shared/api/types' import { createConfig } from './api' export function CreateConfigDialog({ inbounds }: { inbounds: AvailableInboundDto[] }) { const { t } = useTranslation() const queryClient = useQueryClient() const [open, setOpen] = useState(false) const [inboundId, setInboundId] = useState('') const [label, setLabel] = useState('') const [deviceLimit, setDeviceLimit] = useState('') const createMutation = useMutation({ mutationFn: () => createConfig(inboundId, label.trim() || undefined, deviceLimit ? Number(deviceLimit) : undefined), onSuccess: async () => { toast.success(t('configs.created')) await queryClient.invalidateQueries({ queryKey: ['my-configs'] }) setOpen(false) setInboundId('') setLabel('') setDeviceLimit('') }, onError: (error) => { const message = error instanceof HttpError && error.status === 409 ? t('configs.quotaExceeded') : t('auth.genericError') toast.error(message) }, }) return ( {t('configs.create')}
{ e.preventDefault() if (inboundId) createMutation.mutate() }} >
setLabel(e.target.value)} />
setDeviceLimit(e.target.value)} />
) }