From 4c0e4e490af2411eb70ca3166e73d0c105df78d5 Mon Sep 17 00:00:00 2001 From: Yi Date: Wed, 6 Nov 2024 16:49:16 +0800 Subject: [PATCH] chore: update the 'Update plugin from GitHub' --- web/app/components/base/select/index.tsx | 7 ++--- .../plugins/install-plugin/hooks.ts | 4 +-- .../install-from-github/index.tsx | 26 +++++++++++++------ .../install-from-github/steps/loaded.tsx | 16 +++++++++--- .../steps/selectPackage.tsx | 12 ++++----- .../plugins/install-plugin/utils.ts | 4 +++ web/i18n/en-US/plugin.ts | 5 +++- web/i18n/zh-Hans/plugin.ts | 5 +++- 8 files changed, 54 insertions(+), 25 deletions(-) diff --git a/web/app/components/base/select/index.tsx b/web/app/components/base/select/index.tsx index 0b8ff096ff..482b923a76 100644 --- a/web/app/components/base/select/index.tsx +++ b/web/app/components/base/select/index.tsx @@ -316,7 +316,7 @@ const PortalSelect: FC = ({ const { t } = useTranslation() const [open, setOpen] = useState(false) const localPlaceholder = placeholder || t('common.placeholder.select') - const selectedItem = items.find(item => item.value === value) + const selectedItem = value ? items.find(item => item.value === value) : undefined return ( = ({ > {selectedItem?.name ?? localPlaceholder} +
{installedValue && selectedItem && selectedItem.value !== installedValue && {installedValue} {'->'} {selectedItem.value} }
)} @@ -356,8 +357,8 @@ const PortalSelect: FC = ({
{ diff --git a/web/app/components/plugins/install-plugin/hooks.ts b/web/app/components/plugins/install-plugin/hooks.ts index 90388a06bf..9054933aa2 100644 --- a/web/app/components/plugins/install-plugin/hooks.ts +++ b/web/app/components/plugins/install-plugin/hooks.ts @@ -64,10 +64,10 @@ export const useGitHubUpload = () => { return GitHubPackage } catch (error) { - setError('Error installing package') + setError('Error uploading package') Toast.notify({ type: 'error', - message: 'Error installing package', + message: 'Error uploading package', }) } finally { diff --git a/web/app/components/plugins/install-plugin/install-from-github/index.tsx b/web/app/components/plugins/install-plugin/install-from-github/index.tsx index e8fdd69f0c..6f30e81f7c 100644 --- a/web/app/components/plugins/install-plugin/install-from-github/index.tsx +++ b/web/app/components/plugins/install-plugin/install-from-github/index.tsx @@ -5,7 +5,7 @@ import Modal from '@/app/components/base/modal' import type { Item } from '@/app/components/base/select' import type { InstallState } from '@/app/components/plugins/types' import { useGitHubReleases } from '../hooks' -import { parseGitHubUrl } from '../utils' +import { convertRepoToUrl, parseGitHubUrl } from '../utils' import type { PluginDeclaration, UpdateFromGitHubPayload } from '../../types' import { InstallStepFromGitHub } from '../../types' import Toast from '@/app/components/base/toast' @@ -17,7 +17,7 @@ import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-ico import { useTranslation } from 'react-i18next' import { usePluginPageContext } from '../../plugin-page/context' -const i18nPrefix = 'plugin.installModal' +const i18nPrefix = 'plugin.installFromGitHub' type InstallFromGitHubProps = { updatePayload?: UpdateFromGitHubPayload @@ -27,14 +27,25 @@ type InstallFromGitHubProps = { const InstallFromGitHub: React.FC = ({ updatePayload, onClose }) => { const { t } = useTranslation() + // const updatePayloadTest = { + // originalPackageInfo: { + // id: '0299ff5e-40cc-4690-9308-6687cf344a21', + // repo: 'YIXIAO0/test', + // version: '1.10.1', + // package: 'openai.difypkg', + // } + // } const [state, setState] = useState({ step: updatePayload ? InstallStepFromGitHub.selectPackage : InstallStepFromGitHub.setUrl, - repoUrl: updatePayload?.originalPackageInfo.repo || '', - selectedVersion: updatePayload?.originalPackageInfo.version || '', - selectedPackage: updatePayload?.originalPackageInfo.package || '', + repoUrl: updatePayload?.originalPackageInfo?.repo + ? convertRepoToUrl(updatePayload.originalPackageInfo.repo) + : '', + selectedVersion: '', + selectedPackage: '', releases: [], }) const { getIconUrl } = useGetIcon() + const { fetchReleases } = useGitHubReleases() const [uniqueIdentifier, setUniqueIdentifier] = useState(null) const [manifest, setManifest] = useState(null) const [errorMsg, setErrorMsg] = useState(null) @@ -55,15 +66,13 @@ const InstallFromGitHub: React.FC = ({ updatePayload, on })) || []) : [] - const { fetchReleases } = useGitHubReleases() - const getTitle = useCallback(() => { if (state.step === InstallStepFromGitHub.installed) return t(`${i18nPrefix}.installedSuccessfully`) if (state.step === InstallStepFromGitHub.installFailed) return t(`${i18nPrefix}.installFailed`) - return t(`${i18nPrefix}.installPlugin`) + return updatePayload ? t(`${i18nPrefix}.updatePlugin`) : t(`${i18nPrefix}.installPlugin`) }, [state.step]) const handleUrlSubmit = async () => { @@ -190,6 +199,7 @@ const InstallFromGitHub: React.FC = ({ updatePayload, on )} {state.step === InstallStepFromGitHub.readyToInstall && ( = ({ + updatePayload, uniqueIdentifier, payload, repoUrl, @@ -54,6 +56,9 @@ const Loaded: React.FC = ({ uniqueIdentifier, ) + if (updatePayload && isInstalled) + await uninstallPlugin(updatePayload.originalPackageInfo.id) + if (isInstalled) { onInstalled() return @@ -64,10 +69,15 @@ const Loaded: React.FC = ({ taskId, pluginUniqueIdentifier: uniqueIdentifier, }) + onInstalled() } catch (e) { - onFailed(e instanceof Error ? e.message : String(e)) + if (typeof e === 'string') { + onFailed(e) + return + } + onFailed() } finally { setIsInstalling(false) diff --git a/web/app/components/plugins/install-plugin/install-from-github/steps/selectPackage.tsx b/web/app/components/plugins/install-plugin/install-from-github/steps/selectPackage.tsx index b6758002cd..1d65175267 100644 --- a/web/app/components/plugins/install-plugin/install-from-github/steps/selectPackage.tsx +++ b/web/app/components/plugins/install-plugin/install-from-github/steps/selectPackage.tsx @@ -41,7 +41,7 @@ const SelectPackage: React.FC = ({ const { t } = useTranslation() const isEdit = Boolean(updatePayload) const [isUploading, setIsUploading] = React.useState(false) - const { handleUpload, error } = useGitHubUpload() + const { handleUpload } = useGitHubUpload() const handleUploadPackage = async () => { if (isUploading) return @@ -59,7 +59,6 @@ const SelectPackage: React.FC = ({ catch (e: any) { if (e.response?.message) onFailed(e.response?.message) - else onFailed(t('plugin.error.uploadFailed')) } @@ -74,16 +73,15 @@ const SelectPackage: React.FC = ({ htmlFor='version' className='flex flex-col justify-center items-start self-stretch text-text-secondary' > - {isEdit ? t('plugin.installFromGitHub.updateVersion') - : t('plugin.installFromGitHub.selectVersion')} - + {t('plugin.installFromGitHub.selectVersion')}