component.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. import { isExceptionVariable } from '@/app/components/workflow/utils'
  30. type WorkflowVariableBlockComponentProps = {
  31. nodeKey: string
  32. variables: string[]
  33. workflowNodesMap: WorkflowNodesMap
  34. }
  35. const WorkflowVariableBlockComponent = ({
  36. nodeKey,
  37. variables,
  38. workflowNodesMap = {},
  39. }: WorkflowVariableBlockComponentProps) => {
  40. const { t } = useTranslation()
  41. const [editor] = useLexicalComposerContext()
  42. const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_WORKFLOW_VARIABLE_BLOCK_COMMAND)
  43. const variablesLength = variables.length
  44. const varName = (
  45. () => {
  46. const isSystem = isSystemVar(variables)
  47. const varName = variablesLength >= 3 ? (variables).slice(-2).join('.') : variables[variablesLength - 1]
  48. return `${isSystem ? 'sys.' : ''}${varName}`
  49. }
  50. )()
  51. const [localWorkflowNodesMap, setLocalWorkflowNodesMap] = useState<WorkflowNodesMap>(workflowNodesMap)
  52. const node = localWorkflowNodesMap![variables[0]]
  53. const isEnv = isENV(variables)
  54. const isChatVar = isConversationVar(variables)
  55. const isException = isExceptionVariable(varName, node?.type)
  56. useEffect(() => {
  57. if (!editor.hasNodes([WorkflowVariableBlockNode]))
  58. throw new Error('WorkflowVariableBlockPlugin: WorkflowVariableBlock not registered on editor')
  59. return mergeRegister(
  60. editor.registerCommand(
  61. UPDATE_WORKFLOW_NODES_MAP,
  62. (workflowNodesMap: WorkflowNodesMap) => {
  63. setLocalWorkflowNodesMap(workflowNodesMap)
  64. return true
  65. },
  66. COMMAND_PRIORITY_EDITOR,
  67. ),
  68. )
  69. }, [editor])
  70. const Item = (
  71. <div
  72. className={cn(
  73. 'mx-0.5 relative group/wrap flex items-center h-[18px] pl-0.5 pr-[3px] rounded-[5px] border select-none',
  74. isSelected ? ' border-state-accent-solid bg-state-accent-hover' : ' border-components-panel-border-subtle bg-components-badge-white-to-dark',
  75. !node && !isEnv && !isChatVar && '!border-state-destructive-solid !bg-state-destructive-hover',
  76. )}
  77. ref={ref}
  78. >
  79. {!isEnv && !isChatVar && (
  80. <div className='flex items-center'>
  81. {
  82. node?.type && (
  83. <div className='p-[1px]'>
  84. <VarBlockIcon
  85. className='!text-text-secondary'
  86. type={node?.type}
  87. />
  88. </div>
  89. )
  90. }
  91. <div className='shrink-0 mx-0.5 max-w-[60px] text-xs font-medium text-text-secondary truncate' title={node?.title} style={{
  92. }}>{node?.title}</div>
  93. <Line3 className='mr-0.5 text-divider-deep'></Line3>
  94. </div>
  95. )}
  96. <div className='flex items-center text-text-accent'>
  97. {!isEnv && !isChatVar && <Variable02 className={cn('shrink-0 w-3.5 h-3.5', isException && 'text-text-warning')} />}
  98. {isEnv && <Env className='shrink-0 w-3.5 h-3.5 text-util-colors-violet-violet-600' />}
  99. {isChatVar && <BubbleX className='w-3.5 h-3.5 text-util-colors-teal-teal-700' />}
  100. <div className={cn(
  101. 'shrink-0 ml-0.5 text-xs font-medium truncate',
  102. isEnv && 'text-util-colors-violet-violet-600',
  103. isChatVar && 'text-util-colors-teal-teal-700',
  104. isException && 'text-text-warning',
  105. )} title={varName}>{varName}</div>
  106. {
  107. !node && !isEnv && !isChatVar && (
  108. <RiErrorWarningFill className='ml-0.5 w-3 h-3 text-text-destructive' />
  109. )
  110. }
  111. </div>
  112. </div>
  113. )
  114. if (!node && !isEnv && !isChatVar) {
  115. return (
  116. <Tooltip popupContent={t('workflow.errorMsg.invalidVariable')}>
  117. {Item}
  118. </Tooltip>
  119. )
  120. }
  121. return Item
  122. }
  123. export default memo(WorkflowVariableBlockComponent)