block-icon.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import type { FC } from 'react'
  2. import { memo } from 'react'
  3. import { BlockEnum } from './types'
  4. import {
  5. Answer,
  6. Assigner,
  7. Code,
  8. DocsExtractor,
  9. End,
  10. Home,
  11. Http,
  12. IfElse,
  13. Iteration,
  14. KnowledgeRetrieval,
  15. ListFilter,
  16. Llm,
  17. ParameterExtractor,
  18. QuestionClassifier,
  19. TemplatingTransform,
  20. VariableX,
  21. } from '@/app/components/base/icons/src/vender/workflow'
  22. import AppIcon from '@/app/components/base/app-icon'
  23. type BlockIconProps = {
  24. type: BlockEnum
  25. size?: string
  26. className?: string
  27. toolIcon?: string | { content: string; background: string }
  28. }
  29. const ICON_CONTAINER_CLASSNAME_SIZE_MAP: Record<string, string> = {
  30. xs: 'w-4 h-4 rounded-[5px] shadow-xs',
  31. sm: 'w-5 h-5 rounded-md shadow-xs',
  32. md: 'w-6 h-6 rounded-lg shadow-md',
  33. }
  34. const getIcon = (type: BlockEnum, className: string) => {
  35. return {
  36. [BlockEnum.Start]: <Home className={className} />,
  37. [BlockEnum.LLM]: <Llm className={className} />,
  38. [BlockEnum.Code]: <Code className={className} />,
  39. [BlockEnum.End]: <End className={className} />,
  40. [BlockEnum.IfElse]: <IfElse className={className} />,
  41. [BlockEnum.HttpRequest]: <Http className={className} />,
  42. [BlockEnum.Answer]: <Answer className={className} />,
  43. [BlockEnum.KnowledgeRetrieval]: <KnowledgeRetrieval className={className} />,
  44. [BlockEnum.QuestionClassifier]: <QuestionClassifier className={className} />,
  45. [BlockEnum.TemplateTransform]: <TemplatingTransform className={className} />,
  46. [BlockEnum.VariableAssigner]: <VariableX className={className} />,
  47. [BlockEnum.VariableAggregator]: <VariableX className={className} />,
  48. [BlockEnum.Assigner]: <Assigner className={className} />,
  49. [BlockEnum.Tool]: <VariableX className={className} />,
  50. [BlockEnum.IterationStart]: <VariableX className={className} />,
  51. [BlockEnum.Iteration]: <Iteration className={className} />,
  52. [BlockEnum.ParameterExtractor]: <ParameterExtractor className={className} />,
  53. [BlockEnum.DocExtractor]: <DocsExtractor className={className} />,
  54. [BlockEnum.ListFilter]: <ListFilter className={className} />,
  55. }[type]
  56. }
  57. const ICON_CONTAINER_BG_COLOR_MAP: Record<string, string> = {
  58. [BlockEnum.Start]: 'bg-util-colors-blue-brand-blue-brand-500',
  59. [BlockEnum.LLM]: 'bg-util-colors-indigo-indigo-500',
  60. [BlockEnum.Code]: 'bg-util-colors-blue-blue-500',
  61. [BlockEnum.End]: 'bg-util-colors-warning-warning-500',
  62. [BlockEnum.IfElse]: 'bg-util-colors-cyan-cyan-500',
  63. [BlockEnum.Iteration]: 'bg-util-colors-cyan-cyan-500',
  64. [BlockEnum.HttpRequest]: 'bg-util-colors-violet-violet-500',
  65. [BlockEnum.Answer]: 'bg-util-colors-warning-warning-500',
  66. [BlockEnum.KnowledgeRetrieval]: 'bg-util-colors-green-green-500',
  67. [BlockEnum.QuestionClassifier]: 'bg-util-colors-green-green-500',
  68. [BlockEnum.TemplateTransform]: 'bg-util-colors-blue-blue-500',
  69. [BlockEnum.VariableAssigner]: 'bg-util-colors-blue-blue-500',
  70. [BlockEnum.VariableAggregator]: 'bg-util-colors-blue-blue-500',
  71. [BlockEnum.Assigner]: 'bg-util-colors-blue-blue-500',
  72. [BlockEnum.ParameterExtractor]: 'bg-util-colors-blue-blue-500',
  73. [BlockEnum.DocExtractor]: 'bg-util-colors-green-green-500',
  74. [BlockEnum.ListFilter]: 'bg-util-colors-cyan-cyan-500',
  75. }
  76. const BlockIcon: FC<BlockIconProps> = ({
  77. type,
  78. size = 'sm',
  79. className,
  80. toolIcon,
  81. }) => {
  82. return (
  83. <div className={`
  84. flex items-center justify-center border-[0.5px] border-white/2 text-white
  85. ${ICON_CONTAINER_CLASSNAME_SIZE_MAP[size]}
  86. ${ICON_CONTAINER_BG_COLOR_MAP[type]}
  87. ${toolIcon && '!shadow-none'}
  88. ${className}
  89. `}
  90. >
  91. {
  92. type !== BlockEnum.Tool && (
  93. getIcon(type, size === 'xs' ? 'w-3 h-3' : 'w-3.5 h-3.5')
  94. )
  95. }
  96. {
  97. type === BlockEnum.Tool && toolIcon && (
  98. <>
  99. {
  100. typeof toolIcon === 'string'
  101. ? (
  102. <div
  103. className='shrink-0 w-full h-full bg-cover bg-center rounded-md'
  104. style={{
  105. backgroundImage: `url(${toolIcon})`,
  106. }}
  107. ></div>
  108. )
  109. : (
  110. <AppIcon
  111. className='shrink-0 !w-full !h-full'
  112. size='tiny'
  113. icon={toolIcon?.content}
  114. background={toolIcon?.background}
  115. />
  116. )
  117. }
  118. </>
  119. )
  120. }
  121. </div>
  122. )
  123. }
  124. export const VarBlockIcon: FC<BlockIconProps> = ({
  125. type,
  126. className,
  127. }) => {
  128. return (
  129. <>
  130. {getIcon(type, `w-3 h-3 ${className}`)}
  131. </>
  132. )
  133. }
  134. export default memo(BlockIcon)