dify/web/app/components/plugins/plugin-page/index.tsx

256 lines
8.0 KiB
TypeScript
Raw Normal View History

2024-09-14 17:09:25 +08:00
'use client'
2024-10-23 16:42:24 +08:00
import { useEffect, useMemo, useState } from 'react'
2024-09-14 17:09:25 +08:00
import { useTranslation } from 'react-i18next'
import {
2024-10-14 18:43:08 +08:00
RiDragDropLine,
2024-09-14 17:09:25 +08:00
RiEqualizer2Line,
} from '@remixicon/react'
2024-10-15 19:20:59 +08:00
import { useBoolean } from 'ahooks'
2024-10-14 18:43:08 +08:00
import InstallFromLocalPackage from '../install-plugin/install-from-local-package'
2024-10-12 11:33:12 +08:00
import {
PluginPageContextProvider,
2024-10-12 16:34:02 +08:00
usePluginPageContext,
2024-10-12 11:33:12 +08:00
} from './context'
import InstallPluginDropdown from './install-plugin-dropdown'
2024-10-14 18:43:08 +08:00
import { useUploader } from './use-uploader'
2024-10-15 19:20:59 +08:00
import usePermission from './use-permission'
import DebugInfo from './debug-info'
2024-11-11 18:17:44 +08:00
import PluginTasks from './plugin-tasks'
2024-09-14 17:09:25 +08:00
import Button from '@/app/components/base/button'
import TabSlider from '@/app/components/base/tab-slider'
import Tooltip from '@/app/components/base/tooltip'
import cn from '@/utils/classnames'
2024-10-15 19:20:59 +08:00
import PermissionSetModal from '@/app/components/plugins/permission-setting-modal/modal'
2024-10-16 15:49:56 +08:00
import { useSelector as useAppContextSelector } from '@/context/app-context'
import InstallFromMarketplace from '../install-plugin/install-from-marketplace'
import {
useRouter,
useSearchParams,
} from 'next/navigation'
import type { Dependency } from '../types'
2024-10-29 16:33:27 +08:00
import type { PluginDeclaration, PluginManifestInMarket } from '../types'
2024-10-23 16:42:24 +08:00
import { sleep } from '@/utils'
import { fetchBundleInfoFromMarketPlace, fetchManifestFromMarketPlace } from '@/service/plugins'
2024-10-29 16:33:27 +08:00
import { marketplaceApiPrefix } from '@/config'
2024-11-19 15:05:15 +08:00
import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
const PACKAGE_IDS_KEY = 'package-ids'
const BUNDLE_INFO_KEY = 'bundle-info'
2024-09-14 17:09:25 +08:00
2024-10-29 14:53:14 +08:00
export type PluginPageProps = {
2024-10-11 16:15:24 +08:00
plugins: React.ReactNode
marketplace: React.ReactNode
}
2024-10-12 11:33:12 +08:00
const PluginPage = ({
2024-10-11 16:15:24 +08:00
plugins,
marketplace,
2024-10-12 11:33:12 +08:00
}: PluginPageProps) => {
2024-09-14 17:09:25 +08:00
const { t } = useTranslation()
const searchParams = useSearchParams()
const { replace } = useRouter()
// just support install one package now
const packageId = useMemo(() => {
const idStrings = searchParams.get(PACKAGE_IDS_KEY)
try {
return idStrings ? JSON.parse(idStrings)[0] : ''
}
catch (e) {
return ''
}
}, [searchParams])
const [dependencies, setDependencies] = useState<Dependency[]>([])
const bundleInfo = useMemo(() => {
const info = searchParams.get(BUNDLE_INFO_KEY)
try {
return info ? JSON.parse(info) : undefined
}
catch (e) {
return undefined
}
}, [searchParams])
2024-10-23 16:42:24 +08:00
const [isShowInstallFromMarketplace, {
setTrue: showInstallFromMarketplace,
setFalse: doHideInstallFromMarketplace,
}] = useBoolean(false)
const hideInstallFromMarketplace = () => {
doHideInstallFromMarketplace()
const url = new URL(window.location.href)
url.searchParams.delete(PACKAGE_IDS_KEY)
url.searchParams.delete(BUNDLE_INFO_KEY)
2024-10-23 16:42:24 +08:00
replace(url.toString())
}
2024-10-29 16:33:27 +08:00
const [manifest, setManifest] = useState<PluginDeclaration | PluginManifestInMarket | null>(null)
2024-10-23 16:42:24 +08:00
useEffect(() => {
(async () => {
await sleep(100)
if (packageId) {
2024-10-29 15:19:47 +08:00
const { data } = await fetchManifestFromMarketPlace(encodeURIComponent(packageId))
2024-11-22 15:32:27 +08:00
const { plugin, version } = data
2024-10-29 16:33:27 +08:00
setManifest({
...plugin,
2024-11-22 15:32:27 +08:00
version: version.version,
2024-10-29 16:33:27 +08:00
icon: `${marketplaceApiPrefix}/plugins/${plugin.org}/${plugin.name}/icon`,
})
2024-10-23 16:42:24 +08:00
showInstallFromMarketplace()
return
}
if (bundleInfo) {
const { data } = await fetchBundleInfoFromMarketPlace(bundleInfo)
setDependencies(data.version.dependencies)
showInstallFromMarketplace()
2024-10-23 16:42:24 +08:00
}
})()
2024-11-21 17:38:09 +08:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [packageId, bundleInfo])
2024-10-15 19:20:59 +08:00
const {
2024-10-15 21:35:56 +08:00
canManagement,
2024-10-15 19:20:59 +08:00
canDebugger,
canSetPermissions,
permissions,
setPermissions,
} = usePermission()
const [showPluginSettingModal, {
setTrue: setShowPluginSettingModal,
setFalse: setHidePluginSettingModal,
}] = useBoolean()
2024-10-14 18:43:08 +08:00
const [currentFile, setCurrentFile] = useState<File | null>(null)
2024-10-12 16:34:02 +08:00
const containerRef = usePluginPageContext(v => v.containerRef)
const options = usePluginPageContext(v => v.options)
const activeTab = usePluginPageContext(v => v.activeTab)
const setActiveTab = usePluginPageContext(v => v.setActiveTab)
2024-10-16 15:49:56 +08:00
const { enable_marketplace } = useAppContextSelector(s => s.systemFeatures)
2024-09-14 17:09:25 +08:00
const uploaderProps = useUploader({
onFileChange: setCurrentFile,
containerRef,
enabled: activeTab === 'plugins',
})
const { dragging, fileUploader, fileChangeHandle, removeFile } = uploaderProps
2024-09-14 17:09:25 +08:00
return (
2024-09-18 18:32:33 +08:00
<div
2024-11-07 15:06:55 +08:00
id='marketplace-container'
2024-09-18 18:32:33 +08:00
ref={containerRef}
className={cn('grow relative flex flex-col overflow-y-auto border-t border-divider-subtle', activeTab === 'plugins'
? 'rounded-t-xl bg-components-panel-bg'
: 'bg-background-body',
)}
2024-09-18 18:32:33 +08:00
>
2024-10-12 16:34:02 +08:00
<div
className={cn(
2024-10-15 11:02:25 +08:00
'sticky top-0 flex min-h-[60px] px-12 pt-4 pb-2 items-center self-stretch gap-1 z-10', activeTab === 'discover' && 'bg-background-body',
2024-10-12 16:34:02 +08:00
)}
>
2024-09-14 17:09:25 +08:00
<div className='flex justify-between items-center w-full'>
<div className='flex-1'>
<TabSlider
value={activeTab}
onChange={setActiveTab}
2024-09-14 17:09:25 +08:00
options={options}
/>
</div>
2024-11-14 18:26:16 +08:00
<div className='flex shrink-0 items-center gap-1'>
2024-11-11 18:17:44 +08:00
<PluginTasks />
2024-10-15 21:35:56 +08:00
{canManagement && (
2024-10-23 15:00:31 +08:00
<InstallPluginDropdown
onSwitchToMarketplaceTab={() => setActiveTab('discover')}
/>
2024-10-15 19:20:59 +08:00
)}
{
canDebugger && (
<DebugInfo />
2024-10-15 19:20:59 +08:00
)
}
{
canSetPermissions && (
2024-10-15 21:31:06 +08:00
<Tooltip
popupContent={t('plugin.privilege.title')}
2024-10-15 19:20:59 +08:00
>
2024-10-15 21:31:06 +08:00
<Button
className='w-full h-full p-2 text-components-button-secondary-text group'
onClick={setShowPluginSettingModal}
>
<RiEqualizer2Line className='w-4 h-4' />
</Button>
</Tooltip>
2024-10-15 19:20:59 +08:00
)
}
2024-09-14 17:09:25 +08:00
</div>
</div>
</div>
2024-10-14 18:43:08 +08:00
{activeTab === 'plugins' && (
<>
{plugins}
{dragging && (
<div
className="absolute inset-0 m-0.5 p-2 rounded-2xl bg-[rgba(21,90,239,0.14)] border-2
border-dashed border-components-dropzone-border-accent">
</div>
)}
<div className={`flex py-4 justify-center items-center gap-2 ${dragging ? 'text-text-accent' : 'text-text-quaternary'}`}>
<RiDragDropLine className="w-4 h-4" />
2024-11-14 18:26:16 +08:00
<span className="system-xs-regular">{t('plugin.installModal.dropPluginToInstall')}</span>
2024-10-14 18:43:08 +08:00
</div>
{currentFile && (
2024-10-22 17:21:25 +08:00
<InstallFromLocalPackage
file={currentFile}
onClose={removeFile ?? (() => { })}
onSuccess={() => { }}
/>
2024-10-14 18:43:08 +08:00
)}
<input
ref={fileUploader}
className="hidden"
type="file"
id="fileUploader"
2024-11-19 15:05:15 +08:00
accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
2024-10-15 19:20:59 +08:00
onChange={fileChangeHandle ?? (() => { })}
/>
2024-10-14 18:43:08 +08:00
</>
)}
2024-10-09 15:06:09 +08:00
{
2024-10-25 10:41:05 +08:00
activeTab === 'discover' && enable_marketplace && marketplace
2024-10-09 15:06:09 +08:00
}
2024-10-15 19:20:59 +08:00
{showPluginSettingModal && (
<PermissionSetModal
2024-11-08 16:11:50 +08:00
payload={permissions!}
2024-10-15 19:20:59 +08:00
onHide={setHidePluginSettingModal}
onSave={setPermissions}
/>
)}
{
isShowInstallFromMarketplace && (
<InstallFromMarketplace
2024-10-29 16:33:27 +08:00
manifest={manifest! as PluginManifestInMarket}
2024-10-25 16:46:02 +08:00
uniqueIdentifier={packageId}
isBundle={!!bundleInfo}
dependencies={dependencies}
onClose={hideInstallFromMarketplace}
2024-10-23 16:42:24 +08:00
onSuccess={hideInstallFromMarketplace}
/>
)
}
2024-09-14 17:09:25 +08:00
</div>
)
}
2024-10-12 11:33:12 +08:00
const PluginPageWithContext = (props: PluginPageProps) => {
return (
<PluginPageContextProvider>
<PluginPage {...props} />
</PluginPageContextProvider>
)
}
export default PluginPageWithContext