node.tsx 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import cn from 'classnames'
  4. import type { EndNodeType } from './types'
  5. import type { NodeProps, Variable } from '@/app/components/workflow/types'
  6. import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  7. import {
  8. useIsChatMode,
  9. useWorkflow,
  10. useWorkflowVariables,
  11. } from '@/app/components/workflow/hooks'
  12. import { VarBlockIcon } from '@/app/components/workflow/block-icon'
  13. import { Line3 } from '@/app/components/base/icons/src/public/common'
  14. import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
  15. import { BubbleX, Env } from '@/app/components/base/icons/src/vender/line/others'
  16. import { BlockEnum } from '@/app/components/workflow/types'
  17. const Node: FC<NodeProps<EndNodeType>> = ({
  18. id,
  19. data,
  20. }) => {
  21. const { getBeforeNodesInSameBranch } = useWorkflow()
  22. const availableNodes = getBeforeNodesInSameBranch(id)
  23. const { getCurrentVariableType } = useWorkflowVariables()
  24. const isChatMode = useIsChatMode()
  25. const startNode = availableNodes.find((node: any) => {
  26. return node.data.type === BlockEnum.Start
  27. })
  28. const getNode = (id: string) => {
  29. return availableNodes.find(node => node.id === id) || startNode
  30. }
  31. const { outputs } = data
  32. const filteredOutputs = (outputs as Variable[]).filter(({ value_selector }) => value_selector.length > 0)
  33. if (!filteredOutputs.length)
  34. return null
  35. return (
  36. <div className='mb-1 px-3 py-1 space-y-0.5'>
  37. {filteredOutputs.map(({ value_selector }, index) => {
  38. const node = getNode(value_selector[0])
  39. const isSystem = isSystemVar(value_selector)
  40. const isEnv = isENV(value_selector)
  41. const isChatVar = isConversationVar(value_selector)
  42. const varName = isSystem ? `sys.${value_selector[value_selector.length - 1]}` : value_selector[value_selector.length - 1]
  43. const varType = getCurrentVariableType({
  44. valueSelector: value_selector,
  45. availableNodes,
  46. isChatMode,
  47. })
  48. return (
  49. <div key={index} className='flex items-center h-6 justify-between bg-gray-100 rounded-md px-1 space-x-1 text-xs font-normal text-gray-700'>
  50. <div className='flex items-center text-xs font-medium text-gray-500'>
  51. {!isEnv && !isChatVar && (
  52. <>
  53. <div className='p-[1px]'>
  54. <VarBlockIcon
  55. className='!text-gray-900'
  56. type={node?.data.type || BlockEnum.Start}
  57. />
  58. </div>
  59. <div className='max-w-[75px] truncate'>{node?.data.title}</div>
  60. <Line3 className='mr-0.5'></Line3>
  61. </>
  62. )}
  63. <div className='flex items-center text-primary-600'>
  64. {!isEnv && !isChatVar && <Variable02 className='shrink-0 w-3.5 h-3.5 text-primary-500' />}
  65. {isEnv && <Env className='shrink-0 w-3.5 h-3.5 text-util-colors-violet-violet-600' />}
  66. {isChatVar && <BubbleX className='w-3.5 h-3.5 text-util-colors-teal-teal-700' />}
  67. <div className={cn('max-w-[50px] ml-0.5 text-xs font-medium truncate', (isEnv || isChatVar) && '!max-w-[70px] text-gray-900')}>{varName}</div>
  68. </div>
  69. </div>
  70. <div className='text-xs font-normal text-gray-700'>
  71. <div className='max-w-[42px] ml-0.5 text-xs font-normal text-gray-500 capitalize truncate' title={varType}>{varType}</div>
  72. </div>
  73. </div>
  74. )
  75. })}
  76. </div>
  77. )
  78. }
  79. export default React.memo(Node)