You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 14KB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
Introduce Plugins (#13836) Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: xhe <xw897002528@gmail.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: takatost <takatost@gmail.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Novice Lee <novicelee@NoviPro.local> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: AkaraChen <akarachen@outlook.com> Co-authored-by: Yi <yxiaoisme@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: AkaraChen <85140972+AkaraChen@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Novice <857526207@qq.com> Co-authored-by: Hiroki Nagai <82458324+nagaihiroki-git@users.noreply.github.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in> Co-authored-by: Boris Polonsky <BorisPolonsky@users.noreply.github.com> Co-authored-by: Ademílson Tonato <ademilsonft@outlook.com> Co-authored-by: Ademílson Tonato <ademilson.tonato@refurbed.com> Co-authored-by: IWAI, Masaharu <iwaim.sub@gmail.com> Co-authored-by: Yueh-Po Peng (Yabi) <94939112+y10ab1@users.noreply.github.com> Co-authored-by: Jason <ggbbddjm@gmail.com> Co-authored-by: Xin Zhang <sjhpzx@gmail.com> Co-authored-by: yjc980121 <3898524+yjc980121@users.noreply.github.com> Co-authored-by: heyszt <36215648+hieheihei@users.noreply.github.com> Co-authored-by: Abdullah AlOsaimi <osaimiacc@gmail.com> Co-authored-by: Abdullah AlOsaimi <189027247+osaimi@users.noreply.github.com> Co-authored-by: Yingchun Lai <laiyingchun@apache.org> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: zuodongxu <192560071+zuodongxu@users.noreply.github.com> Co-authored-by: Masashi Tomooka <tmokmss@users.noreply.github.com> Co-authored-by: aplio <ryo.091219@gmail.com> Co-authored-by: Obada Khalili <54270856+obadakhalili@users.noreply.github.com> Co-authored-by: Nam Vu <zuzoovn@gmail.com> Co-authored-by: Kei YAMAZAKI <1715090+kei-yamazaki@users.noreply.github.com> Co-authored-by: TechnoHouse <13776377+deephbz@users.noreply.github.com> Co-authored-by: Riddhimaan-Senapati <114703025+Riddhimaan-Senapati@users.noreply.github.com> Co-authored-by: MaFee921 <31881301+2284730142@users.noreply.github.com> Co-authored-by: te-chan <t-nakanome@sakura-is.co.jp> Co-authored-by: HQidea <HQidea@users.noreply.github.com> Co-authored-by: Joshbly <36315710+Joshbly@users.noreply.github.com> Co-authored-by: xhe <xw897002528@gmail.com> Co-authored-by: weiwenyan-dev <154779315+weiwenyan-dev@users.noreply.github.com> Co-authored-by: ex_wenyan.wei <ex_wenyan.wei@tcl.com> Co-authored-by: engchina <12236799+engchina@users.noreply.github.com> Co-authored-by: engchina <atjapan2015@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: 呆萌闷油瓶 <253605712@qq.com> Co-authored-by: Kemal <kemalmeler@outlook.com> Co-authored-by: Lazy_Frog <4590648+lazyFrogLOL@users.noreply.github.com> Co-authored-by: Yi Xiao <54782454+YIXIAO0@users.noreply.github.com> Co-authored-by: Steven sun <98230804+Tuyohai@users.noreply.github.com> Co-authored-by: steven <sunzwj@digitalchina.com> Co-authored-by: Kalo Chin <91766386+fdb02983rhy@users.noreply.github.com> Co-authored-by: Katy Tao <34019945+KatyTao@users.noreply.github.com> Co-authored-by: depy <42985524+h4ckdepy@users.noreply.github.com> Co-authored-by: 胡春东 <gycm520@gmail.com> Co-authored-by: Junjie.M <118170653@qq.com> Co-authored-by: MuYu <mr.muzea@gmail.com> Co-authored-by: Naoki Takashima <39912547+takatea@users.noreply.github.com> Co-authored-by: Summer-Gu <37869445+gubinjie@users.noreply.github.com> Co-authored-by: Fei He <droxer.he@gmail.com> Co-authored-by: ybalbert001 <120714773+ybalbert001@users.noreply.github.com> Co-authored-by: Yuanbo Li <ybalbert@amazon.com> Co-authored-by: douxc <7553076+douxc@users.noreply.github.com> Co-authored-by: liuzhenghua <1090179900@qq.com> Co-authored-by: Wu Jiayang <62842862+Wu-Jiayang@users.noreply.github.com> Co-authored-by: Your Name <you@example.com> Co-authored-by: kimjion <45935338+kimjion@users.noreply.github.com> Co-authored-by: AugNSo <song.tiankai@icloud.com> Co-authored-by: llinvokerl <38915183+llinvokerl@users.noreply.github.com> Co-authored-by: liusurong.lsr <liusurong.lsr@alibaba-inc.com> Co-authored-by: Vasu Negi <vasu-negi@users.noreply.github.com> Co-authored-by: Hundredwz <1808096180@qq.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
8 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 = (props: NotionConnectorProps) => {
  48. const { onSetting } = props
  49. const { t } = useTranslation()
  50. return (
  51. <div className='flex w-[640px] flex-col items-start rounded-2xl bg-workflow-process-bg p-6'>
  52. <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')} />
  53. <div className='mb-1 flex flex-col gap-y-1 pb-3 pt-1'>
  54. <span className='system-md-semibold text-text-secondary'>
  55. {t('datasetCreation.stepOne.notionSyncTitle')}
  56. <Icon3Dots className='relative -left-1.5 -top-2.5 inline h-4 w-4 text-text-secondary' />
  57. </span>
  58. <div className='system-sm-regular text-text-tertiary'>{t('datasetCreation.stepOne.notionSyncTip')}</div>
  59. </div>
  60. <Button className='h-8' variant='primary' onClick={onSetting}>{t('datasetCreation.stepOne.connect')}</Button>
  61. </div>
  62. )
  63. }
  64. const StepOne = ({
  65. datasetId,
  66. dataSourceType: inCreatePageDataSourceType,
  67. dataSourceTypeDisable,
  68. changeType,
  69. hasConnection,
  70. onSetting,
  71. onStepChange,
  72. files,
  73. updateFileList,
  74. updateFile,
  75. notionPages = [],
  76. updateNotionPages,
  77. websitePages = [],
  78. updateWebsitePages,
  79. onWebsiteCrawlProviderChange,
  80. onWebsiteCrawlJobIdChange,
  81. crawlOptions,
  82. onCrawlOptionsChange,
  83. }: IStepOneProps) => {
  84. const { dataset } = useDatasetDetailContext()
  85. const [showModal, setShowModal] = useState(false)
  86. const [currentFile, setCurrentFile] = useState<File | undefined>()
  87. const [currentNotionPage, setCurrentNotionPage] = useState<NotionPage | undefined>()
  88. const [currentWebsite, setCurrentWebsite] = useState<CrawlResultItem | undefined>()
  89. const { t } = useTranslation()
  90. const modalShowHandle = () => setShowModal(true)
  91. const modalCloseHandle = () => setShowModal(false)
  92. const updateCurrentFile = (file: File) => {
  93. setCurrentFile(file)
  94. }
  95. const hideFilePreview = () => {
  96. setCurrentFile(undefined)
  97. }
  98. const updateCurrentPage = (page: NotionPage) => {
  99. setCurrentNotionPage(page)
  100. }
  101. const hideNotionPagePreview = () => {
  102. setCurrentNotionPage(undefined)
  103. }
  104. const hideWebsitePreview = () => {
  105. setCurrentWebsite(undefined)
  106. }
  107. const shouldShowDataSourceTypeList = !datasetId || (datasetId && !dataset?.data_source_type)
  108. const isInCreatePage = shouldShowDataSourceTypeList
  109. const dataSourceType = isInCreatePage ? inCreatePageDataSourceType : dataset?.data_source_type
  110. const { plan, enableBilling } = useProviderContext()
  111. const allFileLoaded = (files.length > 0 && files.every(file => file.file.id))
  112. const hasNotin = notionPages.length > 0
  113. const isVectorSpaceFull = plan.usage.vectorSpace >= plan.total.vectorSpace
  114. const isShowVectorSpaceFull = (allFileLoaded || hasNotin) && isVectorSpaceFull && enableBilling
  115. const notSupportBatchUpload = enableBilling && plan.type === 'sandbox'
  116. const nextDisabled = useMemo(() => {
  117. if (!files.length)
  118. return true
  119. if (files.some(file => !file.file.id))
  120. return true
  121. return isShowVectorSpaceFull
  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. {(ENABLE_WEBSITE_FIRECRAWL || ENABLE_WEBSITE_JINAREADER || ENABLE_WEBSITE_WATERCRAWL) && (
  186. <div
  187. className={cn(
  188. s.dataSourceItem,
  189. 'system-sm-medium',
  190. dataSourceType === DataSourceType.WEB && s.active,
  191. dataSourceTypeDisable && dataSourceType !== DataSourceType.WEB && s.disabled,
  192. )}
  193. onClick={() => changeType(DataSourceType.WEB)}
  194. >
  195. <span className={cn(s.datasetIcon, s.web)} />
  196. <span
  197. title={t('datasetCreation.stepOne.dataSourceType.web')!}
  198. className='truncate'
  199. >
  200. {t('datasetCreation.stepOne.dataSourceType.web')}
  201. </span>
  202. </div>
  203. )}
  204. </div>
  205. )
  206. }
  207. {dataSourceType === DataSourceType.FILE && (
  208. <>
  209. <FileUploader
  210. fileList={files}
  211. titleClassName={!shouldShowDataSourceTypeList ? 'mt-[30px] !mb-[44px] !text-lg' : undefined}
  212. prepareFileList={updateFileList}
  213. onFileListUpdate={updateFileList}
  214. onFileUpdate={updateFile}
  215. onPreview={updateCurrentFile}
  216. notSupportBatchUpload={notSupportBatchUpload}
  217. />
  218. {isShowVectorSpaceFull && (
  219. <div className='mb-4 max-w-[640px]'>
  220. <VectorSpaceFull />
  221. </div>
  222. )}
  223. <div className="flex max-w-[640px] justify-end gap-2">
  224. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  225. <Button disabled={nextDisabled} variant='primary' onClick={onStepChange}>
  226. <span className="flex gap-0.5 px-[10px]">
  227. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  228. <RiArrowRightLine className="size-4" />
  229. </span>
  230. </Button>
  231. </div>
  232. </>
  233. )}
  234. {dataSourceType === DataSourceType.NOTION && (
  235. <>
  236. {!hasConnection && <NotionConnector onSetting={onSetting} />}
  237. {hasConnection && (
  238. <>
  239. <div className='mb-8 w-[640px]'>
  240. <NotionPageSelector
  241. value={notionPages.map(page => page.page_id)}
  242. onSelect={updateNotionPages}
  243. onPreview={updateCurrentPage}
  244. />
  245. </div>
  246. {isShowVectorSpaceFull && (
  247. <div className='mb-4 max-w-[640px]'>
  248. <VectorSpaceFull />
  249. </div>
  250. )}
  251. <div className="flex max-w-[640px] justify-end gap-2">
  252. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  253. <Button disabled={isShowVectorSpaceFull || !notionPages.length} variant='primary' onClick={onStepChange}>
  254. <span className="flex gap-0.5 px-[10px]">
  255. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  256. <RiArrowRightLine className="size-4" />
  257. </span>
  258. </Button>
  259. </div>
  260. </>
  261. )}
  262. </>
  263. )}
  264. {dataSourceType === DataSourceType.WEB && (
  265. <>
  266. <div className={cn('mb-8 w-[640px]', !shouldShowDataSourceTypeList && 'mt-12')}>
  267. <Website
  268. onPreview={setCurrentWebsite}
  269. checkedCrawlResult={websitePages}
  270. onCheckedCrawlResultChange={updateWebsitePages}
  271. onCrawlProviderChange={onWebsiteCrawlProviderChange}
  272. onJobIdChange={onWebsiteCrawlJobIdChange}
  273. crawlOptions={crawlOptions}
  274. onCrawlOptionsChange={onCrawlOptionsChange}
  275. />
  276. </div>
  277. {isShowVectorSpaceFull && (
  278. <div className='mb-4 max-w-[640px]'>
  279. <VectorSpaceFull />
  280. </div>
  281. )}
  282. <div className="flex max-w-[640px] justify-end gap-2">
  283. {/* <Button>{t('datasetCreation.stepOne.cancel')}</Button> */}
  284. <Button disabled={isShowVectorSpaceFull || !websitePages.length} variant='primary' onClick={onStepChange}>
  285. <span className="flex gap-0.5 px-[10px]">
  286. <span className="px-0.5">{t('datasetCreation.stepOne.button')}</span>
  287. <RiArrowRightLine className="size-4" />
  288. </span>
  289. </Button>
  290. </div>
  291. </>
  292. )}
  293. {!datasetId && (
  294. <>
  295. <div className='my-8 h-px max-w-[640px] bg-divider-regular' />
  296. <span className="inline-flex cursor-pointer items-center text-[13px] leading-4 text-text-accent" onClick={modalShowHandle}>
  297. <RiFolder6Line className="mr-1 size-4" />
  298. {t('datasetCreation.stepOne.emptyDatasetCreation')}
  299. </span>
  300. </>
  301. )}
  302. </div>
  303. <EmptyDatasetCreationModal show={showModal} onHide={modalCloseHandle} />
  304. </div>
  305. </div>
  306. <div className='h-full w-1/2 overflow-y-auto'>
  307. {currentFile && <FilePreview file={currentFile} hidePreview={hideFilePreview} />}
  308. {currentNotionPage && <NotionPagePreview currentPage={currentNotionPage} hidePreview={hideNotionPagePreview} />}
  309. {currentWebsite && <WebsitePreview payload={currentWebsite} hidePreview={hideWebsitePreview} />}
  310. </div>
  311. </div>
  312. </div>
  313. )
  314. }
  315. export default StepOne