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

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