condition-value.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {
  2. memo,
  3. useMemo,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import type { ComparisonOperator } from '../types'
  7. import {
  8. comparisonOperatorNotRequireValue,
  9. isComparisonOperatorNeedTranslate,
  10. } from '../utils'
  11. import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
  12. import cn from '@/utils/classnames'
  13. import { isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  14. type ConditionValueProps = {
  15. variableSelector: string[]
  16. operator: ComparisonOperator
  17. value: string
  18. }
  19. const ConditionValue = ({
  20. variableSelector,
  21. operator,
  22. value,
  23. }: ConditionValueProps) => {
  24. const { t } = useTranslation()
  25. const variableName = isSystemVar(variableSelector) ? variableSelector.slice(0).join('.') : variableSelector.slice(1).join('.')
  26. const operatorName = isComparisonOperatorNeedTranslate(operator) ? t(`workflow.nodes.ifElse.comparisonOperator.${operator}`) : operator
  27. const notHasValue = comparisonOperatorNotRequireValue(operator)
  28. const formatValue = useMemo(() => {
  29. if (notHasValue)
  30. return ''
  31. return value.replace(/{{#([^#]*)#}}/g, (a, b) => {
  32. const arr = b.split('.')
  33. if (isSystemVar(arr))
  34. return `{{${b}}}`
  35. return `{{${arr.slice(1).join('.')}}}`
  36. })
  37. }, [notHasValue, value])
  38. return (
  39. <div className='flex items-center px-1 h-6 rounded-md bg-workflow-block-parma-bg'>
  40. <Variable02 className='shrink-0 mr-1 w-3.5 h-3.5 text-text-accent' />
  41. <div
  42. className={cn(
  43. 'shrink-0 truncate text-xs font-medium text-text-accent',
  44. !notHasValue && 'max-w-[70px]',
  45. )}
  46. title={variableName}
  47. >
  48. {variableName}
  49. </div>
  50. <div
  51. className='shrink-0 mx-1 text-xs font-medium text-text-primary'
  52. title={operatorName}
  53. >
  54. {operatorName}
  55. </div>
  56. {
  57. !notHasValue && (
  58. <div className='truncate text-xs text-text-secondary' title={formatValue}>{formatValue}</div>
  59. )
  60. }
  61. </div>
  62. )
  63. }
  64. export default memo(ConditionValue)