dify/web/app/components/plugins/install-plugin/install-from-local-package/index.tsx

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-10-11 12:39:27 +08:00
'use client'
2024-10-22 17:21:25 +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-22 17:21:25 +08:00
import type { PluginDeclaration } from '../../types'
import { InstallStep } from '../../types'
import Uploading from './steps/uploading'
import Install from './steps/install'
import Installed from './steps/installed'
2024-10-22 18:11:47 +08:00
import { useTranslation } from 'react-i18next'
const i18nPrefix = 'plugin.installModal'
2024-10-11 12:39:27 +08:00
type InstallFromLocalPackageProps = {
file: File
2024-10-22 17:21:25 +08:00
onSuccess: () => void
2024-10-11 12:39:27 +08:00
onClose: () => void
}
2024-10-22 17:21:25 +08:00
const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
file,
2024-10-22 18:11:47 +08:00
onClose,
2024-10-22 17:21:25 +08:00
}) => {
2024-10-22 18:11:47 +08:00
const { t } = useTranslation()
2024-10-22 17:21:25 +08:00
const [step, setStep] = useState<InstallStep>(InstallStep.uploading)
2024-10-15 14:56:59 +08:00
2024-10-22 17:21:25 +08:00
const [uniqueIdentifier, setUniqueIdentifier] = useState<string | null>(null)
2024-10-22 18:11:47 +08:00
const getTitle = useCallback(() => {
if (step === InstallStep.installed)
return t(`${i18nPrefix}.installedSuccessfully`)
return t(`${i18nPrefix}.installPlugin`)
}, [])
2024-10-22 17:21:25 +08:00
const [manifest, setManifest] = useState<PluginDeclaration | null>({
name: 'Notion Sync',
description: 'Sync your Notion notes with Dify',
} as any)
2024-10-15 14:56:59 +08:00
2024-10-22 17:21:25 +08:00
const handleUploaded = useCallback((result: {
uniqueIdentifier: string
manifest: PluginDeclaration
}) => {
setUniqueIdentifier(result.uniqueIdentifier)
setManifest(result.manifest)
setStep(InstallStep.readyToInstall)
2024-10-15 14:56:59 +08:00
}, [])
2024-10-22 17:21:25 +08:00
const handleInstalled = useCallback(async () => {
setStep(InstallStep.installed)
}, [])
2024-10-15 14:56:59 +08:00
2024-10-11 12:39:27 +08:00
return (
<Modal
isShow={true}
onClose={onClose}
2024-10-15 14:56:59 +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'>
<div className='self-stretch text-text-primary title-2xl-semi-bold'>
2024-10-22 18:11:47 +08:00
{getTitle()}
2024-10-11 12:39:27 +08:00
</div>
</div>
2024-10-22 17:21:25 +08:00
{step === InstallStep.uploading && (
<Uploading
file={file}
onCancel={onClose}
onUploaded={handleUploaded}
/>
)}
{
step === InstallStep.readyToInstall && (
<Install
payload={manifest!}
onCancel={onClose}
onInstalled={handleInstalled}
2024-10-15 14:56:59 +08:00
/>
2024-10-22 17:21:25 +08:00
)
}
{
step === InstallStep.installed && (
<Installed
payload={manifest!}
onCancel={onClose}
/>
)
}
2024-10-11 12:39:27 +08:00
</Modal>
)
}
export default InstallFromLocalPackage