index.tsx 9.0 KB

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