condition-value.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 { BubbleX, Env } from '@/app/components/base/icons/src/vender/line/others'
  13. import cn from '@/utils/classnames'
  14. import { isConversationVar, 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 isEnvVar = isENV(variableSelector)
  30. const isChatVar = isConversationVar(variableSelector)
  31. const formatValue = useMemo(() => {
  32. if (notHasValue)
  33. return ''
  34. return value.replace(/{{#([^#]*)#}}/g, (a, b) => {
  35. const arr: string[] = b.split('.')
  36. if (isSystemVar(arr))
  37. return `{{${b}}}`
  38. return `{{${arr.slice(1).join('.')}}}`
  39. })
  40. }, [notHasValue, value])
  41. return (
  42. <div className='flex items-center px-1 h-6 rounded-md bg-workflow-block-parma-bg'>
  43. {!isEnvVar && !isChatVar && <Variable02 className='shrink-0 mr-1 w-3.5 h-3.5 text-text-accent' />}
  44. {isEnvVar && <Env className='shrink-0 mr-1 w-3.5 h-3.5 text-util-colors-violet-violet-600' />}
  45. {isChatVar && <BubbleX className='w-3.5 h-3.5 text-util-colors-teal-teal-700' />}
  46. <div
  47. className={cn(
  48. 'shrink-0 truncate text-xs font-medium text-text-accent',
  49. !notHasValue && 'max-w-[70px]',
  50. )}
  51. title={variableName}
  52. >
  53. {variableName}
  54. </div>
  55. <div
  56. className='shrink-0 mx-1 text-xs font-medium text-text-primary'
  57. title={operatorName}
  58. >
  59. {operatorName}
  60. </div>
  61. {
  62. !notHasValue && (
  63. <div className='truncate text-xs text-text-secondary' title={formatValue}>{formatValue}</div>
  64. )
  65. }
  66. </div>
  67. )
  68. }
  69. export default memo(ConditionValue)