index.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use client'
  2. import React, { useCallback, useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import AppUnavailable from '../../base/app-unavailable'
  5. import { ModelTypeEnum } from '../../header/account-setting/model-provider-page/declarations'
  6. import StepsNavBar from './steps-nav-bar'
  7. import StepOne from './step-one'
  8. import StepTwo from './step-two'
  9. import StepThree from './step-three'
  10. import { DataSourceType } from '@/models/datasets'
  11. import type { CrawlOptions, CrawlResultItem, DataSet, FileItem, createDocumentResponse } from '@/models/datasets'
  12. import { fetchDataSource } from '@/service/common'
  13. import { fetchDatasetDetail } from '@/service/datasets'
  14. import { DataSourceProvider, type NotionPage } from '@/models/common'
  15. import { useModalContext } from '@/context/modal-context'
  16. import { useDefaultModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  17. type DatasetUpdateFormProps = {
  18. datasetId?: string
  19. }
  20. const DEFAULT_CRAWL_OPTIONS: CrawlOptions = {
  21. crawl_sub_pages: true,
  22. only_main_content: true,
  23. includes: '',
  24. excludes: '',
  25. limit: 10,
  26. max_depth: '',
  27. use_sitemap: true,
  28. }
  29. const DatasetUpdateForm = ({ datasetId }: DatasetUpdateFormProps) => {
  30. const { t } = useTranslation()
  31. const { setShowAccountSettingModal } = useModalContext()
  32. const [hasConnection, setHasConnection] = useState(true)
  33. const [dataSourceType, setDataSourceType] = useState<DataSourceType>(DataSourceType.FILE)
  34. const [step, setStep] = useState(1)
  35. const [indexingTypeCache, setIndexTypeCache] = useState('')
  36. const [fileList, setFiles] = useState<FileItem[]>([])
  37. const [result, setResult] = useState<createDocumentResponse | undefined>()
  38. const [hasError, setHasError] = useState(false)
  39. const { data: embeddingsDefaultModel } = useDefaultModel(ModelTypeEnum.textEmbedding)
  40. const [notionPages, setNotionPages] = useState<NotionPage[]>([])
  41. const updateNotionPages = (value: NotionPage[]) => {
  42. setNotionPages(value)
  43. }
  44. const [websitePages, setWebsitePages] = useState<CrawlResultItem[]>([])
  45. const [crawlOptions, setCrawlOptions] = useState<CrawlOptions>(DEFAULT_CRAWL_OPTIONS)
  46. const updateFileList = (preparedFiles: FileItem[]) => {
  47. setFiles(preparedFiles)
  48. }
  49. const [websiteCrawlProvider, setWebsiteCrawlProvider] = useState<DataSourceProvider>(DataSourceProvider.fireCrawl)
  50. const [websiteCrawlJobId, setWebsiteCrawlJobId] = useState('')
  51. const updateFile = (fileItem: FileItem, progress: number, list: FileItem[]) => {
  52. const targetIndex = list.findIndex(file => file.fileID === fileItem.fileID)
  53. list[targetIndex] = {
  54. ...list[targetIndex],
  55. progress,
  56. }
  57. setFiles([...list])
  58. // use follow code would cause dirty list update problem
  59. // const newList = list.map((file) => {
  60. // if (file.fileID === fileItem.fileID) {
  61. // return {
  62. // ...fileItem,
  63. // progress,
  64. // }
  65. // }
  66. // return file
  67. // })
  68. // setFiles(newList)
  69. }
  70. const updateIndexingTypeCache = (type: string) => {
  71. setIndexTypeCache(type)
  72. }
  73. const updateResultCache = (res?: createDocumentResponse) => {
  74. setResult(res)
  75. }
  76. const nextStep = useCallback(() => {
  77. setStep(step + 1)
  78. }, [step, setStep])
  79. const changeStep = useCallback((delta: number) => {
  80. setStep(step + delta)
  81. }, [step, setStep])
  82. const checkNotionConnection = async () => {
  83. const { data } = await fetchDataSource({ url: '/data-source/integrates' })
  84. const hasConnection = data.filter(item => item.provider === 'notion') || []
  85. setHasConnection(hasConnection.length > 0)
  86. }
  87. useEffect(() => {
  88. checkNotionConnection()
  89. }, [])
  90. const [detail, setDetail] = useState<DataSet | null>(null)
  91. useEffect(() => {
  92. (async () => {
  93. if (datasetId) {
  94. try {
  95. const detail = await fetchDatasetDetail(datasetId)
  96. setDetail(detail)
  97. }
  98. catch (e) {
  99. setHasError(true)
  100. }
  101. }
  102. })()
  103. }, [datasetId])
  104. if (hasError)
  105. return <AppUnavailable code={500} unknownReason={t('datasetCreation.error.unavailable') as string} />
  106. return (
  107. <div className='flex' style={{ height: 'calc(100vh - 56px)' }}>
  108. <div className="flex flex-col w-11 sm:w-56 overflow-y-auto bg-white border-r border-gray-200 shrink-0">
  109. <StepsNavBar step={step} datasetId={datasetId} />
  110. </div>
  111. <div className="grow bg-white">
  112. <div className={step === 1 ? 'block h-full' : 'hidden'}>
  113. <StepOne
  114. hasConnection={hasConnection}
  115. onSetting={() => setShowAccountSettingModal({ payload: 'data-source' })}
  116. datasetId={datasetId}
  117. dataSourceType={dataSourceType}
  118. dataSourceTypeDisable={!!detail?.data_source_type}
  119. changeType={setDataSourceType}
  120. files={fileList}
  121. updateFile={updateFile}
  122. updateFileList={updateFileList}
  123. notionPages={notionPages}
  124. updateNotionPages={updateNotionPages}
  125. onStepChange={nextStep}
  126. websitePages={websitePages}
  127. updateWebsitePages={setWebsitePages}
  128. onWebsiteCrawlProviderChange={setWebsiteCrawlProvider}
  129. onWebsiteCrawlJobIdChange={setWebsiteCrawlJobId}
  130. crawlOptions={crawlOptions}
  131. onCrawlOptionsChange={setCrawlOptions}
  132. />
  133. </div>
  134. {(step === 2 && (!datasetId || (datasetId && !!detail))) && <StepTwo
  135. isAPIKeySet={!!embeddingsDefaultModel}
  136. onSetting={() => setShowAccountSettingModal({ payload: 'provider' })}
  137. indexingType={detail?.indexing_technique}
  138. datasetId={datasetId}
  139. dataSourceType={dataSourceType}
  140. files={fileList.map(file => file.file)}
  141. notionPages={notionPages}
  142. websitePages={websitePages}
  143. websiteCrawlProvider={websiteCrawlProvider}
  144. websiteCrawlJobId={websiteCrawlJobId}
  145. onStepChange={changeStep}
  146. updateIndexingTypeCache={updateIndexingTypeCache}
  147. updateResultCache={updateResultCache}
  148. crawlOptions={crawlOptions}
  149. />}
  150. {step === 3 && <StepThree
  151. datasetId={datasetId}
  152. datasetName={detail?.name}
  153. indexingType={detail?.indexing_technique || indexingTypeCache}
  154. creationCache={result}
  155. />}
  156. </div>
  157. </div>
  158. )
  159. }
  160. export default DatasetUpdateForm