index.tsx 13 KB

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