component.tsx 3.7 KB

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