node.tsx 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import type { NodeProps } from 'reactflow'
  4. import { useTranslation } from 'react-i18next'
  5. import { NodeTargetHandle } from '../_base/components/node-handle'
  6. import { BlockEnum } from '../../types'
  7. import type { VariableAssignerNodeType } from './types'
  8. import { VarBlockIcon } from '@/app/components/workflow/block-icon'
  9. import { Line3 } from '@/app/components/base/icons/src/public/common'
  10. import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
  11. import {
  12. useWorkflow,
  13. } from '@/app/components/workflow/hooks'
  14. const i18nPrefix = 'workflow.nodes.variableAssigner'
  15. const Node: FC<NodeProps<VariableAssignerNodeType>> = (props) => {
  16. const { t } = useTranslation()
  17. const { id, data } = props
  18. const { variables: originVariables, output_type } = data
  19. const { getTreeLeafNodes } = useWorkflow()
  20. const availableNodes = getTreeLeafNodes(id)
  21. const variables = originVariables.filter(item => item.length > 0)
  22. return (
  23. <div className='mb-1 px-3 py-1'>
  24. <div className='mb-0.5 leading-4 text-xs font-medium text-gray-500 uppercase'>{t(`${i18nPrefix}.title`)}</div>
  25. {
  26. variables.length === 0 && (
  27. <div className='relative flex items-center h-6 justify-between bg-gray-100 rounded-md px-1 space-x-1 text-xs font-normal text-gray-400 uppercase'>
  28. {t(`${i18nPrefix}.varNotSet`)}
  29. <NodeTargetHandle
  30. {...props}
  31. handleId='varNotSet'
  32. handleClassName='!top-1/2 !-translate-y-1/2 !-left-[21px]'
  33. />
  34. </div>
  35. )
  36. }
  37. {variables.length > 0 && (
  38. <>
  39. <div className='space-y-0.5'>
  40. {variables.map((item, index) => {
  41. const node = availableNodes.find(node => node.id === item[0])
  42. const varName = item[item.length - 1]
  43. return (
  44. <div key={index} className='relative flex items-center h-6 bg-gray-100 rounded-md px-1 text-xs font-normal text-gray-700' >
  45. <NodeTargetHandle
  46. {...props}
  47. handleId={item[0]}
  48. handleClassName='!top-1/2 !-translate-y-1/2 !-left-[21px]'
  49. />
  50. <div className='flex items-center'>
  51. <div className='p-[1px]'>
  52. <VarBlockIcon
  53. className='!text-gray-900'
  54. type={(node?.data.type as BlockEnum) || BlockEnum.Start}
  55. />
  56. </div>
  57. <div className='max-w-[85px] truncate mx-0.5 text-xs font-medium text-gray-700' title={node?.data.title}>{node?.data.title}</div>
  58. <Line3 className='mr-0.5'></Line3>
  59. </div>
  60. <div className='flex items-center text-primary-600'>
  61. <Variable02 className='w-3.5 h-3.5' />
  62. <div className='max-w-[75px] truncate ml-0.5 text-xs font-medium' title={varName}>{varName}</div>
  63. </div>
  64. {/* <div className='ml-0.5 text-xs font-normal text-gray-500'>{output_type}</div> */}
  65. </div>
  66. )
  67. },
  68. )}
  69. </div>
  70. <div className='mt-2 flex items-center h-6 justify-between bg-gray-100 rounded-md px-1 space-x-1 text-xs font-normal text-gray-700'>
  71. <div className='text-xs font-medium text-gray-500 uppercase'>
  72. {t(`${i18nPrefix}.outputType`)}
  73. </div>
  74. <div className='text-xs font-normal text-gray-700'>
  75. {t(`${i18nPrefix}.type.${output_type}`)}
  76. </div>
  77. </div>
  78. </>
  79. )
  80. }
  81. </div >
  82. )
  83. }
  84. export default React.memo(Node)