'use client'; import { zodResolver } from '@hookform/resolvers/zod'; import { Button } from '@workspace/ui/components/button'; import { Card, CardContent, CardHeader, CardTitle } from '@workspace/ui/components/card'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@workspace/ui/components/form'; import { ScrollArea } from '@workspace/ui/components/scroll-area'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@workspace/ui/components/select'; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, } from '@workspace/ui/components/sheet'; import { Switch } from '@workspace/ui/components/switch'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@workspace/ui/components/tabs'; import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input'; import { Icon } from '@workspace/ui/custom-components/icon'; import { useTranslations } from 'next-intl'; import { useEffect, useMemo, useState } from 'react'; import { useFieldArray, useForm, useWatch } from 'react-hook-form'; import { toast } from 'sonner'; import { FINGERPRINTS, FLOWS, formScheme, getLabel, getProtocolDefaultConfig, LABELS, protocols as PROTOCOLS, SECURITY, SS_CIPHERS, TRANSPORTS, TUIC_CONGESTION, TUIC_UDP_RELAY_MODES, } from './form-scheme'; interface ServerFormProps { onSubmit: (data: T) => Promise | boolean; initialValues?: T | any; loading?: boolean; trigger: string; title: string; } function titleCase(s: string) { return s.charAt(0).toUpperCase() + s.slice(1); } function normalizeValues(raw: any) { return { name: raw?.name ?? '', address: raw?.address ?? '', country: raw?.country ?? '', city: raw?.city ?? '', ratio: Number(raw?.ratio ?? 1), protocols: Array.isArray(raw?.protocols) ? raw.protocols : [], }; } export default function ServerForm({ onSubmit, initialValues, loading, trigger, title, }: Readonly>) { const t = useTranslations('servers'); const [open, setOpen] = useState(false); const [activeType, setActiveType] = useState<(typeof PROTOCOLS)[number]>('shadowsocks'); const defaultValues = useMemo( () => normalizeValues({ name: '', address: '', country: '', city: '', ratio: 1, protocols: [], }), [], ); const form = useForm({ resolver: zodResolver(formScheme), defaultValues }); const { control } = form; const { fields, append, remove } = useFieldArray({ control, name: 'protocols' }); const protocolsValues = useWatch({ control, name: 'protocols' }); useEffect(() => { const normalized = normalizeValues(initialValues || {}); const byType = new Map(); (Array.isArray(normalized.protocols) ? normalized.protocols : []).forEach((p: any) => { if (p && p.type) byType.set(String(p.type), p); }); const full = PROTOCOLS.map((t) => byType.get(t) || getProtocolDefaultConfig(t)); form.reset({ ...normalized, protocols: full }); setActiveType('shadowsocks'); // eslint-disable-next-line react-hooks/exhaustive-deps }, [initialValues]); async function handleSubmit(data: { [x: string]: any }) { const all = Array.isArray(data?.protocols) ? data.protocols : []; const filtered = all .filter((p: any) => { const v = (p ?? {}).port; const n = Number(v); return Number.isFinite(n) && n > 0 && n <= 65535; }) .map((p: any) => ({ ...p, port: Number(p.port) })); if (filtered.length === 0) { toast.error(t('validation_failed')); return; } const body = { name: data?.name, country: data?.country, city: data?.city, ratio: Number(data?.ratio ?? 1), address: data?.address, protocols: filtered, } as unknown as T; const ok = await onSubmit(body as unknown as T); if (ok) setOpen(false); } // inlined protocol editor below in TabsContent return ( {title}
( {t('name')} field.onChange(v)} /> )} /> ( {t('country')} field.onChange(v)} /> )} /> ( {t('city')} field.onChange(v)} /> )} />
( {t('address')} field.onChange(v)} /> )} /> ( {t('traffic_ratio')} field.onChange(v)} /> )} />
setActiveType(v as any)}> {PROTOCOLS.map((type) => ( {titleCase(type)} ))} {PROTOCOLS.map((type) => { const i = Math.max( 0, PROTOCOLS.findIndex((t) => t === type), ); const current = Array.isArray(protocolsValues) ? protocolsValues[i] || {} : {}; const transport = (current?.transport as string | undefined) ?? 'tcp'; const security = current?.security as string | undefined; const cipher = current?.cipher as string | undefined; return (
( {t('port')} field.onChange(v)} /> )} /> {type === 'shadowsocks' && ( <> ( {t('encryption_method')} )} /> {[ '2022-blake3-aes-128-gcm', '2022-blake3-aes-256-gcm', '2022-blake3-chacha20-poly1305', ].includes((cipher || '').toString()) && ( ( {t('server_key')} field.onChange(v)} /> )} /> )} )} {type === 'vless' && ( ( {t('flow')} )} /> )} {type === 'hysteria2' && ( <> ( {t('obfs_password')} field.onChange(v)} /> )} /> ( {t('hop_ports')} field.onChange(v)} /> )} /> ( {t('hop_interval')} field.onChange(v)} /> )} /> )} {type === 'tuic' && ( <> ( {t('udp_relay_mode')} )} /> ( {t('congestion_controller')} )} />
( {t('disable_sni')}
field.onChange(checked)} />
)} /> ( {t('reduce_rtt')}
field.onChange(checked)} />
)} />
)}
{['vmess', 'vless', 'trojan'].includes(type) && ( {t('transport_title')} ( )} /> {transport !== 'tcp' && ( {['websocket', 'http2', 'httpupgrade'].includes( transport as string, ) && ( <> ( HOST { form.setValue(field.name, value); }} /> )} /> ( {t('path')} field.onChange(v)} /> )} /> )} {transport === 'grpc' && ( ( {t('service_name')} field.onChange(v)} /> )} /> )} )} )} {['vmess', 'vless', 'trojan', 'anytls', 'tuic', 'hysteria2'].includes( type, ) && ( {t('security_title')} {['vmess', 'vless', 'trojan'].includes(type) && ( ( )} /> )} {(['anytls', 'tuic', 'hysteria2'].includes(type) || (['vmess', 'vless', 'trojan'].includes(type) && security !== 'none')) && ( ( {t('security_sni')} field.onChange(v)} /> )} /> {type === 'vless' && security === 'reality' && ( <> ( {t('security_server_address')} field.onChange(v)} /> )} /> ( {t('security_server_port')} field.onChange(v)} /> )} /> ( {t('security_private_key')} field.onChange(v)} /> )} /> ( {t('security_public_key')} field.onChange(v)} /> )} /> ( {t('security_short_id')} field.onChange(v)} /> )} /> )} ( {t('security_fingerprint')} )} /> ( {t('security_allow_insecure')}
field.onChange(checked)} />
)} />
)}
)}
); })}
); }