index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. 'use client'
  2. import React, { useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import FilePreview from '../file-preview'
  5. import FileUploader from '../file-uploader'
  6. import NotionPagePreview from '../notion-page-preview'
  7. import EmptyDatasetCreationModal from '../empty-dataset-creation-modal'
  8. import Website from '../website'
  9. import WebsitePreview from '../website/preview'
  10. import s from './index.module.css'
  11. import cn from '@/utils/classnames'
  12. import type { CrawlOptions, CrawlResultItem, FileItem } from '@/models/datasets'
  13. import type { DataSourceProvider, NotionPage } from '@/models/common'
  14. import { DataSourceType } from '@/models/datasets'
  15. import Button from '@/app/components/base/button'
  16. import { NotionPageSelector } from '@/app/components/base/notion-page-selector'
  17. import { useDatasetDetailContext } from '@/context/dataset-detail'
  18. import { useProviderContext } from '@/context/provider-context'
  19. import VectorSpaceFull from '@/app/components/billing/vector-space-full'
  20. type IStepOneProps = {
  21. datasetId?: string
  22. dataSourceType?: DataSourceType
  23. dataSourceTypeDisable: Boolean
  24. hasConnection: boolean
  25. onSetting: () => void
  26. files: FileItem[]
  27. updateFileList: (files: FileItem[]) => void
  28. updateFile: (fileItem: FileItem, progress: number, list: FileItem[]) => void
  29. notionPages?: NotionPage[]
  30. updateNotionPages: (value: NotionPage[]) => void
  31. onStepChange: () => void
  32. changeType: (type: DataSourceType) => void
  33. websitePages?: CrawlResultItem[]
  34. updateWebsitePages: (value: CrawlResultItem[]) => void
  35. onWebsiteCrawlProviderChange: (provider: DataSourceProvider) => void
  36. onWebsiteCrawlJobIdChange: (jobId: string) => void
  37. crawlOptions: CrawlOptions
  38. onCrawlOptionsChange: (payload: CrawlOptions) => void
  39. }
  40. type NotionConnectorProps = {
  41. onSetting: () => void
  42. }
  43. export const NotionConnector = ({ onSetting }: NotionConnectorProps) => {
  44. const { t } = useTranslation()
  45. return (
  46. <div className={s.notionConnectionTip}>
  47. <span className={s.notionIcon} />
  48. <div className={s.title}>{t('datasetCreation.stepOne.notionSyncTitle')}</div>
  49. <div className={s.tip}>{t('datasetCreation.stepOne.notionSyncTip')}</div>
  50. <Button className='h-8' variant='primary' onClick={onSetting}>{t('datasetCreation.stepOne.connect')}</Button>
  51. </div>
  52. )
  53. }
  54. const StepOne = ({
  55. datasetId,
  56. dataSourceType: inCreatePageDataSourceType,
  57. dataSourceTypeDisable,
  58. changeType,
  59. hasConnection,
  60. onSetting,
  61. onStepChange,
  62. files,
  63. updateFileList,
  64. updateFile,
  65. notionPages = [],
  66. updateNotionPages,
  67. websitePages = [],
  68. updateWebsitePages,
  69. onWebsiteCrawlProviderChange,
  70. onWebsiteCrawlJobIdChange,
  71. crawlOptions,
  72. onCrawlOptionsChange,
  73. }: IStepOneProps) => {
  74. const { dataset } = useDatasetDetailContext()
  75. const [showModal, setShowModal] = useState(false)
  76. const [currentFile, setCurrentFile] = useState<File | undefined>()
  77. const [currentNotionPage, setCurrentNotionPage] = useState<NotionPage | undefined>()
  78. const [currentWebsite, setCurrentWebsite] = useState<CrawlResultItem | undefined>()
  79. const { t } = useTranslation()
  80. const modalShowHandle = () => setShowModal(true)
  81. const modalCloseHandle = () => setShowModal(false)
  82. const updateCurrentFile = (file: File) => {
  83. setCurrentFile(file)
  84. }
  85. const hideFilePreview = () => {
  86. setCurrentFile(undefined)
  87. }
  88. const updateCurrentPage = (page: NotionPage) => {
  89. setCurrentNotionPage(page)
  90. }
  91. const hideNotionPagePreview = () => {
  92. setCurrentNotionPage(undefined)
  93. }
  94. const hideWebsitePreview = () => {
  95. setCurrentWebsite(undefined)
  96. }
  97. const shouldShowDataSourceTypeList = !datasetId || (datasetId && !dataset?.data_source_type)
  98. const isInCreatePage = shouldShowDataSourceTypeList
  99. const dataSourceType = isInCreatePage ? inCreatePageDataSourceType : dataset?.data_source_type
  100. const { plan, enableBilling } = useProviderContext()
  101. const allFileLoaded = (files.length > 0 && files.every(file => file.file.id))
  102. const hasNotin = notionPages.length > 0
  103. const isVectorSpaceFull = plan.usage.vectorSpace >= plan.total.vectorSpace
  104. const isShowVectorSpaceFull = (allFileLoaded || hasNotin) && isVectorSpaceFull && enableBilling
  105. const notSupportBatchUpload = enableBilling && plan.type === 'sandbox'
  106. const nextDisabled = useMemo(() => {
  107. if (!files.length)
  108. return true
  109. if (files.some(file => !file.file.id))
  110. return true
  111. if (isShowVectorSpaceFull)
  112. return true
  113. return false
  114. }, [files])
  115. return (
  116. <div className='flex w-full h-full'>
  117. <div className='grow overflow-y-auto relative'>
  118. {
  119. shouldShowDataSourceTypeList && (
  120. <div className={s.stepHeader}>{t('datasetCreation.steps.one')}</div>
  121. )
  122. }
  123. <div className={s.form}>
  124. {
  125. shouldShowDataSourceTypeList && (
  126. <div className='flex items-center mb-8 flex-wrap gap-y-4'>
  127. <div
  128. className={cn(
  129. s.dataSourceItem,
  130. dataSourceType === DataSourceType.FILE && s.active,
  131. dataSourceTypeDisable && dataSourceType !== DataSourceType.FILE && s.disabled,
  132. )}
  133. onClick={() => {
  134. if (dataSourceTypeDisable)
  135. return
  136. changeType(DataSourceType.FILE)
  137. hideFilePreview()
  138. hideNotionPagePreview()
  139. }}
  140. >
  141. <span className={cn(s.datasetIcon)} />
  142. {t('datasetCreation.stepOne.dataSourceType.file')}
  143. </div>
  144. <div
  145. className={cn(
  146. s.dataSourceItem,
  147. dataSourceType === DataSourceType.NOTION && s.active,
  148. dataSourceTypeDisable && dataSourceType !== DataSourceType.NOTION && s.disabled,
  149. )}
  150. onClick={() => {
  151. if (dataSourceTypeDisable)
  152. return
  153. changeType(DataSourceType.NOTION)
  154. hideFilePreview()
  155. hideNotionPagePreview()
  156. }}
  157. >
  158. <span className={cn(s.datasetIcon, s.notion)} />
  159. {t('datasetCreation.stepOne.dataSourceType.notion')}
  160. </div>
  161. <div
  162. className={cn(
  163. s.dataSourceItem,
  164. dataSourceType === DataSourceType.WEB && s.active,
  165. dataSourceTypeDisable && dataSourceType !== DataSourceType.WEB && s.disabled,
  166. )}
  167. onClick={() => changeType(DataSourceType.WEB)}
  168. >
  169. <span className={cn(s.datasetIcon, s.web)} />
  170. {t('datasetCreation.stepOne.dataSourceType.web')}
  171. </div>
  172. </div>
  173. )
  174. }
  175. {dataSourceType === DataSourceType.FILE && (
  176. <>
  177. <FileUploader
  178. fileList={files}
  179. titleClassName={!shouldShowDataSourceTypeList ? 'mt-[30px] !mb-[44px] !text-lg !font-semibold !text-gray-900' : undefined}
  180. prepareFileList={updateFileList}
  181. onFileListUpdate={updateFileList}
  182. onFileUpdate={updateFile}
  183. onPreview={updateCurrentFile}
  184. notSupportBatchUpload={notSupportBatchUpload}
  185. />
  186. {isShowVectorSpaceFull && (
  187. <div className='max-w-[640px] mb-4'>
  188. <VectorSpaceFull />
  189. </div>
  190. )}
  191. <Button disabled={nextDisabled} className={s.submitButton} variant='primary' onClick={onStepChange}>{t('datasetCreation.stepOne.button')}</Button>
  192. </>
  193. )}
  194. {dataSourceType === DataSourceType.NOTION && (
  195. <>
  196. {!hasConnection && <NotionConnector onSetting={onSetting} />}
  197. {hasConnection && (
  198. <>
  199. <div className='mb-8 w-[640px]'>
  200. <NotionPageSelector
  201. value={notionPages.map(page => page.page_id)}
  202. onSelect={updateNotionPages}
  203. onPreview={updateCurrentPage}
  204. />
  205. </div>
  206. {isShowVectorSpaceFull && (
  207. <div className='max-w-[640px] mb-4'>
  208. <VectorSpaceFull />
  209. </div>
  210. )}
  211. <Button disabled={isShowVectorSpaceFull || !notionPages.length} className={s.submitButton} variant='primary' onClick={onStepChange}>{t('datasetCreation.stepOne.button')}</Button>
  212. </>
  213. )}
  214. </>
  215. )}
  216. {dataSourceType === DataSourceType.WEB && (
  217. <>
  218. <div className={cn('mb-8 w-[640px]', !shouldShowDataSourceTypeList && 'mt-12')}>
  219. <Website
  220. onPreview={setCurrentWebsite}
  221. checkedCrawlResult={websitePages}
  222. onCheckedCrawlResultChange={updateWebsitePages}
  223. onCrawlProviderChange={onWebsiteCrawlProviderChange}
  224. onJobIdChange={onWebsiteCrawlJobIdChange}
  225. crawlOptions={crawlOptions}
  226. onCrawlOptionsChange={onCrawlOptionsChange}
  227. />
  228. </div>
  229. {isShowVectorSpaceFull && (
  230. <div className='max-w-[640px] mb-4'>
  231. <VectorSpaceFull />
  232. </div>
  233. )}
  234. <Button disabled={isShowVectorSpaceFull || !websitePages.length} className={s.submitButton} variant='primary' onClick={onStepChange}>{t('datasetCreation.stepOne.button')}</Button>
  235. </>
  236. )}
  237. {!datasetId && (
  238. <>
  239. <div className={s.dividerLine} />
  240. <div onClick={modalShowHandle} className={s.OtherCreationOption}>{t('datasetCreation.stepOne.emptyDatasetCreation')}</div>
  241. </>
  242. )}
  243. </div>
  244. <EmptyDatasetCreationModal show={showModal} onHide={modalCloseHandle} />
  245. </div>
  246. {currentFile && <FilePreview file={currentFile} hidePreview={hideFilePreview} />}
  247. {currentNotionPage && <NotionPagePreview currentPage={currentNotionPage} hidePreview={hideNotionPagePreview} />}
  248. {currentWebsite && <WebsitePreview payload={currentWebsite} hidePreview={hideWebsitePreview} />}
  249. </div>
  250. )
  251. }
  252. export default StepOne