condition-value.tsx 2.3 KB

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