index.tsx 8.2 KB

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