component.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { 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 { isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  28. import TooltipPlus from '@/app/components/base/tooltip-plus'
  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. useEffect(() => {
  54. if (!editor.hasNodes([WorkflowVariableBlockNode]))
  55. throw new Error('WorkflowVariableBlockPlugin: WorkflowVariableBlock not registered on editor')
  56. return mergeRegister(
  57. editor.registerCommand(
  58. UPDATE_WORKFLOW_NODES_MAP,
  59. (workflowNodesMap: WorkflowNodesMap) => {
  60. setLocalWorkflowNodesMap(workflowNodesMap)
  61. return true
  62. },
  63. COMMAND_PRIORITY_EDITOR,
  64. ),
  65. )
  66. }, [editor])
  67. const Item = (
  68. <div
  69. className={cn(
  70. 'mx-0.5 relative group/wrap flex items-center h-[18px] pl-0.5 pr-[3px] rounded-[5px] border select-none',
  71. isSelected ? ' border-[#84ADFF] bg-[#F5F8FF]' : ' border-black/5 bg-white',
  72. !node && !isEnv && '!border-[#F04438] !bg-[#FEF3F2]',
  73. )}
  74. ref={ref}
  75. >
  76. {!isEnv && (
  77. <div className='flex items-center'>
  78. {
  79. node?.type && (
  80. <div className='p-[1px]'>
  81. <VarBlockIcon
  82. className='!text-gray-500'
  83. type={node?.type}
  84. />
  85. </div>
  86. )
  87. }
  88. <div className='shrink-0 mx-0.5 max-w-[60px] text-xs font-medium text-gray-500 truncate' title={node?.title} style={{
  89. }}>{node?.title}</div>
  90. <Line3 className='mr-0.5 text-gray-300'></Line3>
  91. </div>
  92. )}
  93. <div className='flex items-center text-primary-600'>
  94. {!isEnv && <Variable02 className='shrink-0 w-3.5 h-3.5' />}
  95. {isEnv && <Env className='shrink-0 w-3.5 h-3.5 text-util-colors-violet-violet-600' />}
  96. <div className={cn('shrink-0 ml-0.5 text-xs font-medium truncate', isEnv && 'text-gray-900')} title={varName}>{varName}</div>
  97. {
  98. !node && !isEnv && (
  99. <RiErrorWarningFill className='ml-0.5 w-3 h-3 text-[#D92D20]' />
  100. )
  101. }
  102. </div>
  103. </div>
  104. )
  105. if (!node && !isEnv) {
  106. return (
  107. <TooltipPlus popupContent={t('workflow.errorMsg.invalidVariable')}>
  108. {Item}
  109. </TooltipPlus>
  110. )
  111. }
  112. return Item
  113. }
  114. export default memo(WorkflowVariableBlockComponent)