index.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. style={{ scrollbarGutter: 'stable' }}
  137. className={cn('grow relative flex flex-col overflow-y-auto border-t border-divider-subtle', activeTab === 'plugins'
  138. ? 'rounded-t-xl bg-components-panel-bg'
  139. : 'bg-background-body',
  140. )}
  141. >
  142. <div
  143. className={cn(
  144. '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',
  145. )}
  146. >
  147. <div className='flex justify-between items-center w-full'>
  148. <div className='flex-1'>
  149. <TabSlider
  150. value={activeTab}
  151. onChange={setActiveTab}
  152. options={options}
  153. />
  154. </div>
  155. <div className='flex shrink-0 items-center gap-1'>
  156. {
  157. activeTab === 'discover' && (
  158. <>
  159. <Link
  160. href={`https://docs.dify.ai/${locale === LanguagesSupported[1] ? 'v/zh-hans/' : ''}plugins/publish-plugins/publish-to-dify-marketplace`}
  161. target='_blank'
  162. >
  163. <Button
  164. className='px-3'
  165. variant='secondary-accent'
  166. >
  167. <RiBookOpenLine className='mr-1 w-4 h-4' />
  168. {t('plugin.submitPlugin')}
  169. </Button>
  170. </Link>
  171. <div className='mx-2 w-[1px] h-3.5 bg-divider-regular'></div>
  172. </>
  173. )
  174. }
  175. <PluginTasks />
  176. {canManagement && (
  177. <InstallPluginDropdown
  178. onSwitchToMarketplaceTab={() => setActiveTab('discover')}
  179. />
  180. )}
  181. {
  182. canDebugger && (
  183. <DebugInfo />
  184. )
  185. }
  186. {
  187. canSetPermissions && (
  188. <Tooltip
  189. popupContent={t('plugin.privilege.title')}
  190. >
  191. <Button
  192. className='w-full h-full p-2 text-components-button-secondary-text group'
  193. onClick={setShowPluginSettingModal}
  194. >
  195. <RiEqualizer2Line className='w-4 h-4' />
  196. </Button>
  197. </Tooltip>
  198. )
  199. }
  200. </div>
  201. </div>
  202. </div>
  203. {activeTab === 'plugins' && (
  204. <>
  205. {plugins}
  206. {dragging && (
  207. <div
  208. className="absolute inset-0 m-0.5 p-2 rounded-2xl bg-[rgba(21,90,239,0.14)] border-2
  209. border-dashed border-components-dropzone-border-accent">
  210. </div>
  211. )}
  212. <div className={`flex py-4 justify-center items-center gap-2 ${dragging ? 'text-text-accent' : 'text-text-quaternary'}`}>
  213. <RiDragDropLine className="w-4 h-4" />
  214. <span className="system-xs-regular">{t('plugin.installModal.dropPluginToInstall')}</span>
  215. </div>
  216. {currentFile && (
  217. <InstallFromLocalPackage
  218. file={currentFile}
  219. onClose={removeFile ?? (() => { })}
  220. onSuccess={() => { }}
  221. />
  222. )}
  223. <input
  224. ref={fileUploader}
  225. className="hidden"
  226. type="file"
  227. id="fileUploader"
  228. accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
  229. onChange={fileChangeHandle ?? (() => { })}
  230. />
  231. </>
  232. )}
  233. {
  234. activeTab === 'discover' && enable_marketplace && marketplace
  235. }
  236. {showPluginSettingModal && (
  237. <PermissionSetModal
  238. payload={permissions!}
  239. onHide={setHidePluginSettingModal}
  240. onSave={setPermissions}
  241. />
  242. )}
  243. {
  244. isShowInstallFromMarketplace && (
  245. <InstallFromMarketplace
  246. manifest={manifest! as PluginManifestInMarket}
  247. uniqueIdentifier={packageId}
  248. isBundle={!!bundleInfo}
  249. dependencies={dependencies}
  250. onClose={hideInstallFromMarketplace}
  251. onSuccess={hideInstallFromMarketplace}
  252. />
  253. )
  254. }
  255. </div>
  256. )
  257. }
  258. const PluginPageWithContext = (props: PluginPageProps) => {
  259. return (
  260. <PluginPageContextProvider>
  261. <PluginPage {...props} />
  262. </PluginPageContextProvider>
  263. )
  264. }
  265. export default PluginPageWithContext