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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, isConversationVar, 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 { BubbleX, 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 isChatVar = isConversationVar(value)
  41. const node = (isSystem ? startNode : getNodeInfoById(availableNodes, value[0]))?.data
  42. const varName = `${isSystem ? 'sys.' : ''}${value[value.length - 1]}`
  43. return (<span key={index}>
  44. <span className='relative top-[-3px] leading-[16px]'>{str}</span>
  45. <div className=' inline-flex h-[16px] items-center px-1.5 rounded-[5px] bg-white'>
  46. {!isEnv && !isChatVar && (
  47. <div className='flex items-center'>
  48. <div className='p-[1px]'>
  49. <VarBlockIcon
  50. className='!text-gray-900'
  51. type={node?.type || BlockEnum.Start}
  52. />
  53. </div>
  54. <div className='max-w-[60px] mx-0.5 text-xs font-medium text-gray-700 truncate' title={node?.title}>{node?.title}</div>
  55. <Line3 className='mr-0.5'></Line3>
  56. </div>
  57. )}
  58. <div className='flex items-center text-primary-600'>
  59. {!isEnv && !isChatVar && <Variable02 className='shrink-0 w-3.5 h-3.5' />}
  60. {isEnv && <Env className='shrink-0 w-3.5 h-3.5 text-util-colors-violet-violet-600' />}
  61. {isChatVar && <BubbleX className='w-3.5 h-3.5 text-util-colors-teal-teal-700' />}
  62. <div className={cn('max-w-[50px] ml-0.5 text-xs font-medium truncate', (isEnv || isChatVar) && 'text-gray-900')} title={varName}>{varName}</div>
  63. </div>
  64. </div>
  65. </span>)
  66. })
  67. return html
  68. })()
  69. return (
  70. <div className={cn('break-all text-xs', className)}>
  71. {res}
  72. </div>
  73. )
  74. }
  75. export default React.memo(ReadonlyInputWithSelectVar)