您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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. import { ENABLE_WEBSITE_FIRECRAWL, ENABLE_WEBSITE_JINAREADER, ENABLE_WEBSITE_WATERCRAWL } from '@/config'
  24. type IStepOneProps = {
  25. datasetId?: string
  26. dataSourceType?: DataSourceType
  27. dataSourceTypeDisable: boolean
  28. hasConnection: boolean
  29. onSetting: () => void
  30. files: FileItem[]
  31. updateFileList: (files: FileItem[]) => void
  32. updateFile: (fileItem: FileItem, progress: number, list: FileItem[]) => void
  33. notionPages?: NotionPage[]
  34. updateNotionPages: (value: NotionPage[]) => void
  35. onStepChange: () => void
  36. changeType: (type: DataSourceType) => void
  37. websitePages?: CrawlResultItem[]
  38. updateWebsitePages: (value: CrawlResultItem[]) => void
  39. onWebsiteCrawlProviderChange: (provider: DataSourceProvider) => void
  40. onWebsiteCrawlJobIdChange: (jobId: string) => void
  41. crawlOptions: CrawlOptions
  42. onCrawlOptionsChange: (payload: CrawlOptions) => void
  43. }
  44. type NotionConnectorProps = {
  45. onSetting: () => void
  46. }
  47. export const NotionConnector = ({ onSetting }: NotionConnectorProps) => {
  48. const { t } = useTranslation()
  49. return (
  50. <div className='flex w-[640px] flex-col items-start rounded-2xl bg-workflow-process-bg p-6'>
  51. <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')} />
  52. <div className='mb-1 flex flex-col gap-y-1 pb-3 pt-1'>
  53. <span className='system-md-semibold text-text-secondary'>
  54. {t('datasetCreation.stepOne.notionSyncTitle')}
  55. <Icon3Dots className='relative -left-1.5 -top-2.5 inline h-4 w-4 text-text-secondary' />
  56. </span>
  57. <div className='system-sm-regular text-text-tertiary'>{t('datasetCreation.stepOne.notionSyncTip')}</div>
  58. </div>
  59. <Button className='h-8' variant='primary' onClick={onSetting}>{t('datasetCreation.stepOne.connect')}</Button>
  60. </div>
  61. )
  62. }
  63. const StepOne = ({
  64. datasetId,
  65. dataSourceType: inCreatePageDataSourceType,
  66. dataSourceTypeDisable,
  67. changeType,
  68. hasConnection,
  69. onSetting,
  70. onStepChange,
  71. files,
  72. updateFileList,
  73. updateFile,
  74. notionPages = [],
  75. updateNotionPages,
  76. websitePages = [],
  77. updateWebsitePages,
  78. onWebsiteCrawlProviderChange,
  79. onWebsiteCrawlJobIdChange,
  80. crawlOptions,
  81. onCrawlOptionsChange,
  82. }: IStepOneProps) => {
  83. const { dataset } = useDatasetDetailContext()
  84. const [showModal, setShowModal] = useState(false)
  85. const [currentFile, setCurrentFile] = useState<File | undefined>()
  86. const [currentNotionPage, setCurrentNotionPage] = useState<NotionPage | undefined>()
  87. const [currentWebsite, setCurrentWebsite] = useState<CrawlResultItem | undefined>()
  88. const { t } = useTranslation()
  89. const modalShowHandle = () => setShowModal(true)
  90. const modalCloseHandle = () => setShowModal(false)
  91. const updateCurrentFile = (file: File) => {
  92. setCurrentFile(file)
  93. }
  94. const hideFilePreview = () => {
  95. setCurrentFile(undefined)
  96. }
  97. const updateCurrentPage = (page: NotionPage) => {
  98. setCurrentNotionPage(page)
  99. }
  100. const hideNotionPagePreview = () => {
  101. setCurrentNotionPage(undefined)
  102. }
  103. const hideWebsitePreview = () => {
  104. setCurrentWebsite(undefined)
  105. }
  106. const shouldShowDataSourceTypeList = !datasetId || (datasetId && !dataset?.data_source_type)
  107. const isInCreatePage = shouldShowDataSourceTypeList
  108. const dataSourceType = isInCreatePage ? inCreatePageDataSourceType : dataset?.data_source_type
  109. const { plan, enableBilling } = useProviderContext()
  110. const allFileLoaded = (files.length > 0 && files.every(file => file.file.id))
  111. const hasNotin = notionPages.length > 0
  112. const isVectorSpaceFull = plan.usage.vectorSpace >= plan.total.vectorSpace
  113. const isShowVectorSpaceFull = (allFileLoaded || hasNotin) && isVectorSpaceFull && enableBilling
  114. const notSupportBatchUpload = enableBilling && plan.type === 'sandbox'
  115. const nextDisabled = useMemo(() => {
  116. if (!files.length)
  117. return true
  118. if (files.some(file => !file.file.id))
  119. return true
  120. return isShowVectorSpaceFull
  121. }, [files, isShowVectorSpaceFull])
  122. return (
  123. <div className='h-full w-full overflow-x-auto'>
  124. <div className='flex h-full w-full min-w-[1440px]'>
  125. <div className='relative h-full w-1/2 overflow-y-auto'>
  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='mb-8 grid grid-cols-3 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. {(ENABLE_WEBSITE_FIRECRAWL || ENABLE_WEBSITE_JINAREADER || ENABLE_WEBSITE_WATERCRAWL) && (
  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. )}
  203. </div>
  204. )
  205. }
  206. {dataSourceType === DataSourceType.FILE && (
  207. <>
  208. <FileUploader
  209. fileList={files}
  210. titleClassName={!shouldShowDataSourceTypeList ? 'mt-[30px] !mb-[44px] !text-lg' : undefined}
  211. prepareFileList={updateFileList}
  212. onFileListUpdate={updateFileList}
  213. onFileUpdate={updateFile}
  214. onPreview={updateCurrentFile}
  215. notSupportBatchUpload={notSupportBatchUpload}
  216. />
  217. {isShowVectorSpaceFull && (
  218. <div className='mb-4 max-w-[640px]'>
  219. <VectorSpaceFull />
  220. </div>
  221. )}
  222. <div className="flex max-w-[640px] justify-end gap-2">
  223. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  224. <Button disabled={nextDisabled} variant='primary' onClick={onStepChange}>
  225. <span className="flex gap-0.5 px-[10px]">
  226. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  227. <RiArrowRightLine className="size-4" />
  228. </span>
  229. </Button>
  230. </div>
  231. </>
  232. )}
  233. {dataSourceType === DataSourceType.NOTION && (
  234. <>
  235. {!hasConnection && <NotionConnector onSetting={onSetting} />}
  236. {hasConnection && (
  237. <>
  238. <div className='mb-8 w-[640px]'>
  239. <NotionPageSelector
  240. value={notionPages.map(page => page.page_id)}
  241. onSelect={updateNotionPages}
  242. onPreview={updateCurrentPage}
  243. />
  244. </div>
  245. {isShowVectorSpaceFull && (
  246. <div className='mb-4 max-w-[640px]'>
  247. <VectorSpaceFull />
  248. </div>
  249. )}
  250. <div className="flex max-w-[640px] justify-end gap-2">
  251. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  252. <Button disabled={isShowVectorSpaceFull || !notionPages.length} variant='primary' onClick={onStepChange}>
  253. <span className="flex gap-0.5 px-[10px]">
  254. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  255. <RiArrowRightLine className="size-4" />
  256. </span>
  257. </Button>
  258. </div>
  259. </>
  260. )}
  261. </>
  262. )}
  263. {dataSourceType === DataSourceType.WEB && (
  264. <>
  265. <div className={cn('mb-8 w-[640px]', !shouldShowDataSourceTypeList && 'mt-12')}>
  266. <Website
  267. onPreview={setCurrentWebsite}
  268. checkedCrawlResult={websitePages}
  269. onCheckedCrawlResultChange={updateWebsitePages}
  270. onCrawlProviderChange={onWebsiteCrawlProviderChange}
  271. onJobIdChange={onWebsiteCrawlJobIdChange}
  272. crawlOptions={crawlOptions}
  273. onCrawlOptionsChange={onCrawlOptionsChange}
  274. />
  275. </div>
  276. {isShowVectorSpaceFull && (
  277. <div className='mb-4 max-w-[640px]'>
  278. <VectorSpaceFull />
  279. </div>
  280. )}
  281. <div className="flex max-w-[640px] justify-end gap-2">
  282. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  283. <Button disabled={isShowVectorSpaceFull || !websitePages.length} variant='primary' onClick={onStepChange}>
  284. <span className="flex gap-0.5 px-[10px]">
  285. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  286. <RiArrowRightLine className="size-4" />
  287. </span>
  288. </Button>
  289. </div>
  290. </>
  291. )}
  292. {!datasetId && (
  293. <>
  294. <div className='my-8 h-px max-w-[640px] bg-divider-regular' />
  295. <span className="inline-flex cursor-pointer items-center text-[13px] leading-4 text-text-accent" onClick={modalShowHandle}>
  296. <RiFolder6Line className="mr-1 size-4" />
  297. {t('datasetCreation.stepOne.emptyDatasetCreation')}
  298. </span>
  299. </>
  300. )}
  301. </div>
  302. <EmptyDatasetCreationModal show={showModal} onHide={modalCloseHandle} />
  303. </div>
  304. </div>
  305. <div className='h-full w-1/2 overflow-y-auto'>
  306. {currentFile && <FilePreview file={currentFile} hidePreview={hideFilePreview} />}
  307. {currentNotionPage && <NotionPagePreview currentPage={currentNotionPage} hidePreview={hideNotionPagePreview} />}
  308. {currentWebsite && <WebsitePreview payload={currentWebsite} hidePreview={hideWebsitePreview} />}
  309. </div>
  310. </div>
  311. </div>
  312. )
  313. }
  314. export default StepOne