2024-11-05 15:11:44 +08:00
|
|
|
'use client'
|
|
|
|
import type { FC } from 'react'
|
2024-11-05 16:04:52 +08:00
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
2024-11-05 15:11:44 +08:00
|
|
|
import { RiInformation2Line } from '@remixicon/react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import Card from '@/app/components/plugins/card'
|
|
|
|
import Modal from '@/app/components/base/modal'
|
|
|
|
import Button from '@/app/components/base/button'
|
|
|
|
import Badge, { BadgeState } from '@/app/components/base/badge/index'
|
|
|
|
import type { UpdateFromMarketPlacePayload } from '../types'
|
2024-11-05 16:04:52 +08:00
|
|
|
import { pluginManifestToCardPluginProps } from '@/app/components/plugins/install-plugin/utils'
|
|
|
|
import useGetIcon from '../install-plugin/base/use-get-icon'
|
2024-11-05 16:47:09 +08:00
|
|
|
import { updateFromMarketPlace } from '@/service/plugins'
|
|
|
|
import checkTaskStatus from '@/app/components/plugins/install-plugin/base/check-task-status'
|
|
|
|
import { usePluginTasksStore } from '@/app/components/plugins/plugin-page/store'
|
2024-11-05 15:11:44 +08:00
|
|
|
|
|
|
|
const i18nPrefix = 'plugin.upgrade'
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
payload: UpdateFromMarketPlacePayload
|
|
|
|
onSave: () => void
|
|
|
|
onCancel: () => void
|
|
|
|
}
|
|
|
|
|
|
|
|
enum UploadStep {
|
|
|
|
notStarted = 'notStarted',
|
|
|
|
upgrading = 'upgrading',
|
|
|
|
installed = 'installed',
|
|
|
|
}
|
|
|
|
|
|
|
|
const UpdatePluginModal: FC<Props> = ({
|
2024-11-05 16:04:52 +08:00
|
|
|
payload,
|
2024-11-05 15:11:44 +08:00
|
|
|
onSave,
|
|
|
|
onCancel,
|
|
|
|
}) => {
|
2024-11-05 16:04:52 +08:00
|
|
|
const {
|
|
|
|
originalPackageInfo,
|
|
|
|
targetPackageInfo,
|
|
|
|
} = payload
|
2024-11-05 15:11:44 +08:00
|
|
|
const { t } = useTranslation()
|
2024-11-05 16:04:52 +08:00
|
|
|
const { getIconUrl } = useGetIcon()
|
|
|
|
const [icon, setIcon] = useState<string>(originalPackageInfo.payload.icon)
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
const icon = await getIconUrl(originalPackageInfo.payload.icon)
|
|
|
|
setIcon(icon)
|
|
|
|
})()
|
|
|
|
}, [originalPackageInfo, getIconUrl])
|
2024-11-05 16:47:09 +08:00
|
|
|
const {
|
|
|
|
check,
|
|
|
|
stop,
|
|
|
|
} = checkTaskStatus()
|
|
|
|
const handleCancel = () => {
|
|
|
|
stop()
|
|
|
|
onCancel()
|
|
|
|
}
|
|
|
|
|
2024-11-05 15:11:44 +08:00
|
|
|
const [uploadStep, setUploadStep] = useState<UploadStep>(UploadStep.notStarted)
|
2024-11-05 16:47:09 +08:00
|
|
|
const setPluginTasksWithPolling = usePluginTasksStore(s => s.setPluginTasksWithPolling)
|
|
|
|
|
2024-11-05 15:11:44 +08:00
|
|
|
const configBtnText = useMemo(() => {
|
|
|
|
return ({
|
|
|
|
[UploadStep.notStarted]: t(`${i18nPrefix}.upgrade`),
|
|
|
|
[UploadStep.upgrading]: t(`${i18nPrefix}.upgrading`),
|
|
|
|
[UploadStep.installed]: t(`${i18nPrefix}.close`),
|
|
|
|
})[uploadStep]
|
|
|
|
}, [t, uploadStep])
|
|
|
|
|
2024-11-05 16:47:09 +08:00
|
|
|
const handleConfirm = useCallback(async () => {
|
2024-11-05 15:11:44 +08:00
|
|
|
if (uploadStep === UploadStep.notStarted) {
|
|
|
|
setUploadStep(UploadStep.upgrading)
|
2024-11-05 16:47:09 +08:00
|
|
|
const {
|
|
|
|
all_installed: isInstalled,
|
|
|
|
task_id: taskId,
|
|
|
|
} = await updateFromMarketPlace({
|
|
|
|
original_plugin_unique_identifier: originalPackageInfo.id,
|
|
|
|
new_plugin_unique_identifier: targetPackageInfo.id,
|
|
|
|
})
|
|
|
|
if (isInstalled) {
|
|
|
|
onSave()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setPluginTasksWithPolling()
|
|
|
|
await check({
|
|
|
|
taskId,
|
|
|
|
pluginUniqueIdentifier: targetPackageInfo.id,
|
|
|
|
})
|
|
|
|
onSave()
|
2024-11-05 15:11:44 +08:00
|
|
|
}
|
|
|
|
if (uploadStep === UploadStep.installed) {
|
|
|
|
onSave()
|
|
|
|
onCancel()
|
|
|
|
}
|
2024-11-05 16:47:09 +08:00
|
|
|
}, [onCancel, onSave, uploadStep, check, originalPackageInfo.id, setPluginTasksWithPolling, targetPackageInfo.id])
|
|
|
|
const usedInAppInfo = useMemo(() => {
|
|
|
|
return (
|
|
|
|
<div className='flex px-0.5 justify-center items-center gap-0.5'>
|
|
|
|
<div className='text-text-warning system-xs-medium'>{t(`${i18nPrefix}.usedInApps`, { num: 3 })}</div>
|
|
|
|
{/* show the used apps */}
|
|
|
|
<RiInformation2Line className='w-4 h-4 text-text-tertiary' />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}, [t])
|
2024-11-05 15:11:44 +08:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
isShow={true}
|
|
|
|
onClose={onCancel}
|
|
|
|
className='min-w-[560px]'
|
|
|
|
closable
|
|
|
|
title={t(`${i18nPrefix}.${uploadStep === UploadStep.installed ? 'successfulTitle' : 'title'}`)}
|
|
|
|
>
|
|
|
|
<div className='mt-3 mb-2 text-text-secondary system-md-regular'>
|
|
|
|
{t(`${i18nPrefix}.description`)}
|
|
|
|
</div>
|
|
|
|
<div className='flex p-2 items-start content-start gap-1 self-stretch flex-wrap rounded-2xl bg-background-section-burn'>
|
|
|
|
<Card
|
|
|
|
installed={uploadStep === UploadStep.installed}
|
2024-11-05 16:04:52 +08:00
|
|
|
payload={pluginManifestToCardPluginProps({
|
|
|
|
...originalPackageInfo.payload,
|
|
|
|
icon: icon!,
|
|
|
|
})}
|
2024-11-05 15:11:44 +08:00
|
|
|
className='w-full'
|
|
|
|
titleLeft={
|
|
|
|
<>
|
|
|
|
<Badge className='mx-1' size="s" state={BadgeState.Warning}>
|
2024-11-05 16:04:52 +08:00
|
|
|
{`${originalPackageInfo.payload.version} -> ${targetPackageInfo.version}`}
|
2024-11-05 15:11:44 +08:00
|
|
|
</Badge>
|
2024-11-05 16:47:09 +08:00
|
|
|
{false && usedInAppInfo}
|
2024-11-05 15:11:44 +08:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className='flex pt-5 justify-end items-center gap-2 self-stretch'>
|
|
|
|
{uploadStep === UploadStep.notStarted && (
|
|
|
|
<Button
|
2024-11-05 16:47:09 +08:00
|
|
|
onClick={handleCancel}
|
2024-11-05 15:11:44 +08:00
|
|
|
>
|
|
|
|
{t('common.operation.cancel')}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
<Button
|
|
|
|
variant='primary'
|
|
|
|
loading={uploadStep === UploadStep.upgrading}
|
|
|
|
onClick={handleConfirm}
|
|
|
|
disabled={uploadStep === UploadStep.upgrading}
|
|
|
|
>
|
|
|
|
{configBtnText}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
export default React.memo(UpdatePluginModal)
|