node.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import type { FC } from 'react'
  2. import React, { useCallback } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import type { NodeProps } from 'reactflow'
  5. import { NodeSourceHandle } from '../_base/components/node-handle'
  6. import { isEmptyRelatedOperator } from './utils'
  7. import type { Condition, IfElseNodeType } from './types'
  8. import ConditionValue from './components/condition-value'
  9. import ConditionFilesListValue from './components/condition-files-list-value'
  10. const i18nPrefix = 'workflow.nodes.ifElse'
  11. const IfElseNode: FC<NodeProps<IfElseNodeType>> = (props) => {
  12. const { data } = props
  13. const { t } = useTranslation()
  14. const { cases } = data
  15. const casesLength = cases.length
  16. const checkIsConditionSet = useCallback((condition: Condition) => {
  17. if (!condition.variable_selector || condition.variable_selector.length === 0)
  18. return false
  19. if (condition.sub_variable_condition) {
  20. const isSet = condition.sub_variable_condition.conditions.every((c) => {
  21. if (!c.comparison_operator)
  22. return false
  23. if (isEmptyRelatedOperator(c.comparison_operator!))
  24. return true
  25. return !!c.value
  26. })
  27. return isSet
  28. }
  29. else {
  30. if (isEmptyRelatedOperator(condition.comparison_operator!))
  31. return true
  32. return !!condition.value
  33. }
  34. }, [])
  35. const conditionNotSet = (<div className='flex items-center h-6 px-1 space-x-1 text-xs font-normal text-text-secondary bg-workflow-block-parma-bg rounded-md'>
  36. {t(`${i18nPrefix}.conditionNotSetup`)}
  37. </div>)
  38. return (
  39. <div className='px-3'>
  40. {
  41. cases.map((caseItem, index) => (
  42. <div key={caseItem.case_id}>
  43. <div className='relative flex items-center h-6 px-1'>
  44. <div className='flex items-center justify-between w-full'>
  45. <div className='text-[10px] font-semibold text-text-tertiary'>
  46. {casesLength > 1 && `CASE ${index + 1}`}
  47. </div>
  48. <div className='text-[12px] font-semibold text-text-secondary'>{index === 0 ? 'IF' : 'ELIF'}</div>
  49. </div>
  50. <NodeSourceHandle
  51. {...props}
  52. handleId={caseItem.case_id}
  53. handleClassName='!top-1/2 !-right-[21px] !-translate-y-1/2'
  54. />
  55. </div>
  56. <div className='space-y-0.5'>
  57. {caseItem.conditions.map((condition, i) => (
  58. <div key={condition.id} className='relative'>
  59. {
  60. checkIsConditionSet(condition)
  61. ? (
  62. (!isEmptyRelatedOperator(condition.comparison_operator!) && condition.sub_variable_condition)
  63. ? (
  64. <ConditionFilesListValue condition={condition} />
  65. )
  66. : (
  67. <ConditionValue
  68. variableSelector={condition.variable_selector!}
  69. operator={condition.comparison_operator!}
  70. value={condition.value}
  71. />
  72. )
  73. )
  74. : conditionNotSet}
  75. {i !== caseItem.conditions.length - 1 && (
  76. <div className='absolute z-10 right-1 bottom-[-10px] leading-4 text-[10px] font-medium text-text-accent uppercase'>{t(`${i18nPrefix}.${caseItem.logical_operator}`)}</div>
  77. )}
  78. </div>
  79. ))}
  80. </div>
  81. </div>
  82. ))
  83. }
  84. <div className='relative flex items-center h-6 px-1'>
  85. <div className='w-full text-xs font-semibold text-right text-text-secondary'>ELSE</div>
  86. <NodeSourceHandle
  87. {...props}
  88. handleId='false'
  89. handleClassName='!top-1/2 !-right-[21px] !-translate-y-1/2'
  90. />
  91. </div>
  92. </div>
  93. )
  94. }
  95. export default React.memo(IfElseNode)