feat: add version check before Plugin install from GitHub

pull/12372/head
Yi 1 year ago
parent ab9a177c90
commit 1e2ee61f6a

@ -1,10 +1,9 @@
'use client' 'use client'
import React from 'react' import React, { useEffect } from 'react'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import { type PluginDeclaration, type PluginType, TaskStatus, type UpdateFromGitHubPayload } from '../../../types' import { type Plugin, type PluginDeclaration, TaskStatus, type UpdateFromGitHubPayload } from '../../../types'
import Card from '../../../card' import Card from '../../../card'
import Badge, { BadgeState } from '@/app/components/base/badge/index'
import { pluginManifestToCardPluginProps } from '../../utils' import { pluginManifestToCardPluginProps } from '../../utils'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { updateFromGitHub } from '@/service/plugins' import { updateFromGitHub } from '@/service/plugins'
@ -12,13 +11,14 @@ import { useInstallPackageFromGitHub } from '@/service/use-plugins'
import { RiLoader2Line } from '@remixicon/react' import { RiLoader2Line } from '@remixicon/react'
import { usePluginTaskList } from '@/service/use-plugins' import { usePluginTaskList } from '@/service/use-plugins'
import checkTaskStatus from '../../base/check-task-status' import checkTaskStatus from '../../base/check-task-status'
import useCheckInstalled from '@/app/components/plugins/install-plugin/hooks/use-check-installed'
import { parseGitHubUrl } from '../../utils' import { parseGitHubUrl } from '../../utils'
import { useCategories } from '../../../hooks' import Version from '../../base/version'
type LoadedProps = { type LoadedProps = {
updatePayload: UpdateFromGitHubPayload updatePayload: UpdateFromGitHubPayload
uniqueIdentifier: string uniqueIdentifier: string
payload: PluginDeclaration payload: PluginDeclaration | Plugin
repoUrl: string repoUrl: string
selectedVersion: string selectedVersion: string
selectedPackage: string selectedPackage: string
@ -41,12 +41,26 @@ const Loaded: React.FC<LoadedProps> = ({
onFailed, onFailed,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const { categoriesMap } = useCategories() const toInstallVersion = payload.version
const pluginId = (payload as Plugin).plugin_id
const { installedInfo, isLoading } = useCheckInstalled({
pluginIds: [pluginId],
enabled: !!pluginId,
})
const installedInfoPayload = installedInfo?.[pluginId]
const installedVersion = installedInfoPayload?.installedVersion
const hasInstalled = !!installedVersion
const [isInstalling, setIsInstalling] = React.useState(false) const [isInstalling, setIsInstalling] = React.useState(false)
const { mutateAsync: installPackageFromGitHub } = useInstallPackageFromGitHub() const { mutateAsync: installPackageFromGitHub } = useInstallPackageFromGitHub()
const { handleRefetch } = usePluginTaskList() const { handleRefetch } = usePluginTaskList()
const { check } = checkTaskStatus() const { check } = checkTaskStatus()
useEffect(() => {
if (hasInstalled && uniqueIdentifier === installedInfoPayload.uniqueIdentifier)
onInstalled()
}, [hasInstalled])
const handleInstall = async () => { const handleInstall = async () => {
if (isInstalling) return if (isInstalling) return
setIsInstalling(true) setIsInstalling(true)
@ -54,8 +68,9 @@ const Loaded: React.FC<LoadedProps> = ({
try { try {
const { owner, repo } = parseGitHubUrl(repoUrl) const { owner, repo } = parseGitHubUrl(repoUrl)
let taskId let taskId
let isInstalled
if (updatePayload) { if (updatePayload) {
const { all_installed: isInstalled, task_id } = await updateFromGitHub( const { all_installed, task_id } = await updateFromGitHub(
`${owner}/${repo}`, `${owner}/${repo}`,
selectedVersion, selectedVersion,
selectedPackage, selectedPackage,
@ -64,32 +79,42 @@ const Loaded: React.FC<LoadedProps> = ({
) )
taskId = task_id taskId = task_id
isInstalled = all_installed
if (isInstalled) {
onInstalled()
return
}
handleRefetch()
} }
else { else {
const { all_installed: isInstalled, task_id } = await installPackageFromGitHub({ if (hasInstalled) {
repoUrl: `${owner}/${repo}`, const {
selectedVersion, all_installed,
selectedPackage, task_id,
uniqueIdentifier, } = await updateFromGitHub(
}) `${owner}/${repo}`,
selectedVersion,
taskId = task_id selectedPackage,
installedInfoPayload.uniqueIdentifier,
if (isInstalled) { uniqueIdentifier,
onInstalled() )
return taskId = task_id
isInstalled = all_installed
} }
else {
const { all_installed, task_id } = await installPackageFromGitHub({
repoUrl: `${owner}/${repo}`,
selectedVersion,
selectedPackage,
uniqueIdentifier,
})
handleRefetch() taskId = task_id
isInstalled = all_installed
}
}
if (isInstalled) {
onInstalled()
return
} }
handleRefetch()
const { status, error } = await check({ const { status, error } = await check({
taskId, taskId,
pluginUniqueIdentifier: uniqueIdentifier, pluginUniqueIdentifier: uniqueIdentifier,
@ -120,8 +145,12 @@ const Loaded: React.FC<LoadedProps> = ({
<div className='flex p-2 items-start content-start gap-1 self-stretch flex-wrap rounded-2xl bg-background-section-burn'> <div className='flex p-2 items-start content-start gap-1 self-stretch flex-wrap rounded-2xl bg-background-section-burn'>
<Card <Card
className='w-full' className='w-full'
payload={{ ...pluginManifestToCardPluginProps(payload), type: categoriesMap[payload.category].label as PluginType }} payload={pluginManifestToCardPluginProps(payload as PluginDeclaration)}
titleLeft={<Badge className='mx-1' size="s" state={BadgeState.Default}>{payload.version}</Badge>} titleLeft={!isLoading && <Version
hasInstalled={hasInstalled}
installedVersion={installedVersion}
toInstallVersion={toInstallVersion}
/>}
/> />
</div> </div>
<div className='flex justify-end items-center gap-2 self-stretch mt-4'> <div className='flex justify-end items-center gap-2 self-stretch mt-4'>
@ -134,7 +163,7 @@ const Loaded: React.FC<LoadedProps> = ({
variant='primary' variant='primary'
className='min-w-[72px] flex space-x-0.5' className='min-w-[72px] flex space-x-0.5'
onClick={handleInstall} onClick={handleInstall}
disabled={isInstalling} disabled={isInstalling || isLoading}
> >
{isInstalling && <RiLoader2Line className='w-4 h-4 animate-spin-slow' />} {isInstalling && <RiLoader2Line className='w-4 h-4 animate-spin-slow' />}
<span>{t(`${i18nPrefix}.${isInstalling ? 'installing' : 'install'}`)}</span> <span>{t(`${i18nPrefix}.${isInstalling ? 'installing' : 'install'}`)}</span>

Loading…
Cancel
Save