import { FC, useEffect, useState } from "react"; import { DialogTitle, DialogContent, Dialog, DialogHeader, Form, FormItem, FormControl, FormMessage, FormLabel, Input, FormField, Checkbox, Button, SelectItem, SelectContent, SelectValue, Select, SelectTrigger, Alert, AlertTitle, AlertDescription } from '@marzneshin/components'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { useTranslation } from 'react-i18next'; import { HostSchema, HostType, getDefaultValues, useHostsCreationMutation, useHostsUpdateMutation } from "@marzneshin/features/hosts"; import { ExclamationTriangleIcon } from "@radix-ui/react-icons"; interface MutationDialogProps { entity: HostType | null open: boolean inboundId: number onOpenChange: (state: boolean) => void } export const HostsMutationDialog: FC = ({ inboundId, entity, open, onOpenChange, }) => { const form = useForm({ defaultValues: entity ? entity : getDefaultValues(), resolver: zodResolver(HostSchema) }); const updateMutation = useHostsUpdateMutation(); const createMutation = useHostsCreationMutation(); const { t } = useTranslation(); const security = form.watch().security const [extraSecurity, setExtraSecurity] = useState(["tls", "inbound_default"].includes(security)) useEffect(() => { setExtraSecurity(["tls", "inbound_default"].includes(security)) }, [security, setExtraSecurity]) const submit = (values: HostType) => { if (entity) { updateMutation.mutate({ inboundId, host: values }); } else { createMutation.mutate({ inboundId, host: values }); } onOpenChange(false); }; useEffect(() => { if (entity) form.reset(entity); else form.reset(getDefaultValues()); }, [entity, form]); return ( {entity ? t('page.hosts.dialogs.edition.title') : t('page.hosts.dialogs.creation.title')}
( {t('name')} )} />
( {t('address')} )} /> ( {t('port')} )} />
( {t('path')} )} /> ( {t('host')} )} /> ( {t('security')} )} /> {extraSecurity && <> ( {t('SNI')} )} /> ( {t('alpn')} )} /> ( {t('fingerprint')} )} /> (
{t('page.hosts.allowinsecure')}
{form.getValues().allowinsecure && Warning {t('page.hosts.dialogs.mutation.allowinsecure.alert')} }
)} /> }
); };