Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

display-content.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React, { useMemo, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { RiBracesLine, RiEyeLine } from '@remixicon/react'
  4. import Textarea from '@/app/components/base/textarea'
  5. import { Markdown } from '@/app/components/base/markdown'
  6. import SchemaEditor from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/schema-editor'
  7. import { SegmentedControl } from '@/app/components/base/segmented-control'
  8. import cn from '@/utils/classnames'
  9. import { ChunkCardList } from '@/app/components/rag-pipeline/components/chunk-card-list'
  10. import type { ChunkInfo } from '@/app/components/rag-pipeline/components/chunk-card-list/types'
  11. import type { ParentMode } from '@/models/datasets'
  12. import { ChunkingMode } from '@/models/datasets'
  13. import { PreviewType, ViewMode } from './types'
  14. import type { VarType } from '../types'
  15. type DisplayContentProps = {
  16. previewType: PreviewType
  17. varType: VarType
  18. schemaType?: string
  19. mdString?: string
  20. jsonString?: string
  21. readonly: boolean
  22. handleTextChange?: (value: string) => void
  23. handleEditorChange?: (value: string) => void
  24. className?: string
  25. }
  26. const DisplayContent = (props: DisplayContentProps) => {
  27. const { previewType, varType, schemaType, mdString, jsonString, readonly, handleTextChange, handleEditorChange, className } = props
  28. const [viewMode, setViewMode] = useState<ViewMode>(ViewMode.Code)
  29. const [isFocused, setIsFocused] = useState(false)
  30. const { t } = useTranslation()
  31. const chunkType = useMemo(() => {
  32. if (previewType !== PreviewType.Chunks || !schemaType)
  33. return undefined
  34. if (schemaType === 'general_structure')
  35. return ChunkingMode.text
  36. if (schemaType === 'parent_child_structure')
  37. return ChunkingMode.parentChild
  38. if (schemaType === 'qa_structure')
  39. return ChunkingMode.qa
  40. }, [previewType, schemaType])
  41. const parentMode = useMemo(() => {
  42. if (previewType !== PreviewType.Chunks || !schemaType || !jsonString)
  43. return undefined
  44. if (schemaType === 'parent_child_structure')
  45. return JSON.parse(jsonString!)?.parent_mode as ParentMode
  46. return undefined
  47. }, [previewType, schemaType, jsonString])
  48. return (
  49. <div className={cn('flex h-full flex-col rounded-[10px] bg-components-input-bg-normal', isFocused && 'bg-components-input-bg-active outline outline-1 outline-components-input-border-active', className)}>
  50. <div className='flex shrink-0 items-center justify-end p-1'>
  51. {previewType === PreviewType.Markdown && (
  52. <div className='system-xs-semibold-uppercase flex grow items-center px-2 py-0.5 text-text-secondary'>
  53. {previewType.toUpperCase()}
  54. </div>
  55. )}
  56. {previewType === PreviewType.Chunks && (
  57. <div className='system-xs-semibold-uppercase flex grow items-center px-2 py-0.5 text-text-secondary'>
  58. {varType.toUpperCase()}{schemaType ? `(${schemaType})` : ''}
  59. </div>
  60. )}
  61. <SegmentedControl
  62. options={[
  63. { value: ViewMode.Code, text: t('workflow.nodes.templateTransform.code'), Icon: RiBracesLine },
  64. { value: ViewMode.Preview, text: t('workflow.common.preview'), Icon: RiEyeLine },
  65. ]}
  66. value={viewMode}
  67. onChange={setViewMode}
  68. size='small'
  69. padding='with'
  70. activeClassName='!text-text-accent-light-mode-only'
  71. btnClassName='!pl-1.5 !pr-0.5 gap-[3px]'
  72. className='shrink-0'
  73. />
  74. </div>
  75. <div className='flex flex-1 overflow-auto rounded-b-[10px] pl-3 pr-1'>
  76. {viewMode === ViewMode.Code && (
  77. previewType === PreviewType.Markdown
  78. ? <Textarea
  79. readOnly={readonly}
  80. disabled={readonly}
  81. className='h-full border-none bg-transparent p-0 text-text-secondary hover:bg-transparent focus:bg-transparent focus:shadow-none'
  82. value={mdString as any}
  83. onChange={e => handleTextChange?.(e.target.value)}
  84. onFocus={() => setIsFocused(true)}
  85. onBlur={() => setIsFocused(false)}
  86. />
  87. : <SchemaEditor
  88. readonly={readonly}
  89. className='overflow-y-auto bg-transparent'
  90. hideTopMenu
  91. schema={jsonString!}
  92. onUpdate={handleEditorChange!}
  93. onFocus={() => setIsFocused(true)}
  94. onBlur={() => setIsFocused(false)}
  95. />
  96. )}
  97. {viewMode === ViewMode.Preview && (
  98. previewType === PreviewType.Markdown
  99. ? <Markdown className='grow overflow-auto rounded-lg px-4 py-3' content={(mdString ?? '') as string} />
  100. : <ChunkCardList
  101. chunkType={chunkType!}
  102. parentMode={parentMode}
  103. chunkInfo={JSON.parse(jsonString!) as ChunkInfo}
  104. />
  105. )}
  106. </div>
  107. </div>
  108. )
  109. }
  110. export default React.memo(DisplayContent)