'use client' import type { DataSourceAuth } from '@/app/components/header/account-setting/data-source-page-new/types' import type { DataSourceProvider, NotionPage } from '@/models/common' import type { CrawlOptions, CrawlResultItem, FileItem } from '@/models/datasets' import { RiArrowRightLine, RiFolder6Line } from '@remixicon/react' import { useBoolean } from 'ahooks' import * as React from 'react' import { useCallback, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import Button from '@/app/components/base/button' import NotionConnector from '@/app/components/base/notion-connector' import { NotionPageSelector } from '@/app/components/base/notion-page-selector' import PlanUpgradeModal from '@/app/components/billing/plan-upgrade-modal' import { Plan } from '@/app/components/billing/type' import VectorSpaceFull from '@/app/components/billing/vector-space-full' import { ENABLE_WEBSITE_FIRECRAWL, ENABLE_WEBSITE_JINAREADER, ENABLE_WEBSITE_WATERCRAWL } from '@/config' import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail' import { useProviderContext } from '@/context/provider-context' import { DataSourceType } from '@/models/datasets' import { cn } from '@/utils/classnames' import EmptyDatasetCreationModal from '../empty-dataset-creation-modal' import FilePreview from '../file-preview' import FileUploader from '../file-uploader' import NotionPagePreview from '../notion-page-preview' import Website from '../website' import WebsitePreview from '../website/preview' import s from './index.module.css' import UpgradeCard from './upgrade-card' type IStepOneProps = { datasetId?: string dataSourceType?: DataSourceType dataSourceTypeDisable: boolean onSetting: () => void files: FileItem[] updateFileList: (files: FileItem[]) => void updateFile: (fileItem: FileItem, progress: number, list: FileItem[]) => void notionPages?: NotionPage[] notionCredentialId: string updateNotionPages: (value: NotionPage[]) => void updateNotionCredentialId: (credentialId: string) => void onStepChange: () => void changeType: (type: DataSourceType) => void websitePages?: CrawlResultItem[] updateWebsitePages: (value: CrawlResultItem[]) => void onWebsiteCrawlProviderChange: (provider: DataSourceProvider) => void onWebsiteCrawlJobIdChange: (jobId: string) => void crawlOptions: CrawlOptions onCrawlOptionsChange: (payload: CrawlOptions) => void authedDataSourceList: DataSourceAuth[] } const StepOne = ({ datasetId, dataSourceType: inCreatePageDataSourceType, dataSourceTypeDisable, changeType, onSetting, onStepChange: doOnStepChange, files, updateFileList, updateFile, notionPages = [], notionCredentialId, updateNotionPages, updateNotionCredentialId, websitePages = [], updateWebsitePages, onWebsiteCrawlProviderChange, onWebsiteCrawlJobIdChange, crawlOptions, onCrawlOptionsChange, authedDataSourceList, }: IStepOneProps) => { const dataset = useDatasetDetailContextWithSelector(state => state.dataset) const [showModal, setShowModal] = useState(false) const [currentFile, setCurrentFile] = useState() const [currentNotionPage, setCurrentNotionPage] = useState() const [currentWebsite, setCurrentWebsite] = useState() const { t } = useTranslation() const modalShowHandle = () => setShowModal(true) const modalCloseHandle = () => setShowModal(false) const updateCurrentFile = useCallback((file: File) => { setCurrentFile(file) }, []) const hideFilePreview = useCallback(() => { setCurrentFile(undefined) }, []) const updateCurrentPage = useCallback((page: NotionPage) => { setCurrentNotionPage(page) }, []) const hideNotionPagePreview = useCallback(() => { setCurrentNotionPage(undefined) }, []) const updateWebsite = useCallback((website: CrawlResultItem) => { setCurrentWebsite(website) }, []) const hideWebsitePreview = useCallback(() => { setCurrentWebsite(undefined) }, []) const shouldShowDataSourceTypeList = !datasetId || (datasetId && !dataset?.data_source_type) const isInCreatePage = shouldShowDataSourceTypeList const dataSourceType = isInCreatePage ? inCreatePageDataSourceType : dataset?.data_source_type const { plan, enableBilling } = useProviderContext() const allFileLoaded = (files.length > 0 && files.every(file => file.file.id)) const hasNotin = notionPages.length > 0 const isVectorSpaceFull = plan.usage.vectorSpace >= plan.total.vectorSpace const isShowVectorSpaceFull = (allFileLoaded || hasNotin) && isVectorSpaceFull && enableBilling const supportBatchUpload = !enableBilling || plan.type !== Plan.sandbox const notSupportBatchUpload = !supportBatchUpload const [isShowPlanUpgradeModal, { setTrue: showPlanUpgradeModal, setFalse: hidePlanUpgradeModal, }] = useBoolean(false) const onStepChange = useCallback(() => { if (notSupportBatchUpload) { let isMultiple = false if (dataSourceType === DataSourceType.FILE && files.length > 1) isMultiple = true if (dataSourceType === DataSourceType.NOTION && notionPages.length > 1) isMultiple = true if (dataSourceType === DataSourceType.WEB && websitePages.length > 1) isMultiple = true if (isMultiple) { showPlanUpgradeModal() return } } doOnStepChange() }, [dataSourceType, doOnStepChange, files.length, notSupportBatchUpload, notionPages.length, showPlanUpgradeModal, websitePages.length]) const nextDisabled = useMemo(() => { if (!files.length) return true if (files.some(file => !file.file.id)) return true return isShowVectorSpaceFull }, [files, isShowVectorSpaceFull]) const isNotionAuthed = useMemo(() => { if (!authedDataSourceList) return false const notionSource = authedDataSourceList.find(item => item.provider === 'notion_datasource') if (!notionSource) return false return notionSource.credentials_list.length > 0 }, [authedDataSourceList]) const notionCredentialList = useMemo(() => { return authedDataSourceList.find(item => item.provider === 'notion_datasource')?.credentials_list || [] }, [authedDataSourceList]) return (
{ shouldShowDataSourceTypeList && (
{t('datasetCreation.steps.one')}
) } { shouldShowDataSourceTypeList && (
{ if (dataSourceTypeDisable) return changeType(DataSourceType.FILE) hideNotionPagePreview() hideWebsitePreview() }} > {t('datasetCreation.stepOne.dataSourceType.file')}
{ if (dataSourceTypeDisable) return changeType(DataSourceType.NOTION) hideFilePreview() hideWebsitePreview() }} > {t('datasetCreation.stepOne.dataSourceType.notion')}
{(ENABLE_WEBSITE_FIRECRAWL || ENABLE_WEBSITE_JINAREADER || ENABLE_WEBSITE_WATERCRAWL) && (
{ if (dataSourceTypeDisable) return changeType(DataSourceType.WEB) hideFilePreview() hideNotionPagePreview() }} > {t('datasetCreation.stepOne.dataSourceType.web')}
)}
) } {dataSourceType === DataSourceType.FILE && ( <> {isShowVectorSpaceFull && (
)}
{ enableBilling && plan.type === Plan.sandbox && files.length > 0 && (
) } )} {dataSourceType === DataSourceType.NOTION && ( <> {!isNotionAuthed && } {isNotionAuthed && ( <>
page.page_id)} onSelect={updateNotionPages} onPreview={updateCurrentPage} credentialList={notionCredentialList} onSelectCredential={updateNotionCredentialId} datasetId={datasetId} />
{isShowVectorSpaceFull && (
)}
)} )} {dataSourceType === DataSourceType.WEB && ( <>
{isShowVectorSpaceFull && (
)}
)} {!datasetId && ( <>
{t('datasetCreation.stepOne.emptyDatasetCreation')} )}
{currentFile && } {currentNotionPage && ( )} {currentWebsite && } {isShowPlanUpgradeModal && ( )}
) } export default StepOne