index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 flex-col items-start p-6 w-[640px] rounded-2xl bg-workflow-process-bg'>
  50. <span className={cn(s.notionIcon, 'w-12 h-12 p-3 border-[0.5px] border-components-card-border rounded-[10px] shadow-lg shadow-shadow-shadow-5 mb-2')} />
  51. <div className='flex flex-col gap-y-1 pt-1 pb-3 mb-1'>
  52. <span className='text-text-secondary system-md-semibold'>
  53. {t('datasetCreation.stepOne.notionSyncTitle')}
  54. <Icon3Dots className='inline relative -top-2.5 -left-1.5 w-4 h-4 text-text-secondary' />
  55. </span>
  56. <div className='text-text-tertiary system-sm-regular'>{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='flex w-full h-full'>
  125. <div className='w-1/2 h-full overflow-y-auto relative'>
  126. <div className='flex justify-end'>
  127. <div className={classNames(s.form)}>
  128. {
  129. shouldShowDataSourceTypeList && (
  130. <div className={classNames(s.stepHeader, 'text-text-secondary system-md-semibold')}>
  131. {t('datasetCreation.steps.one')}
  132. </div>
  133. )
  134. }
  135. {
  136. shouldShowDataSourceTypeList && (
  137. <div className='grid grid-cols-3 mb-8 gap-4'>
  138. <div
  139. className={cn(
  140. s.dataSourceItem,
  141. 'system-sm-medium',
  142. dataSourceType === DataSourceType.FILE && s.active,
  143. dataSourceTypeDisable && dataSourceType !== DataSourceType.FILE && s.disabled,
  144. )}
  145. onClick={() => {
  146. if (dataSourceTypeDisable)
  147. return
  148. changeType(DataSourceType.FILE)
  149. hideFilePreview()
  150. hideNotionPagePreview()
  151. }}
  152. >
  153. <span className={cn(s.datasetIcon)} />
  154. <span
  155. title={t('datasetCreation.stepOne.dataSourceType.file')}
  156. className='truncate'
  157. >
  158. {t('datasetCreation.stepOne.dataSourceType.file')}
  159. </span>
  160. </div>
  161. <div
  162. className={cn(
  163. s.dataSourceItem,
  164. 'system-sm-medium',
  165. dataSourceType === DataSourceType.NOTION && s.active,
  166. dataSourceTypeDisable && dataSourceType !== DataSourceType.NOTION && s.disabled,
  167. )}
  168. onClick={() => {
  169. if (dataSourceTypeDisable)
  170. return
  171. changeType(DataSourceType.NOTION)
  172. hideFilePreview()
  173. hideNotionPagePreview()
  174. }}
  175. >
  176. <span className={cn(s.datasetIcon, s.notion)} />
  177. <span
  178. title={t('datasetCreation.stepOne.dataSourceType.notion')}
  179. className='truncate'
  180. >
  181. {t('datasetCreation.stepOne.dataSourceType.notion')}
  182. </span>
  183. </div>
  184. <div
  185. className={cn(
  186. s.dataSourceItem,
  187. 'system-sm-medium',
  188. dataSourceType === DataSourceType.WEB && s.active,
  189. dataSourceTypeDisable && dataSourceType !== DataSourceType.WEB && s.disabled,
  190. )}
  191. onClick={() => changeType(DataSourceType.WEB)}
  192. >
  193. <span className={cn(s.datasetIcon, s.web)} />
  194. <span
  195. title={t('datasetCreation.stepOne.dataSourceType.web')}
  196. className='truncate'
  197. >
  198. {t('datasetCreation.stepOne.dataSourceType.web')}
  199. </span>
  200. </div>
  201. </div>
  202. )
  203. }
  204. {dataSourceType === DataSourceType.FILE && (
  205. <>
  206. <FileUploader
  207. fileList={files}
  208. titleClassName={!shouldShowDataSourceTypeList ? 'mt-[30px] !mb-[44px] !text-lg' : undefined}
  209. prepareFileList={updateFileList}
  210. onFileListUpdate={updateFileList}
  211. onFileUpdate={updateFile}
  212. onPreview={updateCurrentFile}
  213. notSupportBatchUpload={notSupportBatchUpload}
  214. />
  215. {isShowVectorSpaceFull && (
  216. <div className='max-w-[640px] mb-4'>
  217. <VectorSpaceFull />
  218. </div>
  219. )}
  220. <div className="flex justify-end gap-2 max-w-[640px]">
  221. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  222. <Button disabled={nextDisabled} variant='primary' onClick={onStepChange}>
  223. <span className="flex gap-0.5 px-[10px]">
  224. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  225. <RiArrowRightLine className="size-4" />
  226. </span>
  227. </Button>
  228. </div>
  229. </>
  230. )}
  231. {dataSourceType === DataSourceType.NOTION && (
  232. <>
  233. {!hasConnection && <NotionConnector onSetting={onSetting} />}
  234. {hasConnection && (
  235. <>
  236. <div className='mb-8 w-[640px]'>
  237. <NotionPageSelector
  238. value={notionPages.map(page => page.page_id)}
  239. onSelect={updateNotionPages}
  240. onPreview={updateCurrentPage}
  241. />
  242. </div>
  243. {isShowVectorSpaceFull && (
  244. <div className='max-w-[640px] mb-4'>
  245. <VectorSpaceFull />
  246. </div>
  247. )}
  248. <div className="flex justify-end gap-2 max-w-[640px]">
  249. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  250. <Button disabled={isShowVectorSpaceFull || !notionPages.length} variant='primary' onClick={onStepChange}>
  251. <span className="flex gap-0.5 px-[10px]">
  252. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  253. <RiArrowRightLine className="size-4" />
  254. </span>
  255. </Button>
  256. </div>
  257. </>
  258. )}
  259. </>
  260. )}
  261. {dataSourceType === DataSourceType.WEB && (
  262. <>
  263. <div className={cn('mb-8 w-[640px]', !shouldShowDataSourceTypeList && 'mt-12')}>
  264. <Website
  265. onPreview={setCurrentWebsite}
  266. checkedCrawlResult={websitePages}
  267. onCheckedCrawlResultChange={updateWebsitePages}
  268. onCrawlProviderChange={onWebsiteCrawlProviderChange}
  269. onJobIdChange={onWebsiteCrawlJobIdChange}
  270. crawlOptions={crawlOptions}
  271. onCrawlOptionsChange={onCrawlOptionsChange}
  272. />
  273. </div>
  274. {isShowVectorSpaceFull && (
  275. <div className='max-w-[640px] mb-4'>
  276. <VectorSpaceFull />
  277. </div>
  278. )}
  279. <div className="flex justify-end gap-2 max-w-[640px]">
  280. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  281. <Button disabled={isShowVectorSpaceFull || !websitePages.length} variant='primary' onClick={onStepChange}>
  282. <span className="flex gap-0.5 px-[10px]">
  283. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  284. <RiArrowRightLine className="size-4" />
  285. </span>
  286. </Button>
  287. </div>
  288. </>
  289. )}
  290. {!datasetId && (
  291. <>
  292. <div className={s.dividerLine} />
  293. <span className="inline-flex items-center cursor-pointer text-[13px] leading-4 text-text-accent" onClick={modalShowHandle}>
  294. <RiFolder6Line className="size-4 mr-1" />
  295. {t('datasetCreation.stepOne.emptyDatasetCreation')}
  296. </span>
  297. </>
  298. )}
  299. </div>
  300. <EmptyDatasetCreationModal show={showModal} onHide={modalCloseHandle} />
  301. </div>
  302. </div>
  303. <div className='w-1/2 h-full overflow-y-auto'>
  304. {currentFile && <FilePreview file={currentFile} hidePreview={hideFilePreview} />}
  305. {currentNotionPage && <NotionPagePreview currentPage={currentNotionPage} hidePreview={hideNotionPagePreview} />}
  306. {currentWebsite && <WebsitePreview payload={currentWebsite} hidePreview={hideWebsitePreview} />}
  307. </div>
  308. </div>
  309. )
  310. }
  311. export default StepOne