2024-10-11 12:39:27 +08:00
|
|
|
'use client'
|
|
|
|
|
2024-10-23 16:42:24 +08:00
|
|
|
import React, { useCallback, useState } from 'react'
|
2024-10-11 12:39:27 +08:00
|
|
|
import Modal from '@/app/components/base/modal'
|
2024-10-23 16:42:24 +08:00
|
|
|
import type { PluginDeclaration } from '../../types'
|
|
|
|
import { InstallStep } from '../../types'
|
|
|
|
import Install from './steps/install'
|
|
|
|
import Installed from './steps/installed'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
|
|
|
|
const i18nPrefix = 'plugin.installModal'
|
2024-10-11 12:39:27 +08:00
|
|
|
|
|
|
|
type InstallFromMarketplaceProps = {
|
2024-10-23 16:42:24 +08:00
|
|
|
packageId: string
|
|
|
|
manifest: PluginDeclaration
|
|
|
|
onSuccess: () => void
|
2024-10-11 12:39:27 +08:00
|
|
|
onClose: () => void
|
|
|
|
}
|
|
|
|
|
2024-10-23 16:42:24 +08:00
|
|
|
const InstallFromMarketplace: React.FC<InstallFromMarketplaceProps> = ({
|
|
|
|
packageId,
|
|
|
|
manifest,
|
|
|
|
onSuccess,
|
|
|
|
onClose,
|
|
|
|
}) => {
|
|
|
|
const { t } = useTranslation()
|
2024-10-23 17:52:39 +08:00
|
|
|
// readyToInstall -> check installed -> installed/failed
|
2024-10-23 16:42:24 +08:00
|
|
|
const [step, setStep] = useState<InstallStep>(InstallStep.readyToInstall)
|
2024-10-15 14:56:59 +08:00
|
|
|
|
2024-10-23 16:42:24 +08:00
|
|
|
// TODO: check installed in beta version.
|
2024-10-15 14:56:59 +08:00
|
|
|
|
2024-10-23 16:42:24 +08:00
|
|
|
const getTitle = useCallback(() => {
|
|
|
|
if (step === InstallStep.installed)
|
|
|
|
return t(`${i18nPrefix}.installedSuccessfully`)
|
2024-10-23 17:52:39 +08:00
|
|
|
if (step === InstallStep.installFailed)
|
|
|
|
return t(`${i18nPrefix}.installFailed`)
|
2024-10-23 16:42:24 +08:00
|
|
|
return t(`${i18nPrefix}.installPlugin`)
|
|
|
|
}, [])
|
2024-10-15 14:56:59 +08:00
|
|
|
|
2024-10-23 17:52:39 +08:00
|
|
|
const handleInstalled = useCallback(() => {
|
2024-10-23 16:42:24 +08:00
|
|
|
setStep(InstallStep.installed)
|
|
|
|
}, [])
|
2024-10-11 12:39:27 +08:00
|
|
|
|
2024-10-23 17:52:39 +08:00
|
|
|
const handleFailed = useCallback(() => {
|
|
|
|
setStep(InstallStep.installFailed)
|
|
|
|
}, [])
|
|
|
|
|
2024-10-11 12:39:27 +08:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
isShow={true}
|
|
|
|
onClose={onClose}
|
2024-10-23 16:42:24 +08:00
|
|
|
className='flex min-w-[560px] p-0 flex-col items-start rounded-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadows-shadow-xl'
|
2024-10-11 12:39:27 +08:00
|
|
|
closable
|
|
|
|
>
|
|
|
|
<div className='flex pt-6 pl-6 pb-3 pr-14 items-start gap-2 self-stretch'>
|
2024-10-15 14:56:59 +08:00
|
|
|
<div className='self-stretch text-text-primary title-2xl-semi-bold'>
|
2024-10-23 16:42:24 +08:00
|
|
|
{getTitle()}
|
2024-10-11 12:39:27 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-10-23 16:42:24 +08:00
|
|
|
{
|
|
|
|
step === InstallStep.readyToInstall && (
|
|
|
|
<Install
|
|
|
|
payload={manifest!}
|
|
|
|
onCancel={onClose}
|
|
|
|
onInstalled={handleInstalled}
|
2024-10-23 17:52:39 +08:00
|
|
|
onFailed={handleFailed}
|
2024-10-23 16:42:24 +08:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{
|
2024-10-23 17:52:39 +08:00
|
|
|
([InstallStep.installed, InstallStep.installFailed].includes(step)) && (
|
2024-10-23 16:42:24 +08:00
|
|
|
<Installed
|
|
|
|
payload={manifest!}
|
2024-10-23 17:52:39 +08:00
|
|
|
isFailed={step === InstallStep.installFailed}
|
2024-10-23 16:42:24 +08:00
|
|
|
onCancel={onSuccess}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2024-10-11 12:39:27 +08:00
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default InstallFromMarketplace
|