'use client' import React, { useCallback, useState } from 'react' import Modal from '@/app/components/base/modal' import type { PluginDeclaration } from '../../types' import { InstallStep } from '../../types' import Uploading from './steps/uploading' import Install from './steps/install' import Installed from '../base/installed' import { useTranslation } from 'react-i18next' const i18nPrefix = 'plugin.installModal' interface InstallFromLocalPackageProps { file: File onSuccess: () => void onClose: () => void } const InstallFromLocalPackage: React.FC = ({ file, onClose, }) => { const { t } = useTranslation() // uploading -> !uploadFailed -> readyToInstall -> installed/failed const [step, setStep] = useState(InstallStep.uploading) const [uniqueIdentifier, setUniqueIdentifier] = useState(null) const [manifest, setManifest] = useState(null) const [errorMsg, setErrorMsg] = useState(null) const getTitle = useCallback(() => { if (step === InstallStep.uploadFailed) return t(`${i18nPrefix}.uploadFailed`) if (step === InstallStep.installed) return t(`${i18nPrefix}.installedSuccessfully`) if (step === InstallStep.installFailed) return t(`${i18nPrefix}.installFailed`) return t(`${i18nPrefix}.installPlugin`) }, [step]) const handleUploaded = useCallback((result: { uniqueIdentifier: string manifest: PluginDeclaration }) => { setUniqueIdentifier(result.uniqueIdentifier) setManifest(result.manifest) setStep(InstallStep.readyToInstall) }, []) const handleUploadFail = useCallback((errorMsg: string) => { setErrorMsg(errorMsg) setStep(InstallStep.uploadFailed) }, []) const handleInstalled = useCallback(async () => { setStep(InstallStep.installed) }, []) const handleFailed = useCallback((errorMsg?: string) => { setStep(InstallStep.installFailed) if (errorMsg) setErrorMsg(errorMsg) }, []) return (
{getTitle()}
{step === InstallStep.uploading && ( )} { step === InstallStep.readyToInstall && ( ) } { ([InstallStep.uploadFailed, InstallStep.installed, InstallStep.installFailed].includes(step)) && ( ) }
) } export default InstallFromLocalPackage