'use client' import React, { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import FilePreview from '../file-preview' import FileUploader from '../file-uploader' import NotionPagePreview from '../notion-page-preview' import EmptyDatasetCreationModal from '../empty-dataset-creation-modal' import Website from '../website' import WebsitePreview from '../website/preview' import s from './index.module.css' import cn from '@/utils/classnames' import type { CrawlOptions, CrawlResultItem, FileItem } from '@/models/datasets' import type { DataSourceProvider, NotionPage } from '@/models/common' import { DataSourceType } from '@/models/datasets' import Button from '@/app/components/base/button' import { NotionPageSelector } from '@/app/components/base/notion-page-selector' import { useDatasetDetailContext } from '@/context/dataset-detail' import { useProviderContext } from '@/context/provider-context' import VectorSpaceFull from '@/app/components/billing/vector-space-full' type IStepOneProps = { datasetId?: string dataSourceType?: DataSourceType dataSourceTypeDisable: Boolean hasConnection: boolean onSetting: () => void files: FileItem[] updateFileList: (files: FileItem[]) => void updateFile: (fileItem: FileItem, progress: number, list: FileItem[]) => void notionPages?: NotionPage[] updateNotionPages: (value: NotionPage[]) => 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 } type NotionConnectorProps = { onSetting: () => void } export const NotionConnector = ({ onSetting }: NotionConnectorProps) => { const { t } = useTranslation() return (
{t('datasetCreation.stepOne.notionSyncTitle')}
{t('datasetCreation.stepOne.notionSyncTip')}
) } const StepOne = ({ datasetId, dataSourceType: inCreatePageDataSourceType, dataSourceTypeDisable, changeType, hasConnection, onSetting, onStepChange, files, updateFileList, updateFile, notionPages = [], updateNotionPages, websitePages = [], updateWebsitePages, onWebsiteCrawlProviderChange, onWebsiteCrawlJobIdChange, crawlOptions, onCrawlOptionsChange, }: IStepOneProps) => { const { dataset } = useDatasetDetailContext() 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 = (file: File) => { setCurrentFile(file) } const hideFilePreview = () => { setCurrentFile(undefined) } const updateCurrentPage = (page: NotionPage) => { setCurrentNotionPage(page) } const hideNotionPagePreview = () => { setCurrentNotionPage(undefined) } const hideWebsitePreview = () => { 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 notSupportBatchUpload = enableBilling && plan.type === 'sandbox' const nextDisabled = useMemo(() => { if (!files.length) return true if (files.some(file => !file.file.id)) return true if (isShowVectorSpaceFull) return true return false }, [files]) return (
{ shouldShowDataSourceTypeList && (
{t('datasetCreation.steps.one')}
) }
{ shouldShowDataSourceTypeList && (
{ if (dataSourceTypeDisable) return changeType(DataSourceType.FILE) hideFilePreview() hideNotionPagePreview() }} > {t('datasetCreation.stepOne.dataSourceType.file')}
{ if (dataSourceTypeDisable) return changeType(DataSourceType.NOTION) hideFilePreview() hideNotionPagePreview() }} > {t('datasetCreation.stepOne.dataSourceType.notion')}
changeType(DataSourceType.WEB)} > {t('datasetCreation.stepOne.dataSourceType.web')}
) } {dataSourceType === DataSourceType.FILE && ( <> {isShowVectorSpaceFull && (
)} )} {dataSourceType === DataSourceType.NOTION && ( <> {!hasConnection && } {hasConnection && ( <>
page.page_id)} onSelect={updateNotionPages} onPreview={updateCurrentPage} />
{isShowVectorSpaceFull && (
)} )} )} {dataSourceType === DataSourceType.WEB && ( <>
{isShowVectorSpaceFull && (
)} )} {!datasetId && ( <>
{t('datasetCreation.stepOne.emptyDatasetCreation')}
)}
{currentFile && } {currentNotionPage && } {currentWebsite && }
) } export default StepOne