File size: 4,133 Bytes
c9d0005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Space } from 'antd';
import classNames from 'classnames';
import React, { memo } from 'react';
import { shallow } from 'zustand/shallow';
import SourceHandle from '../../components/CustomNode/sourceHandle';
import TextEllipsis from '../../components/TextEllipsis';
import { useStore } from '../../hooks/useStore';
import { uuid } from '../../utils';
import './index.less';

export default memo((props: any) => {
  const {
    data,
    position,
    isConnectable,
    selected,
    isHovered,
    handleAddNode,
    CustomNodeWidget,
    isSwitchBottom,
    nodeSetting,
    id,
  } = props;
  const { switchExtra } = nodeSetting;

  const { nodes, edges } = useStore(
    (state: any) => ({
      nodes: state.nodes,
      edges: state.edges,
    }),
    shallow
  );

  const renderTitle = (item, index) => {
    const defTitle = item?.title || `条件${index}`;
    const title = switchExtra?.titleKey
      ? item[switchExtra?.titleKey]
      : defTitle;
    return (
      <div className="item-header">
        <div className="item-title">{title}</div>
        <SourceHandle
          position={position}
          isConnectable={
            (edges || [])?.filter(flow => flow?.sourceHandle === item?._id)
              ?.length === 0
          }
          selected={selected}
          isHovered={isHovered}
          handleAddNode={data => {
            handleAddNode(data, item?._id);
          }}
          id={item?._id}
          className="item-handle"
          isConnected={
            (edges || [])?.filter(flow => flow?.sourceHandle === item?._id)
              ?.length > 0
          }
          nodeType={'switch'}
        />
      </div>
    );
  };

  const renderContent = (item, index) => {
    const value = switchExtra?.valueKey
      ? item[switchExtra?.valueKey]
      : item?.value;

    return (
      <div className="item-content">
        {CustomNodeWidget ? (
          <CustomNodeWidget data={item} index={index} />
        ) : (
          <div>
            {value && (
              <div className="item-content-in">
                <TextEllipsis text={value} rows={5} type="paragraph" />
              </div>
            )}
          </div>
        )}
      </div>
    );
  };

  return (
    <Space
      direction={isSwitchBottom ? 'horizontal' : 'vertical'}
      className={classNames('node-switch-widget', {
        'node-switch-widget-bottom': isSwitchBottom,
      })}
      size={5}
    >
      {(data?.list || [{ _id: `id_${uuid()}` }])?.map((item, index) => (
        <div
          className={classNames('node-switch-widget-item', {
            'node-switch-bottom-item': isSwitchBottom,
          })}
          key={index}
        >
          {isSwitchBottom ? (
            <>
              {renderContent(item, index)}
              {renderTitle(item, index)}
            </>
          ) : (
            <>
              {renderTitle(item, index)}
              {renderContent(item, index)}
            </>
          )}
        </div>
      ))}
      {!switchExtra?.hideElse && (
        <div
          className={classNames('node-switch-widget-item', {
            'node-switch-bottom-item': isSwitchBottom,
          })}
        >
          <div className="item-header">
            <div className="item-title">默认</div>
            <SourceHandle
              position={position}
              isConnectable={
                (edges || [])?.filter(
                  flow =>
                    flow?.sourceHandle === 'id_else' && flow?.source === id
                )?.length === 0
              }
              selected={selected}
              isHovered={isHovered}
              handleAddNode={data => {
                handleAddNode(data, 'id_else');
              }}
              className="item-handle"
              id={'id_else'}
              isConnected={
                (edges || [])?.filter(
                  flow =>
                    flow?.sourceHandle === 'id_else' && flow?.source === id
                )?.length > 0
              }
              nodeType={'Switch'}
            />
          </div>
        </div>
      )}
    </Space>
  );
});