component.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 lastVariable = isSystemVar(variables) ? variables.join('.') : variables[variablesLength - 1]
  41. const [localWorkflowNodesMap, setLocalWorkflowNodesMap] = useState<WorkflowNodesMap>(workflowNodesMap)
  42. const node = localWorkflowNodesMap![variables[0]]
  43. useEffect(() => {
  44. if (!editor.hasNodes([WorkflowVariableBlockNode]))
  45. throw new Error('WorkflowVariableBlockPlugin: WorkflowVariableBlock not registered on editor')
  46. return mergeRegister(
  47. editor.registerCommand(
  48. UPDATE_WORKFLOW_NODES_MAP,
  49. (workflowNodesMap: WorkflowNodesMap) => {
  50. setLocalWorkflowNodesMap(workflowNodesMap)
  51. return true
  52. },
  53. COMMAND_PRIORITY_EDITOR,
  54. ),
  55. )
  56. }, [editor])
  57. const Item = (
  58. <div
  59. className={cn(
  60. 'mx-0.5 relative group/wrap flex items-center h-[18px] pl-0.5 pr-[3px] rounded-[5px] border select-none',
  61. isSelected ? ' border-[#84ADFF] bg-[#F5F8FF]' : ' border-black/5 bg-white',
  62. !node && '!border-[#F04438] !bg-[#FEF3F2]',
  63. )}
  64. ref={ref}
  65. >
  66. <div className='flex items-center'>
  67. {
  68. node?.type && (
  69. <div className='p-[1px]'>
  70. <VarBlockIcon
  71. className='!text-gray-500'
  72. type={node?.type}
  73. />
  74. </div>
  75. )
  76. }
  77. <div className='shrink-0 mx-0.5 text-xs font-medium text-gray-500 truncate' title={node?.title} style={{
  78. }}>{node?.title}</div>
  79. <Line3 className='mr-0.5 text-gray-300'></Line3>
  80. </div>
  81. <div className='flex items-center text-primary-600'>
  82. <Variable02 className='w-3.5 h-3.5' />
  83. <div className='shrink-0 ml-0.5 text-xs font-medium truncate' title={lastVariable}>{lastVariable}</div>
  84. {
  85. !node && (
  86. <AlertCircle className='ml-0.5 w-3 h-3 text-[#D92D20]' />
  87. )
  88. }
  89. </div>
  90. </div>
  91. )
  92. if (!node) {
  93. return (
  94. <TooltipPlus popupContent={t('workflow.errorMsg.invalidVariable')}>
  95. {Item}
  96. </TooltipPlus>
  97. )
  98. }
  99. return Item
  100. }
  101. export default memo(WorkflowVariableBlockComponent)