condition-value.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {
  2. memo,
  3. useMemo,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import { useNodes } from 'reactflow'
  7. import { ComparisonOperator } from '../types'
  8. import {
  9. comparisonOperatorNotRequireValue,
  10. isComparisonOperatorNeedTranslate,
  11. } from '../utils'
  12. import { FILE_TYPE_OPTIONS, TRANSFER_METHOD } from '../../constants'
  13. import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
  14. import { BubbleX, Env } from '@/app/components/base/icons/src/vender/line/others'
  15. import cn from '@/utils/classnames'
  16. import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  17. import { isExceptionVariable } from '@/app/components/workflow/utils'
  18. import type {
  19. CommonNodeType,
  20. Node,
  21. } from '@/app/components/workflow/types'
  22. type ConditionValueProps = {
  23. variableSelector: string[]
  24. labelName?: string
  25. operator: ComparisonOperator
  26. value: string | string[]
  27. }
  28. const ConditionValue = ({
  29. variableSelector,
  30. labelName,
  31. operator,
  32. value,
  33. }: ConditionValueProps) => {
  34. const { t } = useTranslation()
  35. const nodes = useNodes()
  36. const variableName = labelName || (isSystemVar(variableSelector) ? variableSelector.slice(0).join('.') : variableSelector.slice(1).join('.'))
  37. const operatorName = isComparisonOperatorNeedTranslate(operator) ? t(`workflow.nodes.ifElse.comparisonOperator.${operator}`) : operator
  38. const notHasValue = comparisonOperatorNotRequireValue(operator)
  39. const isEnvVar = isENV(variableSelector)
  40. const isChatVar = isConversationVar(variableSelector)
  41. const node: Node<CommonNodeType> | undefined = nodes.find(n => n.id === variableSelector[0]) as Node<CommonNodeType>
  42. const isException = isExceptionVariable(variableName, node?.data.type)
  43. const formatValue = useMemo(() => {
  44. if (notHasValue)
  45. return ''
  46. if (Array.isArray(value)) // transfer method
  47. return value[0]
  48. return value.replace(/{{#([^#]*)#}}/g, (a, b) => {
  49. const arr: string[] = b.split('.')
  50. if (isSystemVar(arr))
  51. return `{{${b}}}`
  52. return `{{${arr.slice(1).join('.')}}}`
  53. })
  54. }, [notHasValue, value])
  55. const isSelect = operator === ComparisonOperator.in || operator === ComparisonOperator.notIn
  56. const selectName = useMemo(() => {
  57. if (isSelect) {
  58. const name = [...FILE_TYPE_OPTIONS, ...TRANSFER_METHOD].filter(item => item.value === (Array.isArray(value) ? value[0] : value))[0]
  59. return name
  60. ? t(`workflow.nodes.ifElse.optionName.${name.i18nKey}`).replace(/{{#([^#]*)#}}/g, (a, b) => {
  61. const arr: string[] = b.split('.')
  62. if (isSystemVar(arr))
  63. return `{{${b}}}`
  64. return `{{${arr.slice(1).join('.')}}}`
  65. })
  66. : ''
  67. }
  68. return ''
  69. }, [isSelect, t, value])
  70. return (
  71. <div className='flex h-6 items-center rounded-md bg-workflow-block-parma-bg px-1'>
  72. {!isEnvVar && !isChatVar && <Variable02 className={cn('mr-1 h-3.5 w-3.5 shrink-0 text-text-accent', isException && 'text-text-warning')} />}
  73. {isEnvVar && <Env className='mr-1 h-3.5 w-3.5 shrink-0 text-util-colors-violet-violet-600' />}
  74. {isChatVar && <BubbleX className='h-3.5 w-3.5 shrink-0 text-util-colors-teal-teal-700' />}
  75. <div
  76. className={cn(
  77. 'ml-0.5 shrink-[2] truncate text-xs font-medium text-text-accent',
  78. !notHasValue && 'max-w-[70px]',
  79. isException && 'text-text-warning',
  80. )}
  81. title={variableName}
  82. >
  83. {variableName}
  84. </div>
  85. <div
  86. className='mx-1 shrink-0 text-xs font-medium text-text-primary'
  87. title={operatorName}
  88. >
  89. {operatorName}
  90. </div>
  91. {
  92. !notHasValue && (
  93. <div className='shrink-[3] truncate text-xs text-text-secondary' title={formatValue}>{isSelect ? selectName : formatValue}</div>
  94. )
  95. }
  96. </div>
  97. )
  98. }
  99. export default memo(ConditionValue)