'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 { RiFolder6Line } from '@remixicon/react' import { useBoolean } from 'ahooks' import { useCallback, useMemo } from 'react' import { useTranslation } from 'react-i18next' import NotionConnector from '@/app/components/base/notion-connector' import { NotionPageSelector } from '@/app/components/base/notion-page-selector' import { Plan } from '@/app/components/billing/type' import VectorSpaceFull from '@/app/components/billing/vector-space-full' 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 FileUploader from '../file-uploader' import Website from '../website' import { DataSourceTypeSelector, NextStepButton, PreviewPanel } from './components' import { usePreviewState } from './hooks' 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[] } // Helper function to check if notion is authenticated function checkNotionAuth(authedDataSourceList: DataSourceAuth[]): boolean { const notionSource = authedDataSourceList.find(item => item.provider === 'notion_datasource') return Boolean(notionSource && notionSource.credentials_list.length > 0) } // Helper function to get notion credential list function getNotionCredentialList(authedDataSourceList: DataSourceAuth[]) { return authedDataSourceList.find(item => item.provider === 'notion_datasource')?.credentials_list || [] } // Lookup table for checking multiple items by data source type const MULTIPLE_ITEMS_CHECK: Record boolean> = { [DataSourceType.FILE]: ({ files }) => files.length > 1, [DataSourceType.NOTION]: ({ notionPages }) => notionPages.length > 1, [DataSourceType.WEB]: ({ websitePages }) => websitePages.length > 1, } 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 { t } = useTranslation() const dataset = useDatasetDetailContextWithSelector(state => state.dataset) const { plan, enableBilling } = useProviderContext() // Preview state management const { currentFile, currentNotionPage, currentWebsite, showFilePreview, hideFilePreview, showNotionPagePreview, hideNotionPagePreview, showWebsitePreview, hideWebsitePreview, } = usePreviewState() // Empty dataset modal state const [showModal, { setTrue: openModal, setFalse: closeModal }] = useBoolean(false) // Plan upgrade modal state const [isShowPlanUpgradeModal, { setTrue: showPlanUpgradeModal, setFalse: hidePlanUpgradeModal }] = useBoolean(false) // Computed values const shouldShowDataSourceTypeList = !datasetId || (datasetId && !dataset?.data_source_type) const isInCreatePage = shouldShowDataSourceTypeList // Default to FILE type when no type is provided from either source const dataSourceType = isInCreatePage ? (inCreatePageDataSourceType ?? DataSourceType.FILE) : (dataset?.data_source_type ?? DataSourceType.FILE) const allFileLoaded = files.length > 0 && files.every(file => file.file.id) const hasNotion = notionPages.length > 0 const isVectorSpaceFull = plan.usage.vectorSpace >= plan.total.vectorSpace const isShowVectorSpaceFull = (allFileLoaded || hasNotion) && isVectorSpaceFull && enableBilling const supportBatchUpload = !enableBilling || plan.type !== Plan.sandbox const isNotionAuthed = useMemo(() => checkNotionAuth(authedDataSourceList), [authedDataSourceList]) const notionCredentialList = useMemo(() => getNotionCredentialList(authedDataSourceList), [authedDataSourceList]) const fileNextDisabled = useMemo(() => { if (!files.length) return true if (files.some(file => !file.file.id)) return true return isShowVectorSpaceFull }, [files, isShowVectorSpaceFull]) // Clear previews when switching data source type const handleClearPreviews = useCallback((newType: DataSourceType) => { if (newType !== DataSourceType.FILE) hideFilePreview() if (newType !== DataSourceType.NOTION) hideNotionPagePreview() if (newType !== DataSourceType.WEB) hideWebsitePreview() }, [hideFilePreview, hideNotionPagePreview, hideWebsitePreview]) // Handle step change with batch upload check const onStepChange = useCallback(() => { if (!supportBatchUpload && dataSourceType) { const checkFn = MULTIPLE_ITEMS_CHECK[dataSourceType] if (checkFn?.({ files, notionPages, websitePages })) { showPlanUpgradeModal() return } } doOnStepChange() }, [dataSourceType, doOnStepChange, files, supportBatchUpload, notionPages, showPlanUpgradeModal, websitePages]) return (
{/* Left Panel - Form */}
{shouldShowDataSourceTypeList && ( <>
{t('steps.one', { ns: 'datasetCreation' })}
)} {/* File Data Source */} {dataSourceType === DataSourceType.FILE && ( <> {isShowVectorSpaceFull && (
)} {enableBilling && plan.type === Plan.sandbox && files.length > 0 && (
)} )} {/* Notion Data Source */} {dataSourceType === DataSourceType.NOTION && ( <> {!isNotionAuthed && } {isNotionAuthed && ( <>
page.page_id)} onSelect={updateNotionPages} onPreview={showNotionPagePreview} credentialList={notionCredentialList} onSelectCredential={updateNotionCredentialId} datasetId={datasetId} />
{isShowVectorSpaceFull && (
)} )} )} {/* Web Data Source */} {dataSourceType === DataSourceType.WEB && ( <>
{isShowVectorSpaceFull && (
)} )} {/* Empty Dataset Creation Link */} {!datasetId && ( <>
{t('stepOne.emptyDatasetCreation', { ns: 'datasetCreation' })} )}
{/* Right Panel - Preview */}
) } export default StepOne