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

170 lines
6.2 KiB
TypeScript
Raw Normal View History

2024-09-14 17:09:25 +08:00
'use client'
2024-10-14 18:43:08 +08:00
import { useMemo, useState } from 'react'
2024-09-14 17:09:25 +08:00
import { useTranslation } from 'react-i18next'
import {
RiArrowRightUpLine,
RiBugLine,
2024-09-16 18:58:39 +08:00
RiClipboardLine,
2024-10-14 18:43:08 +08:00
RiDragDropLine,
2024-09-14 17:09:25 +08:00
RiEqualizer2Line,
} from '@remixicon/react'
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-09-14 17:09:25 +08:00
import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
2024-09-16 18:58:39 +08:00
import { useModalContext } from '@/context/modal-context'
2024-09-14 17:09:25 +08:00
import Button from '@/app/components/base/button'
import TabSlider from '@/app/components/base/tab-slider'
2024-09-16 18:58:39 +08:00
import ActionButton from '@/app/components/base/action-button'
2024-09-14 17:09:25 +08:00
import Tooltip from '@/app/components/base/tooltip'
import cn from '@/utils/classnames'
2024-09-14 17:09:25 +08:00
2024-10-12 11:33:12 +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()
2024-10-11 16:15:24 +08:00
const { setShowPluginSettingModal } = useModalContext() as any
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)
2024-09-14 17:09:25 +08:00
const options = useMemo(() => {
return [
{ value: 'plugins', text: t('common.menus.plugins') },
2024-10-11 12:39:27 +08:00
{ value: 'discover', text: 'Explore Marketplace' },
2024-09-14 17:09:25 +08:00
]
}, [t])
const [activeTab, setActiveTab] = useTabSearchParams({
defaultTab: options[0].value,
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
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>
<div className='flex flex-shrink-0 items-center gap-1'>
<InstallPluginDropdown />
<Tooltip
triggerMethod='click'
popupContent={
<>
<div className='flex items-center gap-1 self-stretch'>
<span className='flex flex-col justify-center items-start flex-grow flex-shrink-0 basis-0 text-text-secondary system-sm-semibold'>Debugging</span>
<div className='flex items-center gap-0.5 text-text-accent-light-mode-only cursor-pointer'>
<span className='system-xs-medium'>View docs</span>
<RiArrowRightUpLine className='w-3 h-3' />
</div>
</div>
<div className='flex flex-col items-start gap-0.5 self-stretch'>
2024-09-16 18:58:39 +08:00
{['Port', 'Key'].map((label, index) => (
<div key={label} className='flex items-center gap-1 self-stretch'>
<span className='flex w-10 flex-col justify-center items-start text-text-tertiary system-xs-medium'>{label}</span>
<div className='flex justify-center items-center gap-0.5'>
<span className='system-xs-medium text-text-secondary'>
{index === 0 ? 'cloud.dify,ai:2048' : 'A1B2C3D4E5F6G7H8'}
</span>
<ActionButton>
<RiClipboardLine className='w-3.5 h-3.5 text-text-tertiary' />
</ActionButton>
</div>
</div>
))}
2024-09-14 17:09:25 +08:00
</div>
</>
}
popupClassName='flex flex-col items-start w-[256px] px-4 py-3.5 gap-1 border border-components-panel-border
2024-10-14 18:43:08 +08:00
rounded-xl bg-components-tooltip-bg shadows-shadow-lg z-50'
2024-09-14 17:09:25 +08:00
asChild={false}
position='bottom'
>
<Button className='w-full h-full p-2 text-components-button-secondary-text'>
<RiBugLine className='w-4 h-4' />
</Button>
</Tooltip>
2024-09-16 18:58:39 +08:00
<Button
className='w-full h-full p-2 text-components-button-secondary-text group'
onClick={() => {
setShowPluginSettingModal()
}}
>
2024-09-14 17:09:25 +08:00
<RiEqualizer2Line className='w-4 h-4' />
</Button>
</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" />
<span className="system-xs-regular">Drop plugin package here to install</span>
</div>
{currentFile && (
<InstallFromLocalPackage file={currentFile} onClose={removeFile ?? (() => {})} />
2024-10-14 18:43:08 +08:00
)}
<input
ref={fileUploader}
className="hidden"
type="file"
id="fileUploader"
accept='.difypkg'
onChange={fileChangeHandle ?? (() => {})}
/>
2024-10-14 18:43:08 +08:00
</>
)}
2024-10-09 15:06:09 +08:00
{
2024-10-11 16:15:24 +08:00
activeTab === 'discover' && marketplace
2024-10-09 15:06:09 +08:00
}
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