readonly-input-with-select-var.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import cn from 'classnames'
  5. import { useWorkflow } from '../../../hooks'
  6. import { BlockEnum } from '../../../types'
  7. import { VarBlockIcon } from '../../../block-icon'
  8. import { getNodeInfoById, isENV, isSystemVar } from './variable/utils'
  9. import { Line3 } from '@/app/components/base/icons/src/public/common'
  10. import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
  11. import { Env } from '@/app/components/base/icons/src/vender/line/others'
  12. type Props = {
  13. nodeId: string
  14. value: string
  15. className?: string
  16. }
  17. const VAR_PLACEHOLDER = '@#!@#!'
  18. const ReadonlyInputWithSelectVar: FC<Props> = ({
  19. nodeId,
  20. value,
  21. className,
  22. }) => {
  23. const { getBeforeNodesInSameBranchIncludeParent } = useWorkflow()
  24. const availableNodes = getBeforeNodesInSameBranchIncludeParent(nodeId)
  25. const startNode = availableNodes.find((node: any) => {
  26. return node.data.type === BlockEnum.Start
  27. })
  28. const res = (() => {
  29. const vars: string[] = []
  30. const strWithVarPlaceholder = value.replaceAll(/{{#([^#]*)#}}/g, (_match, p1) => {
  31. vars.push(p1)
  32. return VAR_PLACEHOLDER
  33. })
  34. const html: JSX.Element[] = strWithVarPlaceholder.split(VAR_PLACEHOLDER).map((str, index) => {
  35. if (!vars[index])
  36. return <span className='relative top-[-3px] leading-[16px]' key={index}>{str}</span>
  37. const value = vars[index].split('.')
  38. const isSystem = isSystemVar(value)
  39. const isEnv = isENV(value)
  40. const node = (isSystem ? startNode : getNodeInfoById(availableNodes, value[0]))?.data
  41. const varName = `${isSystem ? 'sys.' : ''}${value[value.length - 1]}`
  42. return (<span key={index}>
  43. <span className='relative top-[-3px] leading-[16px]'>{str}</span>
  44. <div className=' inline-flex h-[16px] items-center px-1.5 rounded-[5px] bg-white'>
  45. {!isEnv && (
  46. <div className='flex items-center'>
  47. <div className='p-[1px]'>
  48. <VarBlockIcon
  49. className='!text-gray-900'
  50. type={node?.type || BlockEnum.Start}
  51. />
  52. </div>
  53. <div className='max-w-[60px] mx-0.5 text-xs font-medium text-gray-700 truncate' title={node?.title}>{node?.title}</div>
  54. <Line3 className='mr-0.5'></Line3>
  55. </div>
  56. )}
  57. <div className='flex items-center text-primary-600'>
  58. {!isEnv && <Variable02 className='shrink-0 w-3.5 h-3.5' />}
  59. {isEnv && <Env className='shrink-0 w-3.5 h-3.5 text-util-colors-violet-violet-600' />}
  60. <div className={cn('max-w-[50px] ml-0.5 text-xs font-medium truncate', isEnv && 'text-gray-900')} title={varName}>{varName}</div>
  61. </div>
  62. </div>
  63. </span>)
  64. })
  65. return html
  66. })()
  67. return (
  68. <div className={cn('break-all text-xs', className)}>
  69. {res}
  70. </div>
  71. )
  72. }
  73. export default React.memo(ReadonlyInputWithSelectVar)