|
@@ -15,7 +15,8 @@ import {
|
|
|
} from 'antd';
|
|
} from 'antd';
|
|
|
import type { NodeRecord } from '@/api/queries/useNodesQuery';
|
|
import type { NodeRecord } from '@/api/queries/useNodesQuery';
|
|
|
import type { Msg } from '@/utils';
|
|
import type { Msg } from '@/utils';
|
|
|
-import type { ProbeResult } from '@/schemas/node';
|
|
|
|
|
|
|
+import { NodeFormSchema, type NodeFormValues, type ProbeResult } from '@/schemas/node';
|
|
|
|
|
+import { antdRule } from '@/utils/zodForm';
|
|
|
import './NodeFormModal.css';
|
|
import './NodeFormModal.css';
|
|
|
|
|
|
|
|
type Mode = 'add' | 'edit';
|
|
type Mode = 'add' | 'edit';
|
|
@@ -29,20 +30,7 @@ interface NodeFormModalProps {
|
|
|
onOpenChange: (open: boolean) => void;
|
|
onOpenChange: (open: boolean) => void;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-interface FormState {
|
|
|
|
|
- id: number;
|
|
|
|
|
- name: string;
|
|
|
|
|
- remark: string;
|
|
|
|
|
- scheme: 'http' | 'https';
|
|
|
|
|
- address: string;
|
|
|
|
|
- port: number;
|
|
|
|
|
- basePath: string;
|
|
|
|
|
- apiToken: string;
|
|
|
|
|
- enable: boolean;
|
|
|
|
|
- allowPrivateAddress: boolean;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function defaultForm(): FormState {
|
|
|
|
|
|
|
+function defaultValues(): NodeFormValues {
|
|
|
return {
|
|
return {
|
|
|
id: 0,
|
|
id: 0,
|
|
|
name: '',
|
|
name: '',
|
|
@@ -66,68 +54,59 @@ export default function NodeFormModal({
|
|
|
onOpenChange,
|
|
onOpenChange,
|
|
|
}: NodeFormModalProps) {
|
|
}: NodeFormModalProps) {
|
|
|
const { t } = useTranslation();
|
|
const { t } = useTranslation();
|
|
|
|
|
+ const [form] = Form.useForm<NodeFormValues>();
|
|
|
const [messageApi, messageContextHolder] = message.useMessage();
|
|
const [messageApi, messageContextHolder] = message.useMessage();
|
|
|
|
|
|
|
|
- const [form, setForm] = useState<FormState>(defaultForm);
|
|
|
|
|
const [submitting, setSubmitting] = useState(false);
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
const [testing, setTesting] = useState(false);
|
|
const [testing, setTesting] = useState(false);
|
|
|
- const [testResult, setTestResult] = useState<{
|
|
|
|
|
- status: string;
|
|
|
|
|
- latencyMs?: number;
|
|
|
|
|
- xrayVersion?: string;
|
|
|
|
|
- error?: string;
|
|
|
|
|
- } | null>(null);
|
|
|
|
|
|
|
+ const [testResult, setTestResult] = useState<ProbeResult | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
if (!open) return;
|
|
if (!open) return;
|
|
|
- const base = defaultForm();
|
|
|
|
|
- const next: FormState = mode === 'edit' && node
|
|
|
|
|
|
|
+ const base = defaultValues();
|
|
|
|
|
+ const next: NodeFormValues = mode === 'edit' && node
|
|
|
? {
|
|
? {
|
|
|
...base,
|
|
...base,
|
|
|
- ...(node as unknown as Partial<FormState>),
|
|
|
|
|
|
|
+ ...(node as unknown as Partial<NodeFormValues>),
|
|
|
id: node.id,
|
|
id: node.id,
|
|
|
scheme: (node.scheme as 'http' | 'https') || base.scheme,
|
|
scheme: (node.scheme as 'http' | 'https') || base.scheme,
|
|
|
}
|
|
}
|
|
|
: base;
|
|
: base;
|
|
|
-
|
|
|
|
|
- setForm(next);
|
|
|
|
|
|
|
+ form.resetFields();
|
|
|
|
|
+ form.setFieldsValue(next);
|
|
|
setTestResult(null);
|
|
setTestResult(null);
|
|
|
-
|
|
|
|
|
- }, [open, mode, node]);
|
|
|
|
|
|
|
+ }, [open, mode, node, form]);
|
|
|
|
|
|
|
|
const title = useMemo(
|
|
const title = useMemo(
|
|
|
() => (mode === 'edit' ? t('pages.nodes.editNode') : t('pages.nodes.addNode')),
|
|
() => (mode === 'edit' ? t('pages.nodes.editNode') : t('pages.nodes.addNode')),
|
|
|
[mode, t],
|
|
[mode, t],
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- function buildPayload(): Partial<NodeRecord> {
|
|
|
|
|
|
|
+ function buildPayload(values: NodeFormValues): Partial<NodeRecord> {
|
|
|
return {
|
|
return {
|
|
|
- id: form.id || 0,
|
|
|
|
|
- name: form.name?.trim() || '',
|
|
|
|
|
- remark: form.remark?.trim() || '',
|
|
|
|
|
- scheme: form.scheme || 'https',
|
|
|
|
|
- address: form.address?.trim() || '',
|
|
|
|
|
- port: Number(form.port) || 0,
|
|
|
|
|
- basePath: form.basePath?.trim() || '/',
|
|
|
|
|
- apiToken: form.apiToken?.trim() || '',
|
|
|
|
|
- enable: !!form.enable,
|
|
|
|
|
- allowPrivateAddress: !!form.allowPrivateAddress,
|
|
|
|
|
|
|
+ id: values.id || 0,
|
|
|
|
|
+ name: values.name.trim(),
|
|
|
|
|
+ remark: values.remark?.trim() || '',
|
|
|
|
|
+ scheme: values.scheme,
|
|
|
|
|
+ address: values.address.trim(),
|
|
|
|
|
+ port: values.port,
|
|
|
|
|
+ basePath: values.basePath.trim() || '/',
|
|
|
|
|
+ apiToken: values.apiToken.trim(),
|
|
|
|
|
+ enable: values.enable,
|
|
|
|
|
+ allowPrivateAddress: values.allowPrivateAddress,
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function update<K extends keyof FormState>(key: K, value: FormState[K]) {
|
|
|
|
|
- setForm((prev) => ({ ...prev, [key]: value }));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
async function onTest() {
|
|
async function onTest() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await form.validateFields(['address', 'port']);
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
setTesting(true);
|
|
setTesting(true);
|
|
|
setTestResult(null);
|
|
setTestResult(null);
|
|
|
try {
|
|
try {
|
|
|
- const payload = buildPayload();
|
|
|
|
|
- if (!payload.address || !payload.port) {
|
|
|
|
|
- messageApi.error(t('pages.nodes.toasts.fillRequired'));
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const payload = buildPayload(form.getFieldsValue(true));
|
|
|
const msg = await testConnection(payload);
|
|
const msg = await testConnection(payload);
|
|
|
if (msg?.success && msg.obj) {
|
|
if (msg?.success && msg.obj) {
|
|
|
setTestResult(msg.obj);
|
|
setTestResult(msg.obj);
|
|
@@ -139,15 +118,15 @@ export default function NodeFormModal({
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async function onSave() {
|
|
|
|
|
- const payload = buildPayload();
|
|
|
|
|
- if (!payload.name || !payload.address || !payload.port) {
|
|
|
|
|
- messageApi.error(t('pages.nodes.toasts.fillRequired'));
|
|
|
|
|
|
|
+ async function onFinish(values: NodeFormValues) {
|
|
|
|
|
+ const result = NodeFormSchema.safeParse(values);
|
|
|
|
|
+ if (!result.success) {
|
|
|
|
|
+ messageApi.error(t(result.error.issues[0]?.message ?? 'pages.nodes.toasts.fillRequired'));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
setSubmitting(true);
|
|
setSubmitting(true);
|
|
|
try {
|
|
try {
|
|
|
- const msg = await save(payload);
|
|
|
|
|
|
|
+ const msg = await save(buildPayload(result.data));
|
|
|
if (msg?.success) {
|
|
if (msg?.success) {
|
|
|
onOpenChange(false);
|
|
onOpenChange(false);
|
|
|
}
|
|
}
|
|
@@ -167,125 +146,127 @@ export default function NodeFormModal({
|
|
|
open={open}
|
|
open={open}
|
|
|
title={title}
|
|
title={title}
|
|
|
confirmLoading={submitting}
|
|
confirmLoading={submitting}
|
|
|
- okText={t('save')}
|
|
|
|
|
- cancelText={t('cancel')}
|
|
|
|
|
- mask={{ closable: false }}
|
|
|
|
|
- width="640px"
|
|
|
|
|
- onOk={onSave}
|
|
|
|
|
- onCancel={close}
|
|
|
|
|
- >
|
|
|
|
|
- <Form layout="vertical">
|
|
|
|
|
- <Row gutter={16}>
|
|
|
|
|
- <Col xs={24} md={12}>
|
|
|
|
|
- <Form.Item label={t('pages.nodes.name')} required>
|
|
|
|
|
- <Input
|
|
|
|
|
- value={form.name}
|
|
|
|
|
- placeholder={t('pages.nodes.namePlaceholder')}
|
|
|
|
|
- onChange={(e) => update('name', e.target.value)}
|
|
|
|
|
- />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
- </Col>
|
|
|
|
|
- <Col xs={24} md={12}>
|
|
|
|
|
- <Form.Item label={t('pages.nodes.remark')}>
|
|
|
|
|
- <Input value={form.remark} onChange={(e) => update('remark', e.target.value)} />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
- </Col>
|
|
|
|
|
- </Row>
|
|
|
|
|
|
|
+ okText={t('save')}
|
|
|
|
|
+ cancelText={t('cancel')}
|
|
|
|
|
+ mask={{ closable: false }}
|
|
|
|
|
+ width="640px"
|
|
|
|
|
+ onOk={() => form.submit()}
|
|
|
|
|
+ onCancel={close}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Form
|
|
|
|
|
+ form={form}
|
|
|
|
|
+ layout="vertical"
|
|
|
|
|
+ initialValues={defaultValues()}
|
|
|
|
|
+ onFinish={onFinish}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Row gutter={16}>
|
|
|
|
|
+ <Col xs={24} md={12}>
|
|
|
|
|
+ <Form.Item
|
|
|
|
|
+ label={t('pages.nodes.name')}
|
|
|
|
|
+ name="name"
|
|
|
|
|
+ rules={[antdRule(NodeFormSchema.shape.name, t)]}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Input placeholder={t('pages.nodes.namePlaceholder')} />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
+ </Col>
|
|
|
|
|
+ <Col xs={24} md={12}>
|
|
|
|
|
+ <Form.Item label={t('pages.nodes.remark')} name="remark">
|
|
|
|
|
+ <Input />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
+ </Col>
|
|
|
|
|
+ </Row>
|
|
|
|
|
|
|
|
- <Row gutter={16}>
|
|
|
|
|
- <Col xs={24} md={6}>
|
|
|
|
|
- <Form.Item label={t('pages.nodes.scheme')}>
|
|
|
|
|
- <Select
|
|
|
|
|
- value={form.scheme}
|
|
|
|
|
- onChange={(v) => update('scheme', v)}
|
|
|
|
|
- options={[
|
|
|
|
|
- { value: 'https', label: 'https' },
|
|
|
|
|
- { value: 'http', label: 'http' },
|
|
|
|
|
- ]}
|
|
|
|
|
- />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
- </Col>
|
|
|
|
|
- <Col xs={24} md={12}>
|
|
|
|
|
- <Form.Item label={t('pages.nodes.address')} required>
|
|
|
|
|
- <Input
|
|
|
|
|
- value={form.address}
|
|
|
|
|
- placeholder={t('pages.nodes.addressPlaceholder')}
|
|
|
|
|
- onChange={(e) => update('address', e.target.value)}
|
|
|
|
|
- />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
- </Col>
|
|
|
|
|
- <Col xs={24} md={6}>
|
|
|
|
|
- <Form.Item label={t('pages.nodes.port')} required>
|
|
|
|
|
- <InputNumber
|
|
|
|
|
- value={form.port}
|
|
|
|
|
- min={1}
|
|
|
|
|
- max={65535}
|
|
|
|
|
- style={{ width: '100%' }}
|
|
|
|
|
- onChange={(v) => update('port', Number(v) || 0)}
|
|
|
|
|
- />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
- </Col>
|
|
|
|
|
- </Row>
|
|
|
|
|
|
|
+ <Row gutter={16}>
|
|
|
|
|
+ <Col xs={24} md={6}>
|
|
|
|
|
+ <Form.Item label={t('pages.nodes.scheme')} name="scheme">
|
|
|
|
|
+ <Select
|
|
|
|
|
+ options={[
|
|
|
|
|
+ { value: 'https', label: 'https' },
|
|
|
|
|
+ { value: 'http', label: 'http' },
|
|
|
|
|
+ ]}
|
|
|
|
|
+ />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
+ </Col>
|
|
|
|
|
+ <Col xs={24} md={12}>
|
|
|
|
|
+ <Form.Item
|
|
|
|
|
+ label={t('pages.nodes.address')}
|
|
|
|
|
+ name="address"
|
|
|
|
|
+ rules={[antdRule(NodeFormSchema.shape.address, t)]}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Input placeholder={t('pages.nodes.addressPlaceholder')} />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
+ </Col>
|
|
|
|
|
+ <Col xs={24} md={6}>
|
|
|
|
|
+ <Form.Item
|
|
|
|
|
+ label={t('pages.nodes.port')}
|
|
|
|
|
+ name="port"
|
|
|
|
|
+ rules={[antdRule(NodeFormSchema.shape.port, t)]}
|
|
|
|
|
+ >
|
|
|
|
|
+ <InputNumber min={1} max={65535} style={{ width: '100%' }} />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
+ </Col>
|
|
|
|
|
+ </Row>
|
|
|
|
|
|
|
|
- <Row gutter={16}>
|
|
|
|
|
- <Col xs={24} md={12}>
|
|
|
|
|
- <Form.Item label={t('pages.nodes.basePath')}>
|
|
|
|
|
- <Input
|
|
|
|
|
- value={form.basePath}
|
|
|
|
|
- placeholder="/"
|
|
|
|
|
- onChange={(e) => update('basePath', e.target.value)}
|
|
|
|
|
- />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
- </Col>
|
|
|
|
|
- <Col xs={24} md={12}>
|
|
|
|
|
- <Form.Item label={t('pages.nodes.enable')}>
|
|
|
|
|
- <Switch checked={form.enable} onChange={(v) => update('enable', v)} />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
- </Col>
|
|
|
|
|
- </Row>
|
|
|
|
|
|
|
+ <Row gutter={16}>
|
|
|
|
|
+ <Col xs={24} md={12}>
|
|
|
|
|
+ <Form.Item label={t('pages.nodes.basePath')} name="basePath">
|
|
|
|
|
+ <Input placeholder="/" />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
+ </Col>
|
|
|
|
|
+ <Col xs={24} md={12}>
|
|
|
|
|
+ <Form.Item
|
|
|
|
|
+ label={t('pages.nodes.enable')}
|
|
|
|
|
+ name="enable"
|
|
|
|
|
+ valuePropName="checked"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Switch />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
+ </Col>
|
|
|
|
|
+ </Row>
|
|
|
|
|
|
|
|
- <Form.Item label={t('pages.nodes.allowPrivateAddress')}>
|
|
|
|
|
- <Switch
|
|
|
|
|
- checked={form.allowPrivateAddress}
|
|
|
|
|
- onChange={(v) => update('allowPrivateAddress', v)}
|
|
|
|
|
- />
|
|
|
|
|
- <div className="hint">{t('pages.nodes.allowPrivateAddressHint')}</div>
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
|
|
+ <Form.Item
|
|
|
|
|
+ label={t('pages.nodes.allowPrivateAddress')}
|
|
|
|
|
+ name="allowPrivateAddress"
|
|
|
|
|
+ valuePropName="checked"
|
|
|
|
|
+ extra={t('pages.nodes.allowPrivateAddressHint')}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Switch />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
|
|
|
- <Form.Item label={t('pages.nodes.apiToken')} required>
|
|
|
|
|
- <Input.Password
|
|
|
|
|
- value={form.apiToken}
|
|
|
|
|
- placeholder={t('pages.nodes.apiTokenPlaceholder')}
|
|
|
|
|
- onChange={(e) => update('apiToken', e.target.value)}
|
|
|
|
|
- />
|
|
|
|
|
- <div className="hint">{t('pages.nodes.apiTokenHint')}</div>
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
|
|
+ <Form.Item
|
|
|
|
|
+ label={t('pages.nodes.apiToken')}
|
|
|
|
|
+ name="apiToken"
|
|
|
|
|
+ rules={[antdRule(NodeFormSchema.shape.apiToken, t)]}
|
|
|
|
|
+ extra={t('pages.nodes.apiTokenHint')}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Input.Password placeholder={t('pages.nodes.apiTokenPlaceholder')} />
|
|
|
|
|
+ </Form.Item>
|
|
|
|
|
|
|
|
- <div className="test-row">
|
|
|
|
|
- <Button type="default" loading={testing} onClick={onTest}>
|
|
|
|
|
- {t('pages.nodes.testConnection')}
|
|
|
|
|
- </Button>
|
|
|
|
|
- {testResult && (
|
|
|
|
|
- <div className="test-result">
|
|
|
|
|
- {testResult.status === 'online' ? (
|
|
|
|
|
- <Alert
|
|
|
|
|
- type="success"
|
|
|
|
|
- showIcon
|
|
|
|
|
- title={t('pages.nodes.connectionOk', { ms: testResult.latencyMs })}
|
|
|
|
|
- description={testResult.xrayVersion ? `Xray ${testResult.xrayVersion}` : undefined}
|
|
|
|
|
- />
|
|
|
|
|
- ) : (
|
|
|
|
|
- <Alert
|
|
|
|
|
- type="error"
|
|
|
|
|
- showIcon
|
|
|
|
|
- title={t('pages.nodes.connectionFailed')}
|
|
|
|
|
- description={testResult.error}
|
|
|
|
|
- />
|
|
|
|
|
- )}
|
|
|
|
|
- </div>
|
|
|
|
|
- )}
|
|
|
|
|
- </div>
|
|
|
|
|
- </Form>
|
|
|
|
|
|
|
+ <div className="test-row">
|
|
|
|
|
+ <Button type="default" loading={testing} onClick={onTest}>
|
|
|
|
|
+ {t('pages.nodes.testConnection')}
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ {testResult && (
|
|
|
|
|
+ <div className="test-result">
|
|
|
|
|
+ {testResult.status === 'online' ? (
|
|
|
|
|
+ <Alert
|
|
|
|
|
+ type="success"
|
|
|
|
|
+ showIcon
|
|
|
|
|
+ title={t('pages.nodes.connectionOk', { ms: testResult.latencyMs })}
|
|
|
|
|
+ description={testResult.xrayVersion ? `Xray ${testResult.xrayVersion}` : undefined}
|
|
|
|
|
+ />
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <Alert
|
|
|
|
|
+ type="error"
|
|
|
|
|
+ showIcon
|
|
|
|
|
+ title={t('pages.nodes.connectionFailed')}
|
|
|
|
|
+ description={testResult.error}
|
|
|
|
|
+ />
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </Form>
|
|
|
</Modal>
|
|
</Modal>
|
|
|
</>
|
|
</>
|
|
|
);
|
|
);
|