You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useNodes } from 'reactflow'
  4. import { useTranslation } from 'react-i18next'
  5. import type { AssignerNodeType } from './types'
  6. import { isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  7. import { BlockEnum, type Node, type NodeProps } from '@/app/components/workflow/types'
  8. import {
  9. VariableLabelInNode,
  10. } from '@/app/components/workflow/nodes/_base/components/variable/variable-label'
  11. import Badge from '@/app/components/base/badge'
  12. const i18nPrefix = 'workflow.nodes.assigner'
  13. const NodeComponent: FC<NodeProps<AssignerNodeType>> = ({
  14. data,
  15. }) => {
  16. const { t } = useTranslation()
  17. const nodes: Node[] = useNodes()
  18. if (data.version === '2') {
  19. const { items: operationItems } = data
  20. const validOperationItems = operationItems?.filter(item =>
  21. item.variable_selector && item.variable_selector.length > 0,
  22. ) || []
  23. if (validOperationItems.length === 0) {
  24. return (
  25. <div className='relative flex flex-col items-start gap-0.5 self-stretch px-3 py-1'>
  26. <div className='flex flex-col items-start gap-1 self-stretch'>
  27. <div className='flex items-center gap-1 self-stretch rounded-md bg-workflow-block-parma-bg px-[5px] py-1'>
  28. <div className='system-xs-medium flex-1 text-text-tertiary'>{t(`${i18nPrefix}.varNotSet`)}</div>
  29. </div>
  30. </div>
  31. </div>
  32. )
  33. }
  34. return (
  35. <div className='relative flex flex-col items-start gap-0.5 self-stretch px-3 py-1'>
  36. {operationItems.map((value, index) => {
  37. const variable = value.variable_selector
  38. if (!variable || variable.length === 0)
  39. return null
  40. const isSystem = isSystemVar(variable)
  41. const node = isSystem ? nodes.find(node => node.data.type === BlockEnum.Start) : nodes.find(node => node.id === variable[0])
  42. return (
  43. <VariableLabelInNode
  44. key={index}
  45. variables={variable}
  46. nodeType={node?.data.type}
  47. nodeTitle={node?.data.title}
  48. rightSlot={
  49. value.operation && <Badge className='!ml-auto shrink-0' text={t(`${i18nPrefix}.operations.${value.operation}`)} />
  50. }
  51. />
  52. )
  53. })}
  54. </div>
  55. )
  56. }
  57. // Legacy version
  58. const { assigned_variable_selector: variable, write_mode: writeMode } = data as any
  59. if (!variable || variable.length === 0)
  60. return null
  61. const isSystem = isSystemVar(variable)
  62. const node = isSystem ? nodes.find(node => node.data.type === BlockEnum.Start) : nodes.find(node => node.id === variable[0])
  63. return (
  64. <div className='relative flex flex-col items-start gap-0.5 self-stretch px-3 py-1'>
  65. <VariableLabelInNode
  66. variables={variable}
  67. nodeType={node?.data.type}
  68. nodeTitle={node?.data.title}
  69. rightSlot={
  70. writeMode && <Badge className='!ml-auto shrink-0' text={t(`${i18nPrefix}.operations.${writeMode}`)} />
  71. }
  72. />
  73. </div>
  74. )
  75. }
  76. export default React.memo(NodeComponent)