balibabu commited on
Commit
63eb0ad
·
1 Parent(s): 64508f3

feat: Generate operator names in an auto-incremental manner #1739 (#2844)

Browse files

### What problem does this PR solve?

feat: Generate operator names in an auto-incremental manner #1739

### Type of change

- [ ] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):

web/src/pages/flow/constant.tsx CHANGED
@@ -75,6 +75,10 @@ export enum Operator {
75
  Note = 'Note',
76
  }
77
 
 
 
 
 
78
  export const operatorIconMap = {
79
  [Operator.Retrieval]: RocketOutlined,
80
  [Operator.Generate]: MergeCellsOutlined,
 
75
  Note = 'Note',
76
  }
77
 
78
+ export const CommonOperatorList = Object.values(Operator).filter(
79
+ (x) => x !== Operator.Note,
80
+ );
81
+
82
  export const operatorIconMap = {
83
  [Operator.Retrieval]: RocketOutlined,
84
  [Operator.Generate]: MergeCellsOutlined,
web/src/pages/flow/hooks.ts CHANGED
@@ -23,7 +23,9 @@ import api from '@/utils/api';
23
  import { useDebounceEffect } from 'ahooks';
24
  import { FormInstance, message } from 'antd';
25
  import { humanId } from 'human-id';
 
26
  import trim from 'lodash/trim';
 
27
  import { useParams } from 'umi';
28
  import { v4 as uuid } from 'uuid';
29
  import {
@@ -152,17 +154,68 @@ export const useHandleDrag = () => {
152
  return { handleDragStart };
153
  };
154
 
 
 
 
 
 
 
 
 
155
  export const useHandleDrop = () => {
156
  const addNode = useGraphStore((state) => state.addNode);
 
157
  const [reactFlowInstance, setReactFlowInstance] =
158
  useState<ReactFlowInstance<any, any>>();
159
  const initializeOperatorParams = useInitializeOperatorParams();
 
160
 
161
  const onDragOver = useCallback((event: React.DragEvent<HTMLDivElement>) => {
162
  event.preventDefault();
163
  event.dataTransfer.dropEffect = 'move';
164
  }, []);
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  const onDrop = useCallback(
167
  (event: React.DragEvent<HTMLDivElement>) => {
168
  event.preventDefault();
@@ -190,7 +243,7 @@ export const useHandleDrop = () => {
190
  },
191
  data: {
192
  label: `${type}`,
193
- name: humanId(),
194
  form: initializeOperatorParams(type as Operator),
195
  },
196
  sourcePosition: Position.Right,
@@ -199,7 +252,7 @@ export const useHandleDrop = () => {
199
 
200
  addNode(newNode);
201
  },
202
- [reactFlowInstance, addNode, initializeOperatorParams],
203
  );
204
 
205
  return { onDrop, onDragOver, setReactFlowInstance };
 
23
  import { useDebounceEffect } from 'ahooks';
24
  import { FormInstance, message } from 'antd';
25
  import { humanId } from 'human-id';
26
+ import { lowerFirst } from 'lodash';
27
  import trim from 'lodash/trim';
28
+ import { useTranslation } from 'react-i18next';
29
  import { useParams } from 'umi';
30
  import { v4 as uuid } from 'uuid';
31
  import {
 
154
  return { handleDragStart };
155
  };
156
 
157
+ const splitName = (name: string) => {
158
+ const names = name.split('_');
159
+ const type = names.at(0);
160
+ const index = Number(names.at(-1));
161
+
162
+ return { type, index };
163
+ };
164
+
165
  export const useHandleDrop = () => {
166
  const addNode = useGraphStore((state) => state.addNode);
167
+ const nodes = useGraphStore((state) => state.nodes);
168
  const [reactFlowInstance, setReactFlowInstance] =
169
  useState<ReactFlowInstance<any, any>>();
170
  const initializeOperatorParams = useInitializeOperatorParams();
171
+ const { t } = useTranslation();
172
 
173
  const onDragOver = useCallback((event: React.DragEvent<HTMLDivElement>) => {
174
  event.preventDefault();
175
  event.dataTransfer.dropEffect = 'move';
176
  }, []);
177
 
178
+ const generateNodeName = useCallback(
179
+ (type: string) => {
180
+ const name = t(`flow.${lowerFirst(type)}`);
181
+ const templateNameList = nodes
182
+ .filter((x) => {
183
+ const temporaryName = x.data.name;
184
+
185
+ const { type, index } = splitName(temporaryName);
186
+
187
+ return (
188
+ temporaryName.match(/_/g)?.length === 1 &&
189
+ type === name &&
190
+ !isNaN(index)
191
+ );
192
+ })
193
+ .map((x) => {
194
+ const temporaryName = x.data.name;
195
+ const { index } = splitName(temporaryName);
196
+
197
+ return {
198
+ idx: index,
199
+ name: temporaryName,
200
+ };
201
+ })
202
+ .sort((a, b) => a.idx - b.idx);
203
+
204
+ let index: number = 0;
205
+ for (let i = 0; i < templateNameList.length; i++) {
206
+ const idx = templateNameList[i]?.idx;
207
+ const nextIdx = templateNameList[i + 1]?.idx;
208
+ if (idx + 1 !== nextIdx) {
209
+ index = idx + 1;
210
+ break;
211
+ }
212
+ }
213
+
214
+ return `${name}_${index}`;
215
+ },
216
+ [t, nodes],
217
+ );
218
+
219
  const onDrop = useCallback(
220
  (event: React.DragEvent<HTMLDivElement>) => {
221
  event.preventDefault();
 
243
  },
244
  data: {
245
  label: `${type}`,
246
+ name: generateNodeName(type),
247
  form: initializeOperatorParams(type as Operator),
248
  },
249
  sourcePosition: Position.Right,
 
252
 
253
  addNode(newNode);
254
  },
255
+ [reactFlowInstance, addNode, initializeOperatorParams, generateNodeName],
256
  );
257
 
258
  return { onDrop, onDragOver, setReactFlowInstance };