HostFinalMaskForm.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { useEffect, useRef, useState } from 'react';
  2. import { Form } from 'antd';
  3. import { FinalMaskForm } from '@/lib/xray/forms/transport';
  4. import type { FinalMaskStreamSettings } from '@/schemas/protocols/stream/finalmask';
  5. // Per-host Final Mask editor — same shape as the sub-JSON settings one
  6. // (SubJsonFinalMaskForm) but reused for a host: reads/writes the host's
  7. // finalMask JSON string. The masks are merged into this host's JSON stream.
  8. function hasValue(v: unknown): boolean {
  9. if (v == null) return false;
  10. if (Array.isArray(v)) return v.some(hasValue);
  11. if (typeof v === 'object') return Object.values(v as Record<string, unknown>).some(hasValue);
  12. if (typeof v === 'string') return v.length > 0;
  13. return true;
  14. }
  15. function parseFinalMask(raw: string): FinalMaskStreamSettings {
  16. try {
  17. if (raw) return JSON.parse(raw) as FinalMaskStreamSettings;
  18. } catch {
  19. return { tcp: [], udp: [] };
  20. }
  21. return { tcp: [], udp: [] };
  22. }
  23. export default function HostFinalMaskForm({ value = '', onChange }: { value?: string; onChange?: (next: string) => void }) {
  24. const [form] = Form.useForm();
  25. const [initial] = useState(() => parseFinalMask(value));
  26. const onChangeRef = useRef(onChange);
  27. onChangeRef.current = onChange;
  28. const finalmask = Form.useWatch('finalmask', form) as FinalMaskStreamSettings | undefined;
  29. useEffect(() => {
  30. if (finalmask === undefined) return;
  31. const next = hasValue(finalmask) ? JSON.stringify(finalmask) : '';
  32. if (next !== value) onChangeRef.current?.(next);
  33. }, [finalmask, value]);
  34. return (
  35. <Form
  36. form={form}
  37. component={false}
  38. colon={false}
  39. labelCol={{ sm: { span: 8 } }}
  40. wrapperCol={{ sm: { span: 14 } }}
  41. labelWrap
  42. initialValues={{ finalmask: initial }}
  43. >
  44. <FinalMaskForm name="finalmask" network="" protocol="" form={form} showAll />
  45. </Form>
  46. );
  47. }