|
|
|
@ -4,21 +4,20 @@ import React, { useCallback, useState } from 'react'
|
|
|
|
import Modal from '@/app/components/base/modal'
|
|
|
|
import Modal from '@/app/components/base/modal'
|
|
|
|
import type { Item } from '@/app/components/base/select'
|
|
|
|
import type { Item } from '@/app/components/base/select'
|
|
|
|
import type { InstallState } from '@/app/components/plugins/types'
|
|
|
|
import type { InstallState } from '@/app/components/plugins/types'
|
|
|
|
import { useGitHubReleases, useGitHubUpload } from '../hooks'
|
|
|
|
import { useGitHubReleases } from '../hooks'
|
|
|
|
import { parseGitHubUrl } from '../utils'
|
|
|
|
import { parseGitHubUrl } from '../utils'
|
|
|
|
import type { PluginDeclaration, UpdateFromGitHubPayload } from '../../types'
|
|
|
|
import type { PluginDeclaration, UpdateFromGitHubPayload } from '../../types'
|
|
|
|
import { InstallStepFromGitHub } from '../../types'
|
|
|
|
import { InstallStepFromGitHub } from '../../types'
|
|
|
|
import checkTaskStatus from '../base/check-task-status'
|
|
|
|
|
|
|
|
import { usePluginTasksStore } from '@/app/components/plugins/plugin-page/store'
|
|
|
|
|
|
|
|
import Toast from '@/app/components/base/toast'
|
|
|
|
import Toast from '@/app/components/base/toast'
|
|
|
|
import SetURL from './steps/setURL'
|
|
|
|
import SetURL from './steps/setURL'
|
|
|
|
import SelectPackage from './steps/selectPackage'
|
|
|
|
import SelectPackage from './steps/selectPackage'
|
|
|
|
import Installed from './steps/installed'
|
|
|
|
import Installed from '../base/installed'
|
|
|
|
import Loaded from './steps/loaded'
|
|
|
|
import Loaded from './steps/loaded'
|
|
|
|
import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-icon'
|
|
|
|
import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-icon'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { usePluginPageContext } from '../../plugin-page/context'
|
|
|
|
import { usePluginPageContext } from '../../plugin-page/context'
|
|
|
|
import { installPackageFromGitHub } from '@/service/plugins'
|
|
|
|
|
|
|
|
|
|
|
|
const i18nPrefix = 'plugin.installModal'
|
|
|
|
|
|
|
|
|
|
|
|
type InstallFromGitHubProps = {
|
|
|
|
type InstallFromGitHubProps = {
|
|
|
|
updatePayload?: UpdateFromGitHubPayload
|
|
|
|
updatePayload?: UpdateFromGitHubPayload
|
|
|
|
@ -30,17 +29,15 @@ const InstallFromGitHub: React.FC<InstallFromGitHubProps> = ({ updatePayload, on
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const [state, setState] = useState<InstallState>({
|
|
|
|
const [state, setState] = useState<InstallState>({
|
|
|
|
step: updatePayload ? InstallStepFromGitHub.selectPackage : InstallStepFromGitHub.setUrl,
|
|
|
|
step: updatePayload ? InstallStepFromGitHub.selectPackage : InstallStepFromGitHub.setUrl,
|
|
|
|
repoUrl: updatePayload?.url || '',
|
|
|
|
repoUrl: updatePayload?.originalPackageInfo.repo || '',
|
|
|
|
selectedVersion: updatePayload?.currVersion || '',
|
|
|
|
selectedVersion: updatePayload?.originalPackageInfo.version || '',
|
|
|
|
selectedPackage: updatePayload?.currPackage || '',
|
|
|
|
selectedPackage: updatePayload?.originalPackageInfo.package || '',
|
|
|
|
releases: [],
|
|
|
|
releases: [],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
const { getIconUrl } = useGetIcon()
|
|
|
|
const { getIconUrl } = useGetIcon()
|
|
|
|
const [uniqueIdentifier, setUniqueIdentifier] = useState<string | null>(null)
|
|
|
|
const [uniqueIdentifier, setUniqueIdentifier] = useState<string | null>(null)
|
|
|
|
const [manifest, setManifest] = useState<PluginDeclaration | null>(null)
|
|
|
|
const [manifest, setManifest] = useState<PluginDeclaration | null>(null)
|
|
|
|
const [errorMsg, setErrorMsg] = useState<string | null>(null)
|
|
|
|
const [errorMsg, setErrorMsg] = useState<string | null>(null)
|
|
|
|
const setPluginTasksWithPolling = usePluginTasksStore(s => s.setPluginTasksWithPolling)
|
|
|
|
|
|
|
|
const { check } = checkTaskStatus()
|
|
|
|
|
|
|
|
const mutateInstalledPluginList = usePluginPageContext(v => v.mutateInstalledPluginList)
|
|
|
|
const mutateInstalledPluginList = usePluginPageContext(v => v.mutateInstalledPluginList)
|
|
|
|
|
|
|
|
|
|
|
|
const versions: Item[] = state.releases.map(release => ({
|
|
|
|
const versions: Item[] = state.releases.map(release => ({
|
|
|
|
@ -58,13 +55,39 @@ const InstallFromGitHub: React.FC<InstallFromGitHubProps> = ({ updatePayload, on
|
|
|
|
})) || [])
|
|
|
|
})) || [])
|
|
|
|
: []
|
|
|
|
: []
|
|
|
|
|
|
|
|
|
|
|
|
const { isLoading, handleUpload, error } = useGitHubUpload()
|
|
|
|
|
|
|
|
const { fetchReleases } = useGitHubReleases()
|
|
|
|
const { fetchReleases } = useGitHubReleases()
|
|
|
|
|
|
|
|
|
|
|
|
const handleError = (e: any) => {
|
|
|
|
const getTitle = useCallback(() => {
|
|
|
|
const message = e?.response?.message || t('plugin.error.installFailed')
|
|
|
|
if (state.step === InstallStepFromGitHub.installed)
|
|
|
|
|
|
|
|
return t(`${i18nPrefix}.installedSuccessfully`)
|
|
|
|
|
|
|
|
if (state.step === InstallStepFromGitHub.installFailed)
|
|
|
|
|
|
|
|
return t(`${i18nPrefix}.installFailed`)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return t(`${i18nPrefix}.installPlugin`)
|
|
|
|
|
|
|
|
}, [state.step])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleUrlSubmit = async () => {
|
|
|
|
|
|
|
|
const { isValid, owner, repo } = parseGitHubUrl(state.repoUrl)
|
|
|
|
|
|
|
|
if (!isValid || !owner || !repo) {
|
|
|
|
|
|
|
|
Toast.notify({
|
|
|
|
|
|
|
|
type: 'error',
|
|
|
|
|
|
|
|
message: t('plugin.error.inValidGitHubUrl'),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await fetchReleases(owner, repo).then((fetchedReleases) => {
|
|
|
|
|
|
|
|
setState(prevState => ({
|
|
|
|
|
|
|
|
...prevState,
|
|
|
|
|
|
|
|
releases: fetchedReleases,
|
|
|
|
|
|
|
|
step: InstallStepFromGitHub.selectPackage,
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleError = (e: any, isInstall: boolean) => {
|
|
|
|
|
|
|
|
const message = e?.response?.message || t('plugin.installModal.installFailedDesc')
|
|
|
|
setErrorMsg(message)
|
|
|
|
setErrorMsg(message)
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.failed }))
|
|
|
|
setState(prevState => ({ ...prevState, step: isInstall ? InstallStepFromGitHub.installFailed : InstallStepFromGitHub.uploadFailed }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleUploaded = async (GitHubPackage: any) => {
|
|
|
|
const handleUploaded = async (GitHubPackage: any) => {
|
|
|
|
@ -78,72 +101,25 @@ const InstallFromGitHub: React.FC<InstallFromGitHubProps> = ({ updatePayload, on
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.readyToInstall }))
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.readyToInstall }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
catch (e) {
|
|
|
|
handleError(e)
|
|
|
|
handleError(e, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleInstall = async () => {
|
|
|
|
const handleUploadFail = useCallback((errorMsg: string) => {
|
|
|
|
try {
|
|
|
|
setErrorMsg(errorMsg)
|
|
|
|
const { all_installed: isInstalled, task_id: taskId } = await installPackageFromGitHub(uniqueIdentifier!)
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.uploadFailed }))
|
|
|
|
|
|
|
|
}, [])
|
|
|
|
if (isInstalled) {
|
|
|
|
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.installed }))
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setPluginTasksWithPolling()
|
|
|
|
|
|
|
|
await check({
|
|
|
|
|
|
|
|
taskId,
|
|
|
|
|
|
|
|
pluginUniqueIdentifier: uniqueIdentifier!,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.installed }))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (e) {
|
|
|
|
|
|
|
|
handleError(e)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleInstalled = useCallback(() => {
|
|
|
|
const handleInstalled = useCallback(() => {
|
|
|
|
mutateInstalledPluginList()
|
|
|
|
mutateInstalledPluginList()
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.installed }))
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.installed }))
|
|
|
|
}, [mutateInstalledPluginList])
|
|
|
|
}, [mutateInstalledPluginList])
|
|
|
|
|
|
|
|
|
|
|
|
const handleNext = async () => {
|
|
|
|
const handleFailed = useCallback((errorMsg?: string) => {
|
|
|
|
switch (state.step) {
|
|
|
|
setState(prevState => ({ ...prevState, step: InstallStepFromGitHub.installFailed }))
|
|
|
|
case InstallStepFromGitHub.setUrl: {
|
|
|
|
if (errorMsg)
|
|
|
|
const { isValid, owner, repo } = parseGitHubUrl(state.repoUrl)
|
|
|
|
setErrorMsg(errorMsg)
|
|
|
|
if (!isValid || !owner || !repo) {
|
|
|
|
}, [])
|
|
|
|
Toast.notify({
|
|
|
|
|
|
|
|
type: 'error',
|
|
|
|
|
|
|
|
message: t('plugin.error.inValidGitHubUrl'),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const fetchedReleases = await fetchReleases(owner, repo)
|
|
|
|
|
|
|
|
setState(prevState => ({
|
|
|
|
|
|
|
|
...prevState,
|
|
|
|
|
|
|
|
releases: fetchedReleases,
|
|
|
|
|
|
|
|
step: InstallStepFromGitHub.selectPackage,
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case InstallStepFromGitHub.selectPackage: {
|
|
|
|
|
|
|
|
const repo = state.repoUrl.replace('https://github.com/', '')
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
|
|
|
|
handleError(error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
await handleUpload(repo, state.selectedVersion, state.selectedPackage, handleUploaded)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case InstallStepFromGitHub.readyToInstall:
|
|
|
|
|
|
|
|
await handleInstall()
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case InstallStepFromGitHub.installed:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleBack = () => {
|
|
|
|
const handleBack = () => {
|
|
|
|
setState((prevState) => {
|
|
|
|
setState((prevState) => {
|
|
|
|
@ -157,63 +133,69 @@ const InstallFromGitHub: React.FC<InstallFromGitHubProps> = ({ updatePayload, on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
<Modal
|
|
|
|
isShow={true}
|
|
|
|
isShow={true}
|
|
|
|
onClose={onClose}
|
|
|
|
onClose={onClose}
|
|
|
|
className='flex min-w-[480px] p-0 flex-col items-start rounded-2xl border-[0.5px]
|
|
|
|
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'
|
|
|
|
border-components-panel-border bg-components-panel-bg shadows-shadow-xl'
|
|
|
|
closable
|
|
|
|
closable
|
|
|
|
>
|
|
|
|
>
|
|
|
|
<div className='flex pt-6 pl-6 pb-3 pr-14 items-start gap-2 self-stretch'>
|
|
|
|
<div className='flex pt-6 pl-6 pb-3 pr-14 items-start gap-2 self-stretch'>
|
|
|
|
<div className='flex flex-col items-start gap-1 flex-grow'>
|
|
|
|
<div className='flex flex-col items-start gap-1 flex-grow'>
|
|
|
|
<div className='self-stretch text-text-primary title-2xl-semi-bold'>
|
|
|
|
<div className='self-stretch text-text-primary title-2xl-semi-bold'>
|
|
|
|
{t('plugin.installFromGitHub.installPlugin')}
|
|
|
|
{getTitle()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className='self-stretch text-text-tertiary system-xs-regular'>
|
|
|
|
<div className='self-stretch text-text-tertiary system-xs-regular'>
|
|
|
|
{state.step !== InstallStepFromGitHub.installed && t('plugin.installFromGitHub.installNote')}
|
|
|
|
{!([InstallStepFromGitHub.uploadFailed, InstallStepFromGitHub.installed, InstallStepFromGitHub.installFailed].includes(state.step)) && t('plugin.installFromGitHub.installNote')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={`flex px-6 py-3 flex-col justify-center items-start self-stretch ${state.step === InstallStepFromGitHub.installed ? 'gap-2' : 'gap-4'}`}>
|
|
|
|
{([InstallStepFromGitHub.uploadFailed, InstallStepFromGitHub.installed, InstallStepFromGitHub.installFailed].includes(state.step))
|
|
|
|
|
|
|
|
? <Installed
|
|
|
|
|
|
|
|
payload={manifest}
|
|
|
|
|
|
|
|
isFailed={[InstallStepFromGitHub.uploadFailed, InstallStepFromGitHub.installFailed].includes(state.step)}
|
|
|
|
|
|
|
|
errMsg={errorMsg}
|
|
|
|
|
|
|
|
onCancel={onClose}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
: <div className={`flex px-6 py-3 flex-col justify-center items-start self-stretch ${state.step === InstallStepFromGitHub.installed ? 'gap-2' : 'gap-4'}`}>
|
|
|
|
{state.step === InstallStepFromGitHub.setUrl && (
|
|
|
|
{state.step === InstallStepFromGitHub.setUrl && (
|
|
|
|
<SetURL
|
|
|
|
<SetURL
|
|
|
|
repoUrl={state.repoUrl}
|
|
|
|
repoUrl={state.repoUrl}
|
|
|
|
onChange={value => setState(prevState => ({ ...prevState, repoUrl: value }))}
|
|
|
|
onChange={value => setState(prevState => ({ ...prevState, repoUrl: value }))}
|
|
|
|
onNext={handleNext}
|
|
|
|
onNext={handleUrlSubmit}
|
|
|
|
onCancel={onClose}
|
|
|
|
onCancel={onClose}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
)}
|
|
|
|
{state.step === InstallStepFromGitHub.selectPackage && (
|
|
|
|
{state.step === InstallStepFromGitHub.selectPackage && (
|
|
|
|
<SelectPackage
|
|
|
|
<SelectPackage
|
|
|
|
updatePayload={updatePayload!}
|
|
|
|
updatePayload={updatePayload!}
|
|
|
|
|
|
|
|
repoUrl={state.repoUrl}
|
|
|
|
selectedVersion={state.selectedVersion}
|
|
|
|
selectedVersion={state.selectedVersion}
|
|
|
|
versions={versions}
|
|
|
|
versions={versions}
|
|
|
|
onSelectVersion={item => setState(prevState => ({ ...prevState, selectedVersion: item.value as string }))}
|
|
|
|
onSelectVersion={item => setState(prevState => ({ ...prevState, selectedVersion: item.value as string }))}
|
|
|
|
selectedPackage={state.selectedPackage}
|
|
|
|
selectedPackage={state.selectedPackage}
|
|
|
|
packages={packages}
|
|
|
|
packages={packages}
|
|
|
|
onSelectPackage={item => setState(prevState => ({ ...prevState, selectedPackage: item.value as string }))}
|
|
|
|
onSelectPackage={item => setState(prevState => ({ ...prevState, selectedPackage: item.value as string }))}
|
|
|
|
onNext={handleNext}
|
|
|
|
onUploaded={handleUploaded}
|
|
|
|
|
|
|
|
onFailed={handleUploadFail}
|
|
|
|
onBack={handleBack}
|
|
|
|
onBack={handleBack}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
)}
|
|
|
|
{state.step === InstallStepFromGitHub.readyToInstall && (
|
|
|
|
{state.step === InstallStepFromGitHub.readyToInstall && (
|
|
|
|
<Loaded
|
|
|
|
<Loaded
|
|
|
|
isLoading={isLoading}
|
|
|
|
uniqueIdentifier={uniqueIdentifier!}
|
|
|
|
payload={manifest as any}
|
|
|
|
payload={manifest as any}
|
|
|
|
onBack={handleBack}
|
|
|
|
|
|
|
|
onInstall={handleNext}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
{state.step === InstallStepFromGitHub.installed && (
|
|
|
|
|
|
|
|
<Installed
|
|
|
|
|
|
|
|
repoUrl={state.repoUrl}
|
|
|
|
repoUrl={state.repoUrl}
|
|
|
|
selectedVersion={state.selectedVersion}
|
|
|
|
selectedVersion={state.selectedVersion}
|
|
|
|
selectedPackage={state.selectedPackage}
|
|
|
|
selectedPackage={state.selectedPackage}
|
|
|
|
onClose={onClose}
|
|
|
|
onBack={handleBack}
|
|
|
|
|
|
|
|
onInstalled={handleInstalled}
|
|
|
|
|
|
|
|
onFailed={handleFailed}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>}
|
|
|
|
</Modal>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|