File size: 1,514 Bytes
10c9dc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { useEffect, useState } from "react";
import NodeParameter from "./NodeParameter";

interface SelectorType {
  name: string;
  default: string;
  type: {
    enum: string[];
  };
}

interface ParameterType {
  name: string;
  default: string;
  type: {
    type: string;
  };
}

interface GroupsType {
  [key: string]: ParameterType[];
}

interface NodeGroupParameterProps {
  meta: { selector: SelectorType; groups: GroupsType };
  value: any;
  setParam: (name: string, value: any, options?: { delay: number }) => void;
  deleteParam: (name: string, options?: { delay: number }) => void;
}

export default function NodeGroupParameter({
  meta,
  value,
  setParam,
  deleteParam,
}: NodeGroupParameterProps) {
  const selector = meta.selector;
  const groups = meta.groups;
  const [selectedValue, setSelectedValue] = useState<string>(
    value || selector.default,
  );

  const handleSelectorChange = (value: any, opts?: { delay: number }) => {
    setSelectedValue(value);
    setParam(selector.name, value, opts);
  };

  useEffect(() => {
    // Clean possible previous parameters first
    Object.values(groups).flatMap((group) =>
      group.map((entry) => deleteParam(entry.name)),
    );
    for (const param of groups[selectedValue]) {
      setParam(param.name, param.default);
    }
  }, [selectedValue]);

  return (
    <NodeParameter
      name={selector.name}
      key={selector.name}
      value={selectedValue}
      meta={selector}
      onChange={handleSelectorChange}
    />
  );
}