component.tsx 3.7 KB

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