You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 9.1KB

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