node.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useNodes } from 'reactflow'
  4. import { useTranslation } from 'react-i18next'
  5. import NodeVariableItem from '../variable-assigner/components/node-variable-item'
  6. import { type AssignerNodeType } from './types'
  7. import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  8. import { BlockEnum, type Node, type NodeProps } from '@/app/components/workflow/types'
  9. const i18nPrefix = 'workflow.nodes.assigner'
  10. const NodeComponent: FC<NodeProps<AssignerNodeType>> = ({
  11. data,
  12. }) => {
  13. const { t } = useTranslation()
  14. const nodes: Node[] = useNodes()
  15. if (data.version === '2') {
  16. const { items: operationItems } = data
  17. const validOperationItems = operationItems?.filter(item =>
  18. item.variable_selector && item.variable_selector.length > 0,
  19. ) || []
  20. if (validOperationItems.length === 0) {
  21. return (
  22. <div className='relative flex flex-col px-3 py-1 gap-0.5 items-start self-stretch'>
  23. <div className='flex flex-col items-start gap-1 self-stretch'>
  24. <div className='flex px-[5px] py-1 items-center gap-1 self-stretch rounded-md bg-workflow-block-parma-bg'>
  25. <div className='flex-1 text-text-tertiary system-xs-medium'>{t(`${i18nPrefix}.varNotSet`)}</div>
  26. </div>
  27. </div>
  28. </div>
  29. )
  30. }
  31. return (
  32. <div className='relative flex flex-col px-3 py-1 gap-0.5 items-start self-stretch'>
  33. {operationItems.map((value, index) => {
  34. const variable = value.variable_selector
  35. if (!variable || variable.length === 0)
  36. return null
  37. const isSystem = isSystemVar(variable)
  38. const isEnv = isENV(variable)
  39. const isChatVar = isConversationVar(variable)
  40. const node = isSystem ? nodes.find(node => node.data.type === BlockEnum.Start) : nodes.find(node => node.id === variable[0])
  41. const varName = isSystem ? `sys.${variable[variable.length - 1]}` : variable.slice(1).join('.')
  42. return (
  43. <NodeVariableItem
  44. key={index}
  45. node={node as Node}
  46. isEnv={isEnv}
  47. isChatVar={isChatVar}
  48. writeMode={value.operation}
  49. varName={varName}
  50. className='bg-workflow-block-parma-bg'
  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 isEnv = isENV(variable)
  63. const isChatVar = isConversationVar(variable)
  64. const node = isSystem ? nodes.find(node => node.data.type === BlockEnum.Start) : nodes.find(node => node.id === variable[0])
  65. const varName = isSystem ? `sys.${variable[variable.length - 1]}` : variable.slice(1).join('.')
  66. return (
  67. <div className='relative flex flex-col px-3 py-1 gap-0.5 items-start self-stretch'>
  68. <NodeVariableItem
  69. node={node as Node}
  70. isEnv={isEnv}
  71. isChatVar={isChatVar}
  72. varName={varName}
  73. writeMode={writeMode}
  74. className='bg-workflow-block-parma-bg'
  75. />
  76. </div>
  77. )
  78. }
  79. export default React.memo(NodeComponent)