index.tsx 14 KB

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