import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Form, Input, message, Modal, Select } from 'antd'; import { HttpUtil } from '@/utils'; import { CustomGeoFormSchema } from '@/schemas/xray'; export interface CustomGeoRecord { id: number; type: 'geosite' | 'geoip'; alias: string; url: string; } interface CustomGeoFormModalProps { open: boolean; record: CustomGeoRecord | null; onClose: () => void; onSaved: () => void; } export default function CustomGeoFormModal({ open, record, onClose, onSaved, }: CustomGeoFormModalProps) { const { t } = useTranslation(); const [messageApi, messageContextHolder] = message.useMessage(); const [type, setType] = useState<'geosite' | 'geoip'>('geosite'); const [alias, setAlias] = useState(''); const [url, setUrl] = useState(''); const [saving, setSaving] = useState(false); const editing = record != null; useEffect(() => { if (!open) return; if (record) { setType(record.type); setAlias(record.alias); setUrl(record.url); } else { setType('geosite'); setAlias(''); setUrl(''); } }, [open, record]); async function submit() { const validated = CustomGeoFormSchema.safeParse({ type, alias, url }); if (!validated.success) { messageApi.error(t(validated.error.issues[0]?.message ?? 'somethingWentWrong')); return; } setSaving(true); try { const apiUrl = editing ? `/panel/api/custom-geo/update/${record!.id}` : '/panel/api/custom-geo/add'; const msg = await HttpUtil.post(apiUrl, validated.data); if (msg?.success) { onSaved(); onClose(); } } finally { setSaving(false); } } return ( <> {messageContextHolder}
setAlias(e.target.value)} /> setUrl(e.target.value)} />
); }