'use client' import React, { useCallback, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { useBoolean } from 'ahooks' import AppUnavailable from '../../base/app-unavailable' import StepsNavBar from './steps-nav-bar' import StepOne from './step-one' import StepTwo from './step-two' import StepThree from './step-three' import { DataSourceType } from '@/models/datasets' import type { DataSet, File, createDocumentResponse } from '@/models/datasets' import { fetchDataSource, fetchTenantInfo } from '@/service/common' import { fetchDataDetail } from '@/service/datasets' import type { DataSourceNotionPage } from '@/models/common' import AccountSetting from '@/app/components/header/account-setting' type Page = DataSourceNotionPage & { workspace_id: string } type DatasetUpdateFormProps = { datasetId?: string } const DatasetUpdateForm = ({ datasetId }: DatasetUpdateFormProps) => { const { t } = useTranslation() const [hasSetAPIKEY, setHasSetAPIKEY] = useState(true) const [isShowSetAPIKey, { setTrue: showSetAPIKey, setFalse: hideSetAPIkey }] = useBoolean() const [hasConnection, setHasConnection] = useState(true) const [isShowDataSourceSetting, { setTrue: showDataSourceSetting, setFalse: hideDataSourceSetting }] = useBoolean() const [dataSourceType, setDataSourceType] = useState(DataSourceType.FILE) const [step, setStep] = useState(1) const [indexingTypeCache, setIndexTypeCache] = useState('') const [file, setFile] = useState() const [result, setResult] = useState() const [hasError, setHasError] = useState(false) const [notionPages, setNotionPages] = useState([]) const updateNotionPages = (value: Page[]) => { setNotionPages(value) } const updateFile = (file?: File) => { setFile(file) } const updateIndexingTypeCache = (type: string) => { setIndexTypeCache(type) } const updateResultCache = (res?: createDocumentResponse) => { setResult(res) } const nextStep = useCallback(() => { setStep(step + 1) }, [step, setStep]) const changeStep = useCallback((delta: number) => { setStep(step + delta) }, [step, setStep]) const checkAPIKey = async () => { const data = await fetchTenantInfo({ url: '/info' }) const hasSetKey = data.providers.some(({ is_valid }) => is_valid) setHasSetAPIKEY(hasSetKey) } const checkNotionConnection = async () => { const { data } = await fetchDataSource({ url: '/data-source/integrates' }) const hasConnection = data.filter(item => item.provider === 'notion') || [] setHasConnection(hasConnection.length > 0) } useEffect(() => { checkAPIKey() checkNotionConnection() }, []) const [detail, setDetail] = useState(null) useEffect(() => { (async () => { if (datasetId) { try { const detail = await fetchDataDetail(datasetId) setDetail(detail) } catch (e) { setHasError(true) } } })() }, [datasetId]) if (hasError) return return (
{step === 1 && } {(step === 2 && (!datasetId || (datasetId && !!detail))) && } {step === 3 && }
{isShowSetAPIKey && { await checkAPIKey() hideSetAPIkey() }} />} {isShowDataSourceSetting && }
) } export default DatasetUpdateForm