node.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import type { FC } from 'react'
  2. import {
  3. memo,
  4. useMemo,
  5. useRef,
  6. } from 'react'
  7. import type { NodeProps } from 'reactflow'
  8. import { useTranslation } from 'react-i18next'
  9. import NodeGroupItem from './components/node-group-item'
  10. import type { VariableAssignerNodeType } from './types'
  11. const i18nPrefix = 'workflow.nodes.variableAssigner'
  12. const Node: FC<NodeProps<VariableAssignerNodeType>> = (props) => {
  13. const { t } = useTranslation()
  14. const ref = useRef<HTMLDivElement>(null)
  15. const { id, data } = props
  16. const { advanced_settings } = data
  17. const groups = useMemo(() => {
  18. if (!advanced_settings?.group_enabled) {
  19. return [{
  20. groupEnabled: false,
  21. targetHandleId: 'target',
  22. title: t(`${i18nPrefix}.title`),
  23. type: data.output_type,
  24. variables: data.variables,
  25. variableAssignerNodeId: id,
  26. variableAssignerNodeData: data,
  27. }]
  28. }
  29. return advanced_settings.groups.map((group) => {
  30. return {
  31. groupEnabled: true,
  32. targetHandleId: group.groupId,
  33. title: group.group_name,
  34. type: group.output_type,
  35. variables: group.variables,
  36. variableAssignerNodeId: id,
  37. variableAssignerNodeData: data,
  38. }
  39. })
  40. }, [t, advanced_settings, data, id])
  41. return (
  42. <div className='relative mb-1 px-1 space-y-0.5' ref={ref}>
  43. {
  44. groups.map((item) => {
  45. return (
  46. <NodeGroupItem
  47. key={item.title}
  48. item={item}
  49. />
  50. )
  51. })
  52. }
  53. </div >
  54. )
  55. }
  56. export default memo(Node)