node.tsx 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import type { EndNodeType } from './types'
  4. import type { NodeProps, ValueSelector, Variable } from '@/app/components/workflow/types'
  5. import { isSystemVar, toNodeOutputVars } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  6. import {
  7. useIsChatMode,
  8. useWorkflow,
  9. } from '@/app/components/workflow/hooks'
  10. import { VarBlockIcon } from '@/app/components/workflow/block-icon'
  11. import { Line3 } from '@/app/components/base/icons/src/public/common'
  12. import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
  13. import { BlockEnum, VarType } from '@/app/components/workflow/types'
  14. const Node: FC<NodeProps<EndNodeType>> = ({
  15. id,
  16. data,
  17. }) => {
  18. const { getBeforeNodesInSameBranch } = useWorkflow()
  19. const availableNodes = getBeforeNodesInSameBranch(id)
  20. const isChatMode = useIsChatMode()
  21. const outputVars = toNodeOutputVars(availableNodes, isChatMode)
  22. const startNode = availableNodes.find((node: any) => {
  23. return node.data.type === BlockEnum.Start
  24. })
  25. const getNode = (id: string) => {
  26. return availableNodes.find(node => node.id === id) || startNode
  27. }
  28. const getVarType = (nodeId: string, value: ValueSelector) => {
  29. const targetVar = outputVars.find(v => v.nodeId === nodeId)
  30. if (!targetVar)
  31. return 'undefined'
  32. let type: VarType = VarType.string
  33. let curr: any = targetVar.vars
  34. const isSystem = isSystemVar(value);
  35. (value).slice(1).forEach((key, i) => {
  36. const isLast = i === value.length - 2
  37. curr = curr.find((v: any) => v.variable === isSystem ? `sys.${key}` : key)
  38. if (isLast) {
  39. type = curr.type
  40. }
  41. else {
  42. if (curr.type === VarType.object)
  43. curr = curr.children
  44. }
  45. })
  46. return type
  47. }
  48. const { outputs } = data
  49. const filteredOutputs = (outputs as Variable[]).filter(({ value_selector }) => value_selector.length > 0)
  50. if (!filteredOutputs.length)
  51. return null
  52. return (
  53. <div className='mb-1 px-3 py-1 space-y-0.5'>
  54. {filteredOutputs.map(({ value_selector }, index) => {
  55. const node = getNode(value_selector[0])
  56. const isSystem = isSystemVar(value_selector)
  57. const varName = isSystem ? `sys.${value_selector[value_selector.length - 1]}` : value_selector[value_selector.length - 1]
  58. return (
  59. <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'>
  60. <div className='flex items-center text-xs font-medium text-gray-500'>
  61. <div className='p-[1px]'>
  62. <VarBlockIcon
  63. className='!text-gray-900'
  64. type={node?.data.type || BlockEnum.Start}
  65. />
  66. </div>
  67. <div className='max-w-[75px] truncate'>{node?.data.title}</div>
  68. <Line3 className='mr-0.5'></Line3>
  69. <div className='flex items-center text-primary-600'>
  70. <Variable02 className='w-3.5 h-3.5' />
  71. <div className='max-w-[50px] ml-0.5 text-xs font-medium truncate'>{varName}</div>
  72. </div>
  73. </div>
  74. <div className='text-xs font-normal text-gray-700'>
  75. <div className='max-w-[42px] ml-0.5 text-xs font-normal text-gray-500 capitalize truncate' title={getVarType(node?.id || '', value_selector)}>{getVarType(node?.id || '', value_selector)}</div>
  76. </div>
  77. </div>
  78. )
  79. })}
  80. </div>
  81. )
  82. }
  83. export default React.memo(Node)