feat: support upload pkg

pull/9940/head
Joel 2 years ago
parent d7def41acc
commit 606fc7be0c

@ -6,31 +6,37 @@ import Card from '../../card'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import { pluginManifestToCardPluginProps } from '../utils' import { pluginManifestToCardPluginProps } from '../utils'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import Badge, { BadgeState } from '@/app/components/base/badge/index'
type Props = { type Props = {
payload: PluginDeclaration payload?: PluginDeclaration | null
isFailed: boolean isFailed: boolean
errMsg?: string | null
onCancel: () => void onCancel: () => void
} }
const Installed: FC<Props> = ({ const Installed: FC<Props> = ({
payload, payload,
isFailed, isFailed,
errMsg,
onCancel, onCancel,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
return ( return (
<> <>
<div className='flex flex-col px-6 py-3 justify-center items-start gap-4 self-stretch'> <div className='flex flex-col px-6 py-3 justify-center items-start gap-4 self-stretch'>
<p className='text-text-secondary system-md-regular'>{t(`plugin.installModal.${isFailed ? 'installFailedDesc' : 'installedSuccessfullyDesc'}`)}</p> <p className='text-text-secondary system-md-regular'>{(isFailed && errMsg) ? errMsg : t(`plugin.installModal.${isFailed ? 'installFailedDesc' : 'installedSuccessfullyDesc'}`)}</p>
<div className='flex p-2 items-start content-start gap-1 self-stretch flex-wrap rounded-2xl bg-background-section-burn'> {payload && (
<Card <div className='flex p-2 items-start content-start gap-1 self-stretch flex-wrap rounded-2xl bg-background-section-burn'>
className='w-full' <Card
payload={pluginManifestToCardPluginProps(payload)} className='w-full'
installed={!isFailed} payload={pluginManifestToCardPluginProps(payload)}
installFailed={isFailed} installed={!isFailed}
/> installFailed={isFailed}
</div> titleLeft={<Badge className='mx-1' size="s" state={BadgeState.Default}>{payload.version}</Badge>}
/>
</div>
)}
</div> </div>
{/* Action Buttons */} {/* Action Buttons */}
<div className='flex p-6 pt-5 justify-end items-center gap-2 self-stretch'> <div className='flex p-6 pt-5 justify-end items-center gap-2 self-stretch'>

@ -8,7 +8,6 @@ import Uploading from './steps/uploading'
import Install from './steps/install' import Install from './steps/install'
import Installed from '../base/installed' import Installed from '../base/installed'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { toolNotionManifest } from '../../card/card-mock'
const i18nPrefix = 'plugin.installModal' const i18nPrefix = 'plugin.installModal'
@ -23,19 +22,21 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
onClose, onClose,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
// uploading -> readyToInstall -> installed/failed // uploading -> !uploadFailed -> readyToInstall -> installed/failed
const [step, setStep] = useState<InstallStep>(InstallStep.uploading) const [step, setStep] = useState<InstallStep>(InstallStep.uploading)
const [uniqueIdentifier, setUniqueIdentifier] = useState<string | null>(null) const [uniqueIdentifier, setUniqueIdentifier] = useState<string | null>(null)
const [manifest, setManifest] = useState<PluginDeclaration | null>(null)
const [errorMsg, setErrorMsg] = useState<string | null>(null)
const getTitle = useCallback(() => { const getTitle = useCallback(() => {
if (step === InstallStep.uploadFailed)
return t(`${i18nPrefix}.uploadFailed`)
if (step === InstallStep.installed) if (step === InstallStep.installed)
return t(`${i18nPrefix}.installedSuccessfully`) return t(`${i18nPrefix}.installedSuccessfully`)
if (step === InstallStep.installFailed) if (step === InstallStep.installFailed)
return t(`${i18nPrefix}.installFailed`) return t(`${i18nPrefix}.installFailed`)
return t(`${i18nPrefix}.installPlugin`) return t(`${i18nPrefix}.installPlugin`)
}, [step]) }, [step])
const [manifest, setManifest] = useState<PluginDeclaration | null>(toolNotionManifest)
const handleUploaded = useCallback((result: { const handleUploaded = useCallback((result: {
uniqueIdentifier: string uniqueIdentifier: string
@ -46,6 +47,11 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
setStep(InstallStep.readyToInstall) setStep(InstallStep.readyToInstall)
}, []) }, [])
const handleUploadFail = useCallback((errorMsg: string) => {
setErrorMsg(errorMsg)
setStep(InstallStep.uploadFailed)
}, [])
const handleInstalled = useCallback(async () => { const handleInstalled = useCallback(async () => {
setStep(InstallStep.installed) setStep(InstallStep.installed)
}, []) }, [])
@ -71,6 +77,7 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
file={file} file={file}
onCancel={onClose} onCancel={onClose}
onUploaded={handleUploaded} onUploaded={handleUploaded}
onFailed={handleUploadFail}
/> />
)} )}
{ {
@ -84,10 +91,11 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
) )
} }
{ {
([InstallStep.installed, InstallStep.installFailed].includes(step)) && ( ([InstallStep.uploadFailed, InstallStep.installed, InstallStep.installFailed].includes(step)) && (
<Installed <Installed
payload={manifest!} payload={manifest}
isFailed={step === InstallStep.installFailed} isFailed={[InstallStep.uploadFailed, InstallStep.installFailed].includes(step)}
errMsg={errorMsg}
onCancel={onClose} onCancel={onClose}
/> />
) )

@ -8,6 +8,7 @@ import Button from '@/app/components/base/button'
import { sleep } from '@/utils' import { sleep } from '@/utils'
import { Trans, useTranslation } from 'react-i18next' import { Trans, useTranslation } from 'react-i18next'
import { RiLoader2Line } from '@remixicon/react' import { RiLoader2Line } from '@remixicon/react'
import Badge, { BadgeState } from '@/app/components/base/badge/index'
const i18nPrefix = 'plugin.installModal' const i18nPrefix = 'plugin.installModal'
@ -51,6 +52,7 @@ const Installed: FC<Props> = ({
<Card <Card
className='w-full' className='w-full'
payload={pluginManifestToCardPluginProps(payload)} payload={pluginManifestToCardPluginProps(payload)}
titleLeft={<Badge className='mx-1' size="s" state={BadgeState.Default}>{payload.version}</Badge>}
/> />
</div> </div>
</div> </div>

@ -5,10 +5,8 @@ import { RiLoader2Line } from '@remixicon/react'
import Card from '../../../card' import Card from '../../../card'
import type { PluginDeclaration } from '../../../types' import type { PluginDeclaration } from '../../../types'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import { sleep } from '@/utils'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { toolNotionManifest } from '../../../card/card-mock' import { uploadPackageFile } from '@/service/plugins'
const i18nPrefix = 'plugin.installModal' const i18nPrefix = 'plugin.installModal'
type Props = { type Props = {
@ -18,21 +16,30 @@ type Props = {
uniqueIdentifier: string uniqueIdentifier: string
manifest: PluginDeclaration manifest: PluginDeclaration
}) => void }) => void
onFailed: (errorMsg: string) => void
} }
const Uploading: FC<Props> = ({ const Uploading: FC<Props> = ({
file, file,
onCancel, onCancel,
onUploaded, onUploaded,
onFailed,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const fileName = file.name const fileName = file.name
const handleUpload = async () => { const handleUpload = async () => {
await sleep(3000) try {
onUploaded({ const res = await uploadPackageFile(file)
uniqueIdentifier: 'yeuoly/neko:0.0.1@5395654da2c0b919b3d9b946a1a0545b737004380765e5f3b8c49976d3276c87', onUploaded(res)
manifest: toolNotionManifest, }
}) catch (e: any) {
if (e.response?.message) {
onFailed(e.response?.message)
}
else { // Why it would into this branch?
onUploaded(e.response)
}
}
} }
React.useEffect(() => { React.useEffect(() => {

@ -168,6 +168,7 @@ export type UpdateEndpointRequest = {
export enum InstallStep { export enum InstallStep {
uploading = 'uploading', uploading = 'uploading',
uploadFailed = 'uploadFailed',
readyToInstall = 'readyToInstall', readyToInstall = 'readyToInstall',
installing = 'installing', installing = 'installing',
installed = 'installed', installed = 'installed',

@ -63,6 +63,7 @@ const translation = {
installPlugin: 'Install Plugin', installPlugin: 'Install Plugin',
installedSuccessfully: 'Installation successful', installedSuccessfully: 'Installation successful',
installedSuccessfullyDesc: 'The plugin has been installed successfully.', installedSuccessfullyDesc: 'The plugin has been installed successfully.',
uploadFailed: 'Upload failed',
installFailed: 'Installation failed', installFailed: 'Installation failed',
installFailedDesc: 'The plugin has been installed failed.', installFailedDesc: 'The plugin has been installed failed.',
install: 'Install', install: 'Install',

@ -63,6 +63,7 @@ const translation = {
installPlugin: '安装插件', installPlugin: '安装插件',
installedSuccessfully: '安装成功', installedSuccessfully: '安装成功',
installedSuccessfullyDesc: '插件已成功安装。', installedSuccessfullyDesc: '插件已成功安装。',
uploadFailed: '上传失败',
installFailed: '安装失败', installFailed: '安装失败',
installFailedDesc: '插件安装失败。', installFailedDesc: '插件安装失败。',
install: '安装', install: '安装',

@ -1,5 +1,5 @@
import type { Fetcher } from 'swr' import type { Fetcher } from 'swr'
import { del, get, post } from './base' import { del, get, post, upload } from './base'
import type { import type {
CreateEndpointRequest, CreateEndpointRequest,
EndpointOperationResponse, EndpointOperationResponse,
@ -49,3 +49,12 @@ export const installPackageFromGitHub: Fetcher<InstallPackageResponse, { repo: s
export const fetchDebugKey = async () => { export const fetchDebugKey = async () => {
return get<DebugInfoTypes>('/workspaces/current/plugin/debugging-key') return get<DebugInfoTypes>('/workspaces/current/plugin/debugging-key')
} }
export const uploadPackageFile = async (file: File) => {
const formData = new FormData()
formData.append('pkg', file)
return upload({
xhr: new XMLHttpRequest(),
data: formData,
}, false, '/workspaces/current/plugin/upload/pkg')
}

Loading…
Cancel
Save