index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. 'use client'
  2. import { useEffect, useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Link from 'next/link'
  5. import {
  6. RiBookOpenLine,
  7. RiDragDropLine,
  8. RiEqualizer2Line,
  9. } from '@remixicon/react'
  10. import { useBoolean } from 'ahooks'
  11. import InstallFromLocalPackage from '../install-plugin/install-from-local-package'
  12. import {
  13. PluginPageContextProvider,
  14. usePluginPageContext,
  15. } from './context'
  16. import InstallPluginDropdown from './install-plugin-dropdown'
  17. import { useUploader } from './use-uploader'
  18. import usePermission from './use-permission'
  19. import DebugInfo from './debug-info'
  20. import PluginTasks from './plugin-tasks'
  21. import Button from '@/app/components/base/button'
  22. import TabSlider from '@/app/components/base/tab-slider'
  23. import Tooltip from '@/app/components/base/tooltip'
  24. import cn from '@/utils/classnames'
  25. import PermissionSetModal from '@/app/components/plugins/permission-setting-modal/modal'
  26. import { useSelector as useAppContextSelector } from '@/context/app-context'
  27. import InstallFromMarketplace from '../install-plugin/install-from-marketplace'
  28. import {
  29. useRouter,
  30. useSearchParams,
  31. } from 'next/navigation'
  32. import type { Dependency } from '../types'
  33. import type { PluginDeclaration, PluginManifestInMarket } from '../types'
  34. import { sleep } from '@/utils'
  35. import { fetchBundleInfoFromMarketPlace, fetchManifestFromMarketPlace } from '@/service/plugins'
  36. import { marketplaceApiPrefix } from '@/config'
  37. import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
  38. const PACKAGE_IDS_KEY = 'package-ids'
  39. const BUNDLE_INFO_KEY = 'bundle-info'
  40. export type PluginPageProps = {
  41. plugins: React.ReactNode
  42. marketplace: React.ReactNode
  43. }
  44. const PluginPage = ({
  45. plugins,
  46. marketplace,
  47. }: PluginPageProps) => {
  48. const { t } = useTranslation()
  49. const searchParams = useSearchParams()
  50. const { replace } = useRouter()
  51. // just support install one package now
  52. const packageId = useMemo(() => {
  53. const idStrings = searchParams.get(PACKAGE_IDS_KEY)
  54. try {
  55. return idStrings ? JSON.parse(idStrings)[0] : ''
  56. }
  57. catch (e) {
  58. return ''
  59. }
  60. }, [searchParams])
  61. const [dependencies, setDependencies] = useState<Dependency[]>([])
  62. const bundleInfo = useMemo(() => {
  63. const info = searchParams.get(BUNDLE_INFO_KEY)
  64. try {
  65. return info ? JSON.parse(info) : undefined
  66. }
  67. catch (e) {
  68. return undefined
  69. }
  70. }, [searchParams])
  71. const [isShowInstallFromMarketplace, {
  72. setTrue: showInstallFromMarketplace,
  73. setFalse: doHideInstallFromMarketplace,
  74. }] = useBoolean(false)
  75. const hideInstallFromMarketplace = () => {
  76. doHideInstallFromMarketplace()
  77. const url = new URL(window.location.href)
  78. url.searchParams.delete(PACKAGE_IDS_KEY)
  79. url.searchParams.delete(BUNDLE_INFO_KEY)
  80. replace(url.toString())
  81. }
  82. const [manifest, setManifest] = useState<PluginDeclaration | PluginManifestInMarket | null>(null)
  83. useEffect(() => {
  84. (async () => {
  85. await sleep(100)
  86. if (packageId) {
  87. const { data } = await fetchManifestFromMarketPlace(encodeURIComponent(packageId))
  88. const { plugin, version } = data
  89. setManifest({
  90. ...plugin,
  91. version: version.version,
  92. icon: `${marketplaceApiPrefix}/plugins/${plugin.org}/${plugin.name}/icon`,
  93. })
  94. showInstallFromMarketplace()
  95. return
  96. }
  97. if (bundleInfo) {
  98. const { data } = await fetchBundleInfoFromMarketPlace(bundleInfo)
  99. setDependencies(data.version.dependencies)
  100. showInstallFromMarketplace()
  101. }
  102. })()
  103. // eslint-disable-next-line react-hooks/exhaustive-deps
  104. }, [packageId, bundleInfo])
  105. const {
  106. canManagement,
  107. canDebugger,
  108. canSetPermissions,
  109. permissions,
  110. setPermissions,
  111. } = usePermission()
  112. const [showPluginSettingModal, {
  113. setTrue: setShowPluginSettingModal,
  114. setFalse: setHidePluginSettingModal,
  115. }] = useBoolean()
  116. const [currentFile, setCurrentFile] = useState<File | null>(null)
  117. const containerRef = usePluginPageContext(v => v.containerRef)
  118. const options = usePluginPageContext(v => v.options)
  119. const activeTab = usePluginPageContext(v => v.activeTab)
  120. const setActiveTab = usePluginPageContext(v => v.setActiveTab)
  121. const { enable_marketplace } = useAppContextSelector(s => s.systemFeatures)
  122. const uploaderProps = useUploader({
  123. onFileChange: setCurrentFile,
  124. containerRef,
  125. enabled: activeTab === 'plugins',
  126. })
  127. const { dragging, fileUploader, fileChangeHandle, removeFile } = uploaderProps
  128. return (
  129. <div
  130. id='marketplace-container'
  131. ref={containerRef}
  132. className={cn('grow relative flex flex-col overflow-y-auto border-t border-divider-subtle', activeTab === 'plugins'
  133. ? 'rounded-t-xl bg-components-panel-bg'
  134. : 'bg-background-body',
  135. )}
  136. >
  137. <div
  138. className={cn(
  139. 'sticky top-0 flex min-h-[60px] px-12 pt-4 pb-2 items-center self-stretch gap-1 z-10 bg-components-panel-bg', activeTab === 'discover' && 'bg-background-body',
  140. )}
  141. >
  142. <div className='flex justify-between items-center w-full'>
  143. <div className='flex-1'>
  144. <TabSlider
  145. value={activeTab}
  146. onChange={setActiveTab}
  147. options={options}
  148. />
  149. </div>
  150. <div className='flex shrink-0 items-center gap-1'>
  151. {
  152. activeTab === 'discover' && (
  153. <>
  154. <Link
  155. href='https://docs.dify.ai/plugins/publish-plugins/publish-to-dify-marketplace'
  156. target='_blank'
  157. >
  158. <Button
  159. className='px-3'
  160. variant='secondary-accent'
  161. >
  162. <RiBookOpenLine className='mr-1 w-4 h-4' />
  163. {t('plugin.submitPlugin')}
  164. </Button>
  165. </Link>
  166. <div className='mx-2 w-[1px] h-3.5 bg-divider-regular'></div>
  167. </>
  168. )
  169. }
  170. <PluginTasks />
  171. {canManagement && (
  172. <InstallPluginDropdown
  173. onSwitchToMarketplaceTab={() => setActiveTab('discover')}
  174. />
  175. )}
  176. {
  177. canDebugger && (
  178. <DebugInfo />
  179. )
  180. }
  181. {
  182. canSetPermissions && (
  183. <Tooltip
  184. popupContent={t('plugin.privilege.title')}
  185. >
  186. <Button
  187. className='w-full h-full p-2 text-components-button-secondary-text group'
  188. onClick={setShowPluginSettingModal}
  189. >
  190. <RiEqualizer2Line className='w-4 h-4' />
  191. </Button>
  192. </Tooltip>
  193. )
  194. }
  195. </div>
  196. </div>
  197. </div>
  198. {activeTab === 'plugins' && (
  199. <>
  200. {plugins}
  201. {dragging && (
  202. <div
  203. className="absolute inset-0 m-0.5 p-2 rounded-2xl bg-[rgba(21,90,239,0.14)] border-2
  204. border-dashed border-components-dropzone-border-accent">
  205. </div>
  206. )}
  207. <div className={`flex py-4 justify-center items-center gap-2 ${dragging ? 'text-text-accent' : 'text-text-quaternary'}`}>
  208. <RiDragDropLine className="w-4 h-4" />
  209. <span className="system-xs-regular">{t('plugin.installModal.dropPluginToInstall')}</span>
  210. </div>
  211. {currentFile && (
  212. <InstallFromLocalPackage
  213. file={currentFile}
  214. onClose={removeFile ?? (() => { })}
  215. onSuccess={() => { }}
  216. />
  217. )}
  218. <input
  219. ref={fileUploader}
  220. className="hidden"
  221. type="file"
  222. id="fileUploader"
  223. accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
  224. onChange={fileChangeHandle ?? (() => { })}
  225. />
  226. </>
  227. )}
  228. {
  229. activeTab === 'discover' && enable_marketplace && marketplace
  230. }
  231. {showPluginSettingModal && (
  232. <PermissionSetModal
  233. payload={permissions!}
  234. onHide={setHidePluginSettingModal}
  235. onSave={setPermissions}
  236. />
  237. )}
  238. {
  239. isShowInstallFromMarketplace && (
  240. <InstallFromMarketplace
  241. manifest={manifest! as PluginManifestInMarket}
  242. uniqueIdentifier={packageId}
  243. isBundle={!!bundleInfo}
  244. dependencies={dependencies}
  245. onClose={hideInstallFromMarketplace}
  246. onSuccess={hideInstallFromMarketplace}
  247. />
  248. )
  249. }
  250. </div>
  251. )
  252. }
  253. const PluginPageWithContext = (props: PluginPageProps) => {
  254. return (
  255. <PluginPageContextProvider>
  256. <PluginPage {...props} />
  257. </PluginPageContextProvider>
  258. )
  259. }
  260. export default PluginPageWithContext