feat: support local installed package

This commit is contained in:
Joel 2024-11-19 16:04:17 +08:00
parent 43d7a538dc
commit 2e3442f74c
8 changed files with 113 additions and 33 deletions

View File

@ -56,7 +56,7 @@ const InstallBundle: FC<Props> = ({
<ReadyToInstall <ReadyToInstall
step={step} step={step}
onStepChange={setStep} onStepChange={setStep}
dependencies={fromDSLPayload} allPlugins={fromDSLPayload}
onClose={onClose} onClose={onClose}
/> />
</Modal> </Modal>

View File

@ -1,7 +1,7 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useEffect } from 'react' import React, { useEffect } from 'react'
import type { Dependency, Plugin } from '../../../types' import type { GitHubItemAndMarketPlaceDependency, Plugin } from '../../../types'
import { pluginManifestToCardPluginProps } from '../../utils' import { pluginManifestToCardPluginProps } from '../../utils'
import { useUploadGitHub } from '@/service/use-plugins' import { useUploadGitHub } from '@/service/use-plugins'
import Loading from './loading' import Loading from './loading'
@ -10,8 +10,9 @@ import LoadedItem from './loaded-item'
type Props = { type Props = {
checked: boolean checked: boolean
onCheckedChange: (plugin: Plugin) => void onCheckedChange: (plugin: Plugin) => void
dependency: Dependency dependency: GitHubItemAndMarketPlaceDependency
onFetchedPayload: (payload: Plugin) => void onFetchedPayload: (payload: Plugin) => void
onFetchError: () => void
} }
const Item: FC<Props> = ({ const Item: FC<Props> = ({
@ -19,9 +20,10 @@ const Item: FC<Props> = ({
onCheckedChange, onCheckedChange,
dependency, dependency,
onFetchedPayload, onFetchedPayload,
onFetchError,
}) => { }) => {
const info = dependency.value const info = dependency.value
const { data } = useUploadGitHub({ const { data, error } = useUploadGitHub({
repo: info.repo!, repo: info.repo!,
version: info.version!, version: info.version!,
package: info.package!, package: info.package!,
@ -38,6 +40,12 @@ const Item: FC<Props> = ({
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [data]) }, [data])
useEffect(() => {
if (error)
onFetchError()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [error])
if (!payload) return <Loading /> if (!payload) return <Loading />
return ( return (
<LoadedItem <LoadedItem

View File

@ -0,0 +1,30 @@
'use client'
import type { FC } from 'react'
import React from 'react'
import type { Plugin } from '../../../types'
import type { PackageDependency } from '../../../types'
import { pluginManifestToCardPluginProps } from '../../utils'
import LoadedItem from './loaded-item'
type Props = {
checked: boolean
onCheckedChange: (plugin: Plugin) => void
payload: PackageDependency
}
const PackageItem: FC<Props> = ({
payload,
checked,
onCheckedChange,
}) => {
const plugin = pluginManifestToCardPluginProps(payload.value.manifest)
return (
<LoadedItem
payload={plugin}
checked={checked}
onCheckedChange={onCheckedChange}
/>
)
}
export default React.memo(PackageItem)

View File

@ -9,14 +9,14 @@ import type { Dependency, InstallStatusResponse, Plugin } from '../../types'
type Props = { type Props = {
step: InstallStep step: InstallStep
onStepChange: (step: InstallStep) => void, onStepChange: (step: InstallStep) => void,
dependencies: Dependency[] allPlugins: Dependency[]
onClose: () => void onClose: () => void
} }
const ReadyToInstall: FC<Props> = ({ const ReadyToInstall: FC<Props> = ({
step, step,
onStepChange, onStepChange,
dependencies, allPlugins,
onClose, onClose,
}) => { }) => {
const [installedPlugins, setInstalledPlugins] = useState<Plugin[]>([]) const [installedPlugins, setInstalledPlugins] = useState<Plugin[]>([])
@ -30,7 +30,7 @@ const ReadyToInstall: FC<Props> = ({
<> <>
{step === InstallStep.readyToInstall && ( {step === InstallStep.readyToInstall && (
<Install <Install
fromDSLPayload={dependencies} allPlugins={allPlugins}
onCancel={onClose} onCancel={onClose}
onInstalled={handleInstalled} onInstalled={handleInstalled}
/> />

View File

@ -1,30 +1,34 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useCallback, useEffect, useMemo } from 'react' import React, { useCallback, useEffect, useMemo, useState } from 'react'
import type { Dependency, Plugin } from '../../../types' import type { Dependency, GitHubItemAndMarketPlaceDependency, PackageDependency, Plugin } from '../../../types'
import MarketplaceItem from '../item/marketplace-item' import MarketplaceItem from '../item/marketplace-item'
import GithubItem from '../item/github-item' import GithubItem from '../item/github-item'
import { useFetchPluginsInMarketPlaceByIds } from '@/service/use-plugins' import { useFetchPluginsInMarketPlaceByIds } from '@/service/use-plugins'
import produce from 'immer' import produce from 'immer'
import { useGetState } from 'ahooks' import { useGetState } from 'ahooks'
import PackageItem from '../item/package-item'
type Props = { type Props = {
fromDSLPayload: Dependency[] allPlugins: Dependency[]
selectedPlugins: Plugin[] selectedPlugins: Plugin[]
onSelect: (plugin: Plugin, selectedIndex: number) => void onSelect: (plugin: Plugin, selectedIndex: number) => void
onLoadedAllPlugin: () => void onLoadedAllPlugin: () => void
} }
const InstallByDSLList: FC<Props> = ({ const InstallByDSLList: FC<Props> = ({
fromDSLPayload, allPlugins,
selectedPlugins, selectedPlugins,
onSelect, onSelect,
onLoadedAllPlugin, onLoadedAllPlugin,
}) => { }) => {
const { isLoading: isFetchingMarketplaceData, data: marketplaceRes } = useFetchPluginsInMarketPlaceByIds(fromDSLPayload.filter(d => d.type === 'marketplace').map(d => d.value.plugin_unique_identifier!)) const { isLoading: isFetchingMarketplaceData, data: marketplaceRes } = useFetchPluginsInMarketPlaceByIds(allPlugins.filter(d => d.type === 'marketplace').map(d => d.value.plugin_unique_identifier!))
const [plugins, setPlugins, getPlugins] = useGetState<Plugin[]>([]) const [plugins, setPlugins, getPlugins] = useGetState<Plugin[]>([])
const handlePlugInFetched = useCallback((index: number) => {
const [errorIndexes, setErrorIndexes] = useState<number[]>([])
const handleGitHubPluginFetched = useCallback((index: number) => {
return (p: Plugin) => { return (p: Plugin) => {
const nextPlugins = produce(getPlugins(), (draft) => { const nextPlugins = produce(getPlugins(), (draft) => {
draft[index] = p draft[index] = p
@ -33,14 +37,20 @@ const InstallByDSLList: FC<Props> = ({
} }
}, [getPlugins, setPlugins]) }, [getPlugins, setPlugins])
const handleGitHubPluginFetchError = useCallback((index: number) => {
return () => {
setErrorIndexes([...errorIndexes, index])
}
}, [errorIndexes])
const marketPlaceInDSLIndex = useMemo(() => { const marketPlaceInDSLIndex = useMemo(() => {
const res: number[] = [] const res: number[] = []
fromDSLPayload.forEach((d, index) => { allPlugins.forEach((d, index) => {
if (d.type === 'marketplace') if (d.type === 'marketplace')
res.push(index) res.push(index)
}) })
return res return res
}, [fromDSLPayload]) }, [allPlugins])
useEffect(() => { useEffect(() => {
if (!isFetchingMarketplaceData && marketplaceRes?.data.plugins && marketplaceRes?.data.plugins.length > 0) { if (!isFetchingMarketplaceData && marketplaceRes?.data.plugins && marketplaceRes?.data.plugins.length > 0) {
@ -57,7 +67,7 @@ const InstallByDSLList: FC<Props> = ({
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [isFetchingMarketplaceData]) }, [isFetchingMarketplaceData])
const isLoadedAllData = fromDSLPayload.length === plugins.length && plugins.every(p => !!p) const isLoadedAllData = allPlugins.length === plugins.length && plugins.every(p => !!p)
useEffect(() => { useEffect(() => {
if (isLoadedAllData) if (isLoadedAllData)
onLoadedAllPlugin() onLoadedAllPlugin()
@ -71,22 +81,44 @@ const InstallByDSLList: FC<Props> = ({
}, [onSelect, plugins]) }, [onSelect, plugins])
return ( return (
<> <>
{fromDSLPayload.map((d, index) => ( {allPlugins.map((d, index) => {
d.type === 'github' if (errorIndexes.includes(index)) {
? <GithubItem return (
<div key={index}>error</div>
)
}
if (d.type === 'github') {
return (<GithubItem
key={index} key={index}
checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)} checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
onCheckedChange={handleSelect(index)} onCheckedChange={handleSelect(index)}
dependency={d} dependency={d as GitHubItemAndMarketPlaceDependency}
onFetchedPayload={handlePlugInFetched(index)} onFetchedPayload={handleGitHubPluginFetched(index)}
/> onFetchError={handleGitHubPluginFetchError(index)}
: <MarketplaceItem />)
}
if (d.type === 'marketplace') {
return (
<MarketplaceItem
key={index}
checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
onCheckedChange={handleSelect(index)}
payload={plugins[index] as Plugin}
/>
)
}
return (
<PackageItem
key={index} key={index}
checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)} checked={!!selectedPlugins.find(p => p.plugin_id === plugins[index]?.plugin_id)}
onCheckedChange={handleSelect(index)} onCheckedChange={handleSelect(index)}
payload={plugins[index] as Plugin} payload={d as PackageDependency}
/> />
))} )
})
}
</> </>
) )
} }

View File

@ -5,19 +5,19 @@ import type { Dependency, InstallStatusResponse, Plugin } from '../../../types'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import { RiLoader2Line } from '@remixicon/react' import { RiLoader2Line } from '@remixicon/react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import InstallByDSLList from './install-by-dsl-list' import InstallByDSLList from './install-multi'
import { useInstallFromMarketplaceAndGitHub } from '@/service/use-plugins' import { useInstallFromMarketplaceAndGitHub } from '@/service/use-plugins'
import { useInvalidateInstalledPluginList } from '@/service/use-plugins' import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
const i18nPrefix = 'plugin.installModal' const i18nPrefix = 'plugin.installModal'
type Props = { type Props = {
fromDSLPayload: Dependency[] allPlugins: Dependency[]
onInstalled: (plugins: Plugin[], installStatus: InstallStatusResponse[]) => void onInstalled: (plugins: Plugin[], installStatus: InstallStatusResponse[]) => void
onCancel: () => void onCancel: () => void
} }
const Install: FC<Props> = ({ const Install: FC<Props> = ({
fromDSLPayload, allPlugins,
onInstalled, onInstalled,
onCancel, onCancel,
}) => { }) => {
@ -49,7 +49,7 @@ const Install: FC<Props> = ({
onInstalled(selectedPlugins, res.map((r, i) => { onInstalled(selectedPlugins, res.map((r, i) => {
return ({ return ({
...r, ...r,
isFromMarketPlace: fromDSLPayload[selectedIndexes[i]].type === 'marketplace', isFromMarketPlace: allPlugins[selectedIndexes[i]].type === 'marketplace',
}) })
})) }))
const hasInstallSuccess = res.some(r => r.success) const hasInstallSuccess = res.some(r => r.success)
@ -58,7 +58,7 @@ const Install: FC<Props> = ({
}, },
}) })
const handleInstall = () => { const handleInstall = () => {
installFromMarketplaceAndGitHub(fromDSLPayload.filter((_d, index) => selectedIndexes.includes(index))) installFromMarketplaceAndGitHub(allPlugins.filter((_d, index) => selectedIndexes.includes(index)))
} }
return ( return (
<> <>
@ -68,7 +68,7 @@ const Install: FC<Props> = ({
</div> </div>
<div className='w-full p-2 rounded-2xl bg-background-section-burn space-y-1'> <div className='w-full p-2 rounded-2xl bg-background-section-burn space-y-1'>
<InstallByDSLList <InstallByDSLList
fromDSLPayload={fromDSLPayload} allPlugins={allPlugins}
selectedPlugins={selectedPlugins} selectedPlugins={selectedPlugins}
onSelect={handleSelect} onSelect={handleSelect}
onLoadedAllPlugin={handleLoadedAllPlugin} onLoadedAllPlugin={handleLoadedAllPlugin}

View File

@ -98,7 +98,7 @@ const InstallFromLocalPackage: React.FC<InstallFromLocalPackageProps> = ({
step={step} step={step}
onStepChange={setStep} onStepChange={setStep}
onClose={onClose} onClose={onClose}
dependencies={dependencies} allPlugins={dependencies}
/> />
) : ( ) : (
<ReadyToInstallPackage <ReadyToInstallPackage

View File

@ -311,7 +311,7 @@ export type PluginsFromMarketplaceResponse = {
plugins: Plugin[] plugins: Plugin[]
} }
export type Dependency = { export type GitHubItemAndMarketPlaceDependency = {
type: 'github' | 'marketplace' | 'package' type: 'github' | 'marketplace' | 'package'
value: { value: {
repo?: string repo?: string
@ -323,6 +323,16 @@ export type Dependency = {
} }
} }
export type PackageDependency = {
type: 'github' | 'marketplace' | 'package'
value: {
unique_identifier: string
manifest: PluginDeclaration
}
}
export type Dependency = GitHubItemAndMarketPlaceDependency | PackageDependency
export type Version = { export type Version = {
plugin_org: string plugin_org: string
plugin_name: string plugin_name: string