SubJsonFinalMaskForm.tsx 1.7 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. interface SubJsonFinalMaskFormProps {
  6. value: string;
  7. onChange: (next: string) => void;
  8. }
  9. function hasValue(v: unknown): boolean {
  10. if (v == null) return false;
  11. if (Array.isArray(v)) return v.some(hasValue);
  12. if (typeof v === 'object') return Object.values(v as Record<string, unknown>).some(hasValue);
  13. if (typeof v === 'string') return v.length > 0;
  14. return true;
  15. }
  16. function parseFinalMask(raw: string): FinalMaskStreamSettings {
  17. try {
  18. if (raw) return JSON.parse(raw) as FinalMaskStreamSettings;
  19. } catch {
  20. return { tcp: [], udp: [] };
  21. }
  22. return { tcp: [], udp: [] };
  23. }
  24. export default function SubJsonFinalMaskForm({ value, onChange }: SubJsonFinalMaskFormProps) {
  25. const [form] = Form.useForm();
  26. const [initial] = useState(() => parseFinalMask(value));
  27. const onChangeRef = useRef(onChange);
  28. onChangeRef.current = onChange;
  29. const finalmask = Form.useWatch('finalmask', form) as FinalMaskStreamSettings | undefined;
  30. useEffect(() => {
  31. if (finalmask === undefined) return;
  32. const next = hasValue(finalmask) ? JSON.stringify(finalmask) : '';
  33. if (next !== value) onChangeRef.current(next);
  34. }, [finalmask, value]);
  35. return (
  36. <Form
  37. form={form}
  38. layout="horizontal"
  39. labelCol={{ flex: '160px' }}
  40. wrapperCol={{ flex: 'auto' }}
  41. colon={false}
  42. initialValues={{ finalmask: initial }}
  43. >
  44. <FinalMaskForm name="finalmask" network="" protocol="" form={form} showAll />
  45. </Form>
  46. );
  47. }