component.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. memo,
  3. useEffect,
  4. useState,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import {
  8. COMMAND_PRIORITY_EDITOR,
  9. } from 'lexical'
  10. import { mergeRegister } from '@lexical/utils'
  11. import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
  12. import {
  13. RiErrorWarningFill,
  14. } from '@remixicon/react'
  15. import { useSelectOrDelete } from '../../hooks'
  16. import type { WorkflowNodesMap } from './node'
  17. import { WorkflowVariableBlockNode } from './node'
  18. import {
  19. DELETE_WORKFLOW_VARIABLE_BLOCK_COMMAND,
  20. UPDATE_WORKFLOW_NODES_MAP,
  21. } from './index'
  22. import cn from '@/utils/classnames'
  23. import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
  24. import { BubbleX, Env } from '@/app/components/base/icons/src/vender/line/others'
  25. import { VarBlockIcon } from '@/app/components/workflow/block-icon'
  26. import { Line3 } from '@/app/components/base/icons/src/public/common'
  27. import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  28. import Tooltip from '@/app/components/base/tooltip'
  29. type WorkflowVariableBlockComponentProps = {
  30. nodeKey: string
  31. variables: string[]
  32. workflowNodesMap: WorkflowNodesMap
  33. }
  34. const WorkflowVariableBlockComponent = ({
  35. nodeKey,
  36. variables,
  37. workflowNodesMap = {},
  38. }: WorkflowVariableBlockComponentProps) => {
  39. const { t } = useTranslation()
  40. const [editor] = useLexicalComposerContext()
  41. const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_WORKFLOW_VARIABLE_BLOCK_COMMAND)
  42. const variablesLength = variables.length
  43. const varName = (
  44. () => {
  45. const isSystem = isSystemVar(variables)
  46. const varName = variablesLength >= 3 ? (variables).slice(-2).join('.') : variables[variablesLength - 1]
  47. return `${isSystem ? 'sys.' : ''}${varName}`
  48. }
  49. )()
  50. const [localWorkflowNodesMap, setLocalWorkflowNodesMap] = useState<WorkflowNodesMap>(workflowNodesMap)
  51. const node = localWorkflowNodesMap![variables[0]]
  52. const isEnv = isENV(variables)
  53. const isChatVar = isConversationVar(variables)
  54. useEffect(() => {
  55. if (!editor.hasNodes([WorkflowVariableBlockNode]))
  56. throw new Error('WorkflowVariableBlockPlugin: WorkflowVariableBlock not registered on editor')
  57. return mergeRegister(
  58. editor.registerCommand(
  59. UPDATE_WORKFLOW_NODES_MAP,
  60. (workflowNodesMap: WorkflowNodesMap) => {
  61. setLocalWorkflowNodesMap(workflowNodesMap)
  62. return true
  63. },
  64. COMMAND_PRIORITY_EDITOR,
  65. ),
  66. )
  67. }, [editor])
  68. const Item = (
  69. <div
  70. className={cn(
  71. 'mx-0.5 relative group/wrap flex items-center h-[18px] pl-0.5 pr-[3px] rounded-[5px] border select-none',
  72. isSelected ? ' border-[#84ADFF] bg-[#F5F8FF]' : ' border-black/5 bg-white',
  73. !node && !isEnv && !isChatVar && '!border-[#F04438] !bg-[#FEF3F2]',
  74. )}
  75. ref={ref}
  76. >
  77. {!isEnv && !isChatVar && (
  78. <div className='flex items-center'>
  79. {
  80. node?.type && (
  81. <div className='p-[1px]'>
  82. <VarBlockIcon
  83. className='!text-gray-500'
  84. type={node?.type}
  85. />
  86. </div>
  87. )
  88. }
  89. <div className='shrink-0 mx-0.5 max-w-[60px] text-xs font-medium text-gray-500 truncate' title={node?.title} style={{
  90. }}>{node?.title}</div>
  91. <Line3 className='mr-0.5 text-gray-300'></Line3>
  92. </div>
  93. )}
  94. <div className='flex items-center text-primary-600'>
  95. {!isEnv && !isChatVar && <Variable02 className='shrink-0 w-3.5 h-3.5' />}
  96. {isEnv && <Env className='shrink-0 w-3.5 h-3.5 text-util-colors-violet-violet-600' />}
  97. {isChatVar && <BubbleX className='w-3.5 h-3.5 text-util-colors-teal-teal-700' />}
  98. <div className={cn('shrink-0 ml-0.5 text-xs font-medium truncate', (isEnv || isChatVar) && 'text-gray-900')} title={varName}>{varName}</div>
  99. {
  100. !node && !isEnv && !isChatVar && (
  101. <RiErrorWarningFill className='ml-0.5 w-3 h-3 text-[#D92D20]' />
  102. )
  103. }
  104. </div>
  105. </div>
  106. )
  107. if (!node && !isEnv && !isChatVar) {
  108. return (
  109. <Tooltip popupContent={t('workflow.errorMsg.invalidVariable')}>
  110. {Item}
  111. </Tooltip>
  112. )
  113. }
  114. return Item
  115. }
  116. export default memo(WorkflowVariableBlockComponent)