index.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /* eslint-disable no-mixed-operators */
  2. 'use client'
  3. import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useContext } from 'use-context-selector'
  6. import { useBoolean } from 'ahooks'
  7. import { XMarkIcon } from '@heroicons/react/20/solid'
  8. import cn from 'classnames'
  9. import Link from 'next/link'
  10. import { groupBy } from 'lodash-es'
  11. import PreviewItem, { PreviewType } from './preview-item'
  12. import LanguageSelect from './language-select'
  13. import s from './index.module.css'
  14. import type { CreateDocumentReq, CustomFile, FullDocumentDetail, FileIndexingEstimateResponse as IndexingEstimateResponse, NotionInfo, PreProcessingRule, Rules, createDocumentResponse } from '@/models/datasets'
  15. import {
  16. createDocument,
  17. createFirstDocument,
  18. fetchFileIndexingEstimate as didFetchFileIndexingEstimate,
  19. fetchDefaultProcessRule,
  20. } from '@/service/datasets'
  21. import Button from '@/app/components/base/button'
  22. import Loading from '@/app/components/base/loading'
  23. import Toast from '@/app/components/base/toast'
  24. import { formatNumber } from '@/utils/format'
  25. import type { DataSourceNotionPage } from '@/models/common'
  26. import { DataSourceType, DocForm } from '@/models/datasets'
  27. import NotionIcon from '@/app/components/base/notion-icon'
  28. import Switch from '@/app/components/base/switch'
  29. import { MessageChatSquare } from '@/app/components/base/icons/src/public/common'
  30. import { XClose } from '@/app/components/base/icons/src/vender/line/general'
  31. import { useDatasetDetailContext } from '@/context/dataset-detail'
  32. import I18n from '@/context/i18n'
  33. import { IS_CE_EDITION } from '@/config'
  34. type Page = DataSourceNotionPage & { workspace_id: string }
  35. type StepTwoProps = {
  36. isSetting?: boolean
  37. documentDetail?: FullDocumentDetail
  38. hasSetAPIKEY: boolean
  39. onSetting: () => void
  40. datasetId?: string
  41. indexingType?: string
  42. dataSourceType: DataSourceType
  43. files: CustomFile[]
  44. notionPages?: Page[]
  45. onStepChange?: (delta: number) => void
  46. updateIndexingTypeCache?: (type: string) => void
  47. updateResultCache?: (res: createDocumentResponse) => void
  48. onSave?: () => void
  49. onCancel?: () => void
  50. }
  51. enum SegmentType {
  52. AUTO = 'automatic',
  53. CUSTOM = 'custom',
  54. }
  55. enum IndexingType {
  56. QUALIFIED = 'high_quality',
  57. ECONOMICAL = 'economy',
  58. }
  59. const StepTwo = ({
  60. isSetting,
  61. documentDetail,
  62. hasSetAPIKEY,
  63. onSetting,
  64. datasetId,
  65. indexingType,
  66. dataSourceType,
  67. files,
  68. notionPages = [],
  69. onStepChange,
  70. updateIndexingTypeCache,
  71. updateResultCache,
  72. onSave,
  73. onCancel,
  74. }: StepTwoProps) => {
  75. const { t } = useTranslation()
  76. const { locale } = useContext(I18n)
  77. const { mutateDatasetRes } = useDatasetDetailContext()
  78. const scrollRef = useRef<HTMLDivElement>(null)
  79. const [scrolled, setScrolled] = useState(false)
  80. const previewScrollRef = useRef<HTMLDivElement>(null)
  81. const [previewScrolled, setPreviewScrolled] = useState(false)
  82. const [segmentationType, setSegmentationType] = useState<SegmentType>(SegmentType.AUTO)
  83. const [segmentIdentifier, setSegmentIdentifier] = useState('\\n')
  84. const [max, setMax] = useState(1000)
  85. const [rules, setRules] = useState<PreProcessingRule[]>([])
  86. const [defaultConfig, setDefaultConfig] = useState<Rules>()
  87. const hasSetIndexType = !!indexingType
  88. const [indexType, setIndexType] = useState<IndexingType>(
  89. indexingType
  90. || hasSetAPIKEY
  91. ? IndexingType.QUALIFIED
  92. : IndexingType.ECONOMICAL,
  93. )
  94. const [docForm, setDocForm] = useState<DocForm | string>(
  95. datasetId && documentDetail ? documentDetail.doc_form : DocForm.TEXT,
  96. )
  97. const [docLanguage, setDocLanguage] = useState<string>(locale === 'en' ? 'English' : 'Chinese')
  98. const [QATipHide, setQATipHide] = useState(false)
  99. const [previewSwitched, setPreviewSwitched] = useState(false)
  100. const [showPreview, { setTrue: setShowPreview, setFalse: hidePreview }] = useBoolean()
  101. const [customFileIndexingEstimate, setCustomFileIndexingEstimate] = useState<IndexingEstimateResponse | null>(null)
  102. const [automaticFileIndexingEstimate, setAutomaticFileIndexingEstimate] = useState<IndexingEstimateResponse | null>(null)
  103. const fileIndexingEstimate = (() => {
  104. return segmentationType === SegmentType.AUTO ? automaticFileIndexingEstimate : customFileIndexingEstimate
  105. })()
  106. const scrollHandle = (e: any) => {
  107. if (e.target.scrollTop > 0)
  108. setScrolled(true)
  109. else
  110. setScrolled(false)
  111. }
  112. const previewScrollHandle = (e: any) => {
  113. if (e.target.scrollTop > 0)
  114. setPreviewScrolled(true)
  115. else
  116. setPreviewScrolled(false)
  117. }
  118. const getFileName = (name: string) => {
  119. const arr = name.split('.')
  120. return arr.slice(0, -1).join('.')
  121. }
  122. const getRuleName = (key: string) => {
  123. if (key === 'remove_extra_spaces')
  124. return t('datasetCreation.stepTwo.removeExtraSpaces')
  125. if (key === 'remove_urls_emails')
  126. return t('datasetCreation.stepTwo.removeUrlEmails')
  127. if (key === 'remove_stopwords')
  128. return t('datasetCreation.stepTwo.removeStopwords')
  129. }
  130. const ruleChangeHandle = (id: string) => {
  131. const newRules = rules.map((rule) => {
  132. if (rule.id === id) {
  133. return {
  134. id: rule.id,
  135. enabled: !rule.enabled,
  136. }
  137. }
  138. return rule
  139. })
  140. setRules(newRules)
  141. }
  142. const resetRules = () => {
  143. if (defaultConfig) {
  144. setSegmentIdentifier(defaultConfig.segmentation.separator === '\n' ? '\\n' : defaultConfig.segmentation.separator || '\\n')
  145. setMax(defaultConfig.segmentation.max_tokens)
  146. setRules(defaultConfig.pre_processing_rules)
  147. }
  148. }
  149. const fetchFileIndexingEstimate = async (docForm = DocForm.TEXT) => {
  150. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  151. const res = await didFetchFileIndexingEstimate(getFileIndexingEstimateParams(docForm))
  152. if (segmentationType === SegmentType.CUSTOM)
  153. setCustomFileIndexingEstimate(res)
  154. else
  155. setAutomaticFileIndexingEstimate(res)
  156. }
  157. const confirmChangeCustomConfig = () => {
  158. setCustomFileIndexingEstimate(null)
  159. setShowPreview()
  160. fetchFileIndexingEstimate()
  161. setPreviewSwitched(false)
  162. }
  163. const getIndexing_technique = () => indexingType || indexType
  164. const getProcessRule = () => {
  165. const processRule: any = {
  166. rules: {}, // api will check this. It will be removed after api refactored.
  167. mode: segmentationType,
  168. }
  169. if (segmentationType === SegmentType.CUSTOM) {
  170. const ruleObj = {
  171. pre_processing_rules: rules,
  172. segmentation: {
  173. separator: segmentIdentifier === '\\n' ? '\n' : segmentIdentifier,
  174. max_tokens: max,
  175. },
  176. }
  177. processRule.rules = ruleObj
  178. }
  179. return processRule
  180. }
  181. const getNotionInfo = () => {
  182. const workspacesMap = groupBy(notionPages, 'workspace_id')
  183. const workspaces = Object.keys(workspacesMap).map((workspaceId) => {
  184. return {
  185. workspaceId,
  186. pages: workspacesMap[workspaceId],
  187. }
  188. })
  189. return workspaces.map((workspace) => {
  190. return {
  191. workspace_id: workspace.workspaceId,
  192. pages: workspace.pages.map((page) => {
  193. const { page_id, page_name, page_icon, type } = page
  194. return {
  195. page_id,
  196. page_name,
  197. page_icon,
  198. type,
  199. }
  200. }),
  201. }
  202. }) as NotionInfo[]
  203. }
  204. const getFileIndexingEstimateParams = (docForm: DocForm) => {
  205. let params
  206. if (dataSourceType === DataSourceType.FILE) {
  207. params = {
  208. info_list: {
  209. data_source_type: dataSourceType,
  210. file_info_list: {
  211. file_ids: files.map(file => file.id),
  212. },
  213. },
  214. indexing_technique: getIndexing_technique(),
  215. process_rule: getProcessRule(),
  216. doc_form: docForm,
  217. doc_language: docLanguage,
  218. dataset_id: datasetId,
  219. }
  220. }
  221. if (dataSourceType === DataSourceType.NOTION) {
  222. params = {
  223. info_list: {
  224. data_source_type: dataSourceType,
  225. notion_info_list: getNotionInfo(),
  226. },
  227. indexing_technique: getIndexing_technique(),
  228. process_rule: getProcessRule(),
  229. doc_form: docForm,
  230. doc_language: docLanguage,
  231. dataset_id: datasetId,
  232. }
  233. }
  234. return params
  235. }
  236. const getCreationParams = () => {
  237. let params
  238. if (isSetting) {
  239. params = {
  240. original_document_id: documentDetail?.id,
  241. doc_form: docForm,
  242. doc_language: docLanguage,
  243. process_rule: getProcessRule(),
  244. } as CreateDocumentReq
  245. }
  246. else {
  247. params = {
  248. data_source: {
  249. type: dataSourceType,
  250. info_list: {
  251. data_source_type: dataSourceType,
  252. },
  253. },
  254. indexing_technique: getIndexing_technique(),
  255. process_rule: getProcessRule(),
  256. doc_form: docForm,
  257. doc_language: docLanguage,
  258. } as CreateDocumentReq
  259. if (dataSourceType === DataSourceType.FILE) {
  260. params.data_source.info_list.file_info_list = {
  261. file_ids: files.map(file => file.id),
  262. }
  263. }
  264. if (dataSourceType === DataSourceType.NOTION)
  265. params.data_source.info_list.notion_info_list = getNotionInfo()
  266. }
  267. return params
  268. }
  269. const getRules = async () => {
  270. try {
  271. const res = await fetchDefaultProcessRule({ url: '/datasets/process-rule' })
  272. const separator = res.rules.segmentation.separator
  273. setSegmentIdentifier(separator === '\n' ? '\\n' : separator || '\\n')
  274. setMax(res.rules.segmentation.max_tokens)
  275. setRules(res.rules.pre_processing_rules)
  276. setDefaultConfig(res.rules)
  277. }
  278. catch (err) {
  279. console.log(err)
  280. }
  281. }
  282. const getRulesFromDetail = () => {
  283. if (documentDetail) {
  284. const rules = documentDetail.dataset_process_rule.rules
  285. const separator = rules.segmentation.separator
  286. const max = rules.segmentation.max_tokens
  287. setSegmentIdentifier(separator === '\n' ? '\\n' : separator || '\\n')
  288. setMax(max)
  289. setRules(rules.pre_processing_rules)
  290. setDefaultConfig(rules)
  291. }
  292. }
  293. const getDefaultMode = () => {
  294. if (documentDetail)
  295. setSegmentationType(documentDetail.dataset_process_rule.mode)
  296. }
  297. const createHandle = async () => {
  298. try {
  299. let res
  300. const params = getCreationParams()
  301. if (!datasetId) {
  302. res = await createFirstDocument({
  303. body: params,
  304. })
  305. updateIndexingTypeCache && updateIndexingTypeCache(indexType)
  306. updateResultCache && updateResultCache(res)
  307. }
  308. else {
  309. res = await createDocument({
  310. datasetId,
  311. body: params,
  312. })
  313. updateIndexingTypeCache && updateIndexingTypeCache(indexType)
  314. updateResultCache && updateResultCache(res)
  315. }
  316. if (mutateDatasetRes)
  317. mutateDatasetRes()
  318. onStepChange && onStepChange(+1)
  319. isSetting && onSave && onSave()
  320. }
  321. catch (err) {
  322. Toast.notify({
  323. type: 'error',
  324. message: `${err}`,
  325. })
  326. }
  327. }
  328. const handleSwitch = (state: boolean) => {
  329. if (state)
  330. setDocForm(DocForm.QA)
  331. else
  332. setDocForm(DocForm.TEXT)
  333. }
  334. const handleSelect = (language: string) => {
  335. setDocLanguage(language)
  336. }
  337. const changeToEconomicalType = () => {
  338. if (!hasSetIndexType) {
  339. setIndexType(IndexingType.ECONOMICAL)
  340. setDocForm(DocForm.TEXT)
  341. }
  342. }
  343. const previewSwitch = async () => {
  344. setPreviewSwitched(true)
  345. if (segmentationType === SegmentType.AUTO)
  346. setAutomaticFileIndexingEstimate(null)
  347. else
  348. setCustomFileIndexingEstimate(null)
  349. await fetchFileIndexingEstimate(DocForm.QA)
  350. }
  351. useEffect(() => {
  352. // fetch rules
  353. if (!isSetting) {
  354. getRules()
  355. }
  356. else {
  357. getRulesFromDetail()
  358. getDefaultMode()
  359. }
  360. }, [])
  361. useEffect(() => {
  362. scrollRef.current?.addEventListener('scroll', scrollHandle)
  363. return () => {
  364. scrollRef.current?.removeEventListener('scroll', scrollHandle)
  365. }
  366. }, [])
  367. useLayoutEffect(() => {
  368. if (showPreview) {
  369. previewScrollRef.current?.addEventListener('scroll', previewScrollHandle)
  370. return () => {
  371. previewScrollRef.current?.removeEventListener('scroll', previewScrollHandle)
  372. }
  373. }
  374. }, [showPreview])
  375. useEffect(() => {
  376. if (indexingType === IndexingType.ECONOMICAL && docForm === DocForm.QA)
  377. setDocForm(DocForm.TEXT)
  378. }, [indexingType, docForm])
  379. useEffect(() => {
  380. // get indexing type by props
  381. if (indexingType)
  382. setIndexType(indexingType as IndexingType)
  383. else
  384. setIndexType(hasSetAPIKEY ? IndexingType.QUALIFIED : IndexingType.ECONOMICAL)
  385. }, [hasSetAPIKEY, indexingType, datasetId])
  386. useEffect(() => {
  387. if (segmentationType === SegmentType.AUTO) {
  388. setAutomaticFileIndexingEstimate(null)
  389. setShowPreview()
  390. fetchFileIndexingEstimate()
  391. setPreviewSwitched(false)
  392. }
  393. else {
  394. hidePreview()
  395. setCustomFileIndexingEstimate(null)
  396. setPreviewSwitched(false)
  397. }
  398. }, [segmentationType, indexType])
  399. return (
  400. <div className='flex w-full h-full'>
  401. <div ref={scrollRef} className='relative h-full w-full overflow-y-scroll'>
  402. <div className={cn(s.pageHeader, scrolled && s.fixed)}>{t('datasetCreation.steps.two')}</div>
  403. <div className={cn(s.form)}>
  404. <div className={s.label}>{t('datasetCreation.stepTwo.segmentation')}</div>
  405. <div className='max-w-[640px]'>
  406. <div
  407. className={cn(
  408. s.radioItem,
  409. s.segmentationItem,
  410. segmentationType === SegmentType.AUTO && s.active,
  411. )}
  412. onClick={() => setSegmentationType(SegmentType.AUTO)}
  413. >
  414. <span className={cn(s.typeIcon, s.auto)} />
  415. <span className={cn(s.radio)} />
  416. <div className={s.typeHeader}>
  417. <div className={s.title}>{t('datasetCreation.stepTwo.auto')}</div>
  418. <div className={s.tip}>{t('datasetCreation.stepTwo.autoDescription')}</div>
  419. </div>
  420. </div>
  421. <div
  422. className={cn(
  423. s.radioItem,
  424. s.segmentationItem,
  425. segmentationType === SegmentType.CUSTOM && s.active,
  426. segmentationType === SegmentType.CUSTOM && s.custom,
  427. )}
  428. onClick={() => setSegmentationType(SegmentType.CUSTOM)}
  429. >
  430. <span className={cn(s.typeIcon, s.customize)} />
  431. <span className={cn(s.radio)} />
  432. <div className={s.typeHeader}>
  433. <div className={s.title}>{t('datasetCreation.stepTwo.custom')}</div>
  434. <div className={s.tip}>{t('datasetCreation.stepTwo.customDescription')}</div>
  435. </div>
  436. {segmentationType === SegmentType.CUSTOM && (
  437. <div className={s.typeFormBody}>
  438. <div className={s.formRow}>
  439. <div className='w-full'>
  440. <div className={s.label}>{t('datasetCreation.stepTwo.separator')}</div>
  441. <input
  442. type="text"
  443. className={s.input}
  444. placeholder={t('datasetCreation.stepTwo.separatorPlaceholder') || ''} value={segmentIdentifier}
  445. onChange={e => setSegmentIdentifier(e.target.value)}
  446. />
  447. </div>
  448. </div>
  449. <div className={s.formRow}>
  450. <div className='w-full'>
  451. <div className={s.label}>{t('datasetCreation.stepTwo.maxLength')}</div>
  452. <input
  453. type="number"
  454. className={s.input}
  455. placeholder={t('datasetCreation.stepTwo.separatorPlaceholder') || ''} value={max}
  456. onChange={e => setMax(Number(e.target.value))}
  457. />
  458. </div>
  459. </div>
  460. <div className={s.formRow}>
  461. <div className='w-full'>
  462. <div className={s.label}>{t('datasetCreation.stepTwo.rules')}</div>
  463. {rules.map(rule => (
  464. <div key={rule.id} className={s.ruleItem}>
  465. <input id={rule.id} type="checkbox" defaultChecked={rule.enabled} onChange={() => ruleChangeHandle(rule.id)} className="w-4 h-4 rounded border-gray-300 text-blue-700 focus:ring-blue-700" />
  466. <label htmlFor={rule.id} className="ml-2 text-sm font-normal cursor-pointer text-gray-800">{getRuleName(rule.id)}</label>
  467. </div>
  468. ))}
  469. </div>
  470. </div>
  471. <div className={s.formFooter}>
  472. <Button type="primary" className={cn(s.button, '!h-8 text-primary-600')} onClick={confirmChangeCustomConfig}>{t('datasetCreation.stepTwo.preview')}</Button>
  473. <Button className={cn(s.button, 'ml-2 !h-8')} onClick={resetRules}>{t('datasetCreation.stepTwo.reset')}</Button>
  474. </div>
  475. </div>
  476. )}
  477. </div>
  478. </div>
  479. <div className={s.label}>{t('datasetCreation.stepTwo.indexMode')}</div>
  480. <div className='max-w-[640px]'>
  481. <div className='flex items-center gap-3'>
  482. {(!hasSetIndexType || (hasSetIndexType && indexingType === IndexingType.QUALIFIED)) && (
  483. <div
  484. className={cn(
  485. s.radioItem,
  486. s.indexItem,
  487. !hasSetAPIKEY && s.disabled,
  488. !hasSetIndexType && indexType === IndexingType.QUALIFIED && s.active,
  489. hasSetIndexType && s.disabled,
  490. hasSetIndexType && '!w-full',
  491. )}
  492. onClick={() => {
  493. if (hasSetAPIKEY)
  494. setIndexType(IndexingType.QUALIFIED)
  495. }}
  496. >
  497. <span className={cn(s.typeIcon, s.qualified)} />
  498. {!hasSetIndexType && <span className={cn(s.radio)} />}
  499. <div className={s.typeHeader}>
  500. <div className={s.title}>
  501. {t('datasetCreation.stepTwo.qualified')}
  502. {!hasSetIndexType && <span className={s.recommendTag}>{t('datasetCreation.stepTwo.recommend')}</span>}
  503. </div>
  504. <div className={s.tip}>{t('datasetCreation.stepTwo.qualifiedTip')}</div>
  505. <div className='pb-0.5 text-xs font-medium text-gray-500'>{t('datasetCreation.stepTwo.emstimateCost')}</div>
  506. {
  507. fileIndexingEstimate
  508. ? (
  509. <div className='text-xs font-medium text-gray-800'>{formatNumber(fileIndexingEstimate.tokens)} tokens(<span className='text-yellow-500'>${formatNumber(fileIndexingEstimate.total_price)}</span>)</div>
  510. )
  511. : (
  512. <div className={s.calculating}>{t('datasetCreation.stepTwo.calculating')}</div>
  513. )
  514. }
  515. </div>
  516. {!hasSetAPIKEY && (
  517. <div className={s.warningTip}>
  518. <span>{t('datasetCreation.stepTwo.warning')}&nbsp;</span>
  519. <span className={s.click} onClick={onSetting}>{t('datasetCreation.stepTwo.click')}</span>
  520. </div>
  521. )}
  522. </div>
  523. )}
  524. {(!hasSetIndexType || (hasSetIndexType && indexingType === IndexingType.ECONOMICAL)) && (
  525. <div
  526. className={cn(
  527. s.radioItem,
  528. s.indexItem,
  529. !hasSetIndexType && indexType === IndexingType.ECONOMICAL && s.active,
  530. hasSetIndexType && s.disabled,
  531. hasSetIndexType && '!w-full',
  532. )}
  533. onClick={changeToEconomicalType}
  534. >
  535. <span className={cn(s.typeIcon, s.economical)} />
  536. {!hasSetIndexType && <span className={cn(s.radio)} />}
  537. <div className={s.typeHeader}>
  538. <div className={s.title}>{t('datasetCreation.stepTwo.economical')}</div>
  539. <div className={s.tip}>{t('datasetCreation.stepTwo.economicalTip')}</div>
  540. <div className='pb-0.5 text-xs font-medium text-gray-500'>{t('datasetCreation.stepTwo.emstimateCost')}</div>
  541. <div className='text-xs font-medium text-gray-800'>0 tokens</div>
  542. </div>
  543. </div>
  544. )}
  545. </div>
  546. {hasSetIndexType && (
  547. <div className='mt-2 text-xs text-gray-500 font-medium'>
  548. {t('datasetCreation.stepTwo.indexSettedTip')}
  549. <Link className='text-[#155EEF]' href={`/datasets/${datasetId}/settings`}>{t('datasetCreation.stepTwo.datasetSettingLink')}</Link>
  550. </div>
  551. )}
  552. {IS_CE_EDITION && indexType === IndexingType.QUALIFIED && (
  553. <div className='mt-3 rounded-xl bg-gray-50 border border-gray-100'>
  554. <div className='flex justify-between items-center px-5 py-4'>
  555. <div className='flex justify-center items-center w-8 h-8 rounded-lg bg-indigo-50'>
  556. <MessageChatSquare className='w-4 h-4' />
  557. </div>
  558. <div className='grow mx-3'>
  559. <div className='mb-[2px] text-md font-medium text-gray-900'>{t('datasetCreation.stepTwo.QATitle')}</div>
  560. <div className='inline-flex items-center text-[13px] leading-[18px] text-gray-500'>
  561. <span className='pr-1'>{t('datasetCreation.stepTwo.QALanguage')}</span>
  562. <LanguageSelect currentLanguage={docLanguage} onSelect={handleSelect} />
  563. </div>
  564. </div>
  565. <div className='shrink-0'>
  566. <Switch
  567. defaultValue={docForm === DocForm.QA}
  568. onChange={handleSwitch}
  569. size='md'
  570. />
  571. </div>
  572. </div>
  573. {docForm === DocForm.QA && !QATipHide && (
  574. <div className='flex justify-between items-center px-5 py-2 bg-orange-50 border-t border-amber-100 rounded-b-xl text-[13px] leading-[18px] text-medium text-amber-500'>
  575. {t('datasetCreation.stepTwo.QATip')}
  576. <XClose className='w-4 h-4 text-gray-500 cursor-pointer' onClick={() => setQATipHide(true)} />
  577. </div>
  578. )}
  579. </div>
  580. )}
  581. <div className={s.source}>
  582. <div className={s.sourceContent}>
  583. {dataSourceType === DataSourceType.FILE && (
  584. <>
  585. <div className='mb-2 text-xs font-medium text-gray-500'>{t('datasetCreation.stepTwo.fileSource')}</div>
  586. <div className='flex items-center text-sm leading-6 font-medium text-gray-800'>
  587. <span className={cn(s.fileIcon, files.length && s[files[0].extension])} />
  588. {getFileName(files[0].name || '')}
  589. {files.length > 1 && (
  590. <span className={s.sourceCount}>
  591. <span>{t('datasetCreation.stepTwo.other')}</span>
  592. <span>{files.length - 1}</span>
  593. <span>{t('datasetCreation.stepTwo.fileUnit')}</span>
  594. </span>
  595. )}
  596. </div>
  597. </>
  598. )}
  599. {dataSourceType === DataSourceType.NOTION && (
  600. <>
  601. <div className='mb-2 text-xs font-medium text-gray-500'>{t('datasetCreation.stepTwo.notionSource')}</div>
  602. <div className='flex items-center text-sm leading-6 font-medium text-gray-800'>
  603. <NotionIcon
  604. className='shrink-0 mr-1'
  605. type='page'
  606. src={notionPages[0]?.page_icon}
  607. />
  608. {notionPages[0]?.page_name}
  609. {notionPages.length > 1 && (
  610. <span className={s.sourceCount}>
  611. <span>{t('datasetCreation.stepTwo.other')}</span>
  612. <span>{notionPages.length - 1}</span>
  613. <span>{t('datasetCreation.stepTwo.notionUnit')}</span>
  614. </span>
  615. )}
  616. </div>
  617. </>
  618. )}
  619. </div>
  620. <div className={s.divider} />
  621. <div className={s.segmentCount}>
  622. <div className='mb-2 text-xs font-medium text-gray-500'>{t('datasetCreation.stepTwo.emstimateSegment')}</div>
  623. <div className='flex items-center text-sm leading-6 font-medium text-gray-800'>
  624. {
  625. fileIndexingEstimate
  626. ? (
  627. <div className='text-xs font-medium text-gray-800'>{formatNumber(fileIndexingEstimate.total_segments)} </div>
  628. )
  629. : (
  630. <div className={s.calculating}>{t('datasetCreation.stepTwo.calculating')}</div>
  631. )
  632. }
  633. </div>
  634. </div>
  635. </div>
  636. {!isSetting
  637. ? (
  638. <div className='flex items-center mt-8 py-2'>
  639. <Button onClick={() => onStepChange && onStepChange(-1)}>{t('datasetCreation.stepTwo.lastStep')}</Button>
  640. <div className={s.divider} />
  641. <Button type='primary' onClick={createHandle}>{t('datasetCreation.stepTwo.nextStep')}</Button>
  642. </div>
  643. )
  644. : (
  645. <div className='flex items-center mt-8 py-2'>
  646. <Button type='primary' onClick={createHandle}>{t('datasetCreation.stepTwo.save')}</Button>
  647. <Button className='ml-2' onClick={onCancel}>{t('datasetCreation.stepTwo.cancel')}</Button>
  648. </div>
  649. )}
  650. </div>
  651. </div>
  652. </div>
  653. {(showPreview)
  654. ? (
  655. <div ref={previewScrollRef} className={cn(s.previewWrap, 'relativeh-full overflow-y-scroll border-l border-[#F2F4F7]')}>
  656. <div className={cn(s.previewHeader, previewScrolled && `${s.fixed} pb-3`)}>
  657. <div className='flex items-center justify-between px-8'>
  658. <div className='grow flex items-center'>
  659. <div>{t('datasetCreation.stepTwo.previewTitle')}</div>
  660. {docForm === DocForm.QA && !previewSwitched && (
  661. <Button className='ml-2 !h-[26px] !py-[3px] !px-2 !text-xs !font-medium !text-primary-600' onClick={previewSwitch}>{t('datasetCreation.stepTwo.previewButton')}</Button>
  662. )}
  663. </div>
  664. <div className='flex items-center justify-center w-6 h-6 cursor-pointer' onClick={hidePreview}>
  665. <XMarkIcon className='h-4 w-4'></XMarkIcon>
  666. </div>
  667. </div>
  668. {docForm === DocForm.QA && !previewSwitched && (
  669. <div className='px-8 pr-12 text-xs text-gray-500'>
  670. <span>{t('datasetCreation.stepTwo.previewSwitchTipStart')}</span>
  671. <span className='text-amber-600'>{t('datasetCreation.stepTwo.previewSwitchTipEnd')}</span>
  672. </div>
  673. )}
  674. </div>
  675. <div className='my-4 px-8 space-y-4'>
  676. {previewSwitched && docForm === DocForm.QA && fileIndexingEstimate?.qa_preview && (
  677. <>
  678. {fileIndexingEstimate?.qa_preview.map((item, index) => (
  679. <PreviewItem type={PreviewType.QA} key={item.question} qa={item} index={index + 1} />
  680. ))}
  681. </>
  682. )}
  683. {(docForm === DocForm.TEXT || !previewSwitched) && fileIndexingEstimate?.preview && (
  684. <>
  685. {fileIndexingEstimate?.preview.map((item, index) => (
  686. <PreviewItem type={PreviewType.TEXT} key={item} content={item} index={index + 1} />
  687. ))}
  688. </>
  689. )}
  690. {previewSwitched && docForm === DocForm.QA && !fileIndexingEstimate?.qa_preview && (
  691. <div className='flex items-center justify-center h-[200px]'>
  692. <Loading type='area' />
  693. </div>
  694. )}
  695. {!previewSwitched && !fileIndexingEstimate?.preview && (
  696. <div className='flex items-center justify-center h-[200px]'>
  697. <Loading type='area' />
  698. </div>
  699. )}
  700. </div>
  701. </div>
  702. )
  703. : (<div className={cn(s.sideTip)}>
  704. <div className={s.tipCard}>
  705. <span className={s.icon} />
  706. <div className={s.title}>{t('datasetCreation.stepTwo.sideTipTitle')}</div>
  707. <div className={s.content}>
  708. <p className='mb-3'>{t('datasetCreation.stepTwo.sideTipP1')}</p>
  709. <p className='mb-3'>{t('datasetCreation.stepTwo.sideTipP2')}</p>
  710. <p className='mb-3'>{t('datasetCreation.stepTwo.sideTipP3')}</p>
  711. <p>{t('datasetCreation.stepTwo.sideTipP4')}</p>
  712. </div>
  713. </div>
  714. </div>)}
  715. </div>
  716. )
  717. }
  718. export default StepTwo