index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 useReferenceSetting from './use-reference-setting'
  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 ReferenceSettingModal from '@/app/components/plugins/reference-setting-modal/modal'
  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 { getDocsUrl } from '@/app/components/plugins/utils'
  36. import { fetchBundleInfoFromMarketPlace, fetchManifestFromMarketPlace } from '@/service/plugins'
  37. import { MARKETPLACE_API_PREFIX } from '@/config'
  38. import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
  39. // import I18n from '@/context/i18n'
  40. import { noop } from 'lodash-es'
  41. import { PLUGIN_TYPE_SEARCH_MAP } from '../marketplace/plugin-type-switch'
  42. import { PLUGIN_PAGE_TABS_MAP } from '../hooks'
  43. import { useGlobalPublicStore } from '@/context/global-public-context'
  44. import useDocumentTitle from '@/hooks/use-document-title'
  45. const PACKAGE_IDS_KEY = 'package-ids'
  46. const BUNDLE_INFO_KEY = 'bundle-info'
  47. export type PluginPageProps = {
  48. plugins: React.ReactNode
  49. marketplace: React.ReactNode
  50. }
  51. const PluginPage = ({
  52. plugins,
  53. marketplace,
  54. }: PluginPageProps) => {
  55. const { t } = useTranslation()
  56. // zlwu12 2025-09-18 注释
  57. // const { locale } = useContext(I18n)
  58. const searchParams = useSearchParams()
  59. const { replace } = useRouter()
  60. useDocumentTitle(t('plugin.metadata.title'))
  61. // just support install one package now
  62. const packageId = useMemo(() => {
  63. const idStrings = searchParams.get(PACKAGE_IDS_KEY)
  64. try {
  65. return idStrings ? JSON.parse(idStrings)[0] : ''
  66. }
  67. catch {
  68. return ''
  69. }
  70. }, [searchParams])
  71. const [dependencies, setDependencies] = useState<Dependency[]>([])
  72. const bundleInfo = useMemo(() => {
  73. const info = searchParams.get(BUNDLE_INFO_KEY)
  74. try {
  75. return info ? JSON.parse(info) : undefined
  76. }
  77. catch {
  78. return undefined
  79. }
  80. }, [searchParams])
  81. const [isShowInstallFromMarketplace, {
  82. setTrue: showInstallFromMarketplace,
  83. setFalse: doHideInstallFromMarketplace,
  84. }] = useBoolean(false)
  85. const hideInstallFromMarketplace = () => {
  86. doHideInstallFromMarketplace()
  87. const url = new URL(window.location.href)
  88. url.searchParams.delete(PACKAGE_IDS_KEY)
  89. url.searchParams.delete(BUNDLE_INFO_KEY)
  90. replace(url.toString())
  91. }
  92. const [manifest, setManifest] = useState<PluginDeclaration | PluginManifestInMarket | null>(null)
  93. useEffect(() => {
  94. (async () => {
  95. await sleep(100)
  96. if (packageId) {
  97. const { data } = await fetchManifestFromMarketPlace(encodeURIComponent(packageId))
  98. const { plugin, version } = data
  99. setManifest({
  100. ...plugin,
  101. version: version.version,
  102. icon: `${MARKETPLACE_API_PREFIX}/plugins/${plugin.org}/${plugin.name}/icon`,
  103. })
  104. showInstallFromMarketplace()
  105. return
  106. }
  107. if (bundleInfo) {
  108. const { data } = await fetchBundleInfoFromMarketPlace(bundleInfo)
  109. setDependencies(data.version.dependencies)
  110. showInstallFromMarketplace()
  111. }
  112. })()
  113. }, [packageId, bundleInfo])
  114. const {
  115. referenceSetting,
  116. canManagement,
  117. canDebugger,
  118. canSetPermissions,
  119. setReferenceSettings,
  120. } = useReferenceSetting()
  121. const [showPluginSettingModal, {
  122. setTrue: setShowPluginSettingModal,
  123. setFalse: setHidePluginSettingModal,
  124. }] = useBoolean(false)
  125. const [currentFile, setCurrentFile] = useState<File | null>(null)
  126. const containerRef = usePluginPageContext(v => v.containerRef)
  127. const options = usePluginPageContext(v => v.options)
  128. const activeTab = usePluginPageContext(v => v.activeTab)
  129. const setActiveTab = usePluginPageContext(v => v.setActiveTab)
  130. const { enable_marketplace } = useGlobalPublicStore(s => s.systemFeatures)
  131. const isPluginsTab = useMemo(() => activeTab === PLUGIN_PAGE_TABS_MAP.plugins, [activeTab])
  132. const isExploringMarketplace = useMemo(() => {
  133. const values = Object.values(PLUGIN_TYPE_SEARCH_MAP)
  134. return activeTab === PLUGIN_PAGE_TABS_MAP.marketplace || values.includes(activeTab)
  135. }, [activeTab])
  136. const handleFileChange = (file: File | null) => {
  137. if (!file || !file.name.endsWith('.difypkg')) {
  138. setCurrentFile(null)
  139. return
  140. }
  141. setCurrentFile(file)
  142. }
  143. const uploaderProps = useUploader({
  144. onFileChange: handleFileChange,
  145. containerRef,
  146. enabled: isPluginsTab && canManagement,
  147. })
  148. const { dragging, fileUploader, fileChangeHandle, removeFile } = uploaderProps
  149. return (
  150. <div
  151. id='marketplace-container'
  152. ref={containerRef}
  153. style={{ scrollbarGutter: 'stable' }}
  154. className={cn('relative flex grow flex-col overflow-y-auto border-t border-divider-subtle', isPluginsTab
  155. ? 'rounded-t-xl bg-components-panel-bg'
  156. : 'bg-background-body',
  157. )}
  158. >
  159. <div
  160. className={cn(
  161. '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', isExploringMarketplace && 'bg-background-body',
  162. )}
  163. >
  164. <div className='flex w-full items-center justify-between'>
  165. <div className='flex-1'>
  166. <TabSlider
  167. value={isPluginsTab ? PLUGIN_PAGE_TABS_MAP.plugins : PLUGIN_PAGE_TABS_MAP.marketplace}
  168. onChange={setActiveTab}
  169. options={options}
  170. />
  171. </div>
  172. <div className='flex shrink-0 items-center gap-1'>
  173. {
  174. isExploringMarketplace && (
  175. <>
  176. {/* zlwu12 2025-09-18 注释 */}
  177. {/* <Link
  178. href='https://github.com/langgenius/dify-plugins/issues/new?template=plugin_request.yaml'
  179. target='_blank'
  180. >
  181. <Button
  182. variant='ghost'
  183. className='text-text-tertiary'
  184. >
  185. {t('plugin.requestAPlugin')}
  186. </Button>
  187. </Link>
  188. <Link
  189. href={getDocsUrl(locale, '/plugins/publish-plugins/publish-to-dify-marketplace/README')}
  190. target='_blank'
  191. >
  192. <Button
  193. className='px-3'
  194. variant='secondary-accent'
  195. >
  196. <RiBookOpenLine className='mr-1 h-4 w-4' />
  197. {t('plugin.publishPlugins')}
  198. </Button>
  199. </Link> */}
  200. <div className='mx-1 h-3.5 w-[1px] shrink-0 bg-divider-regular'></div>
  201. </>
  202. )
  203. }
  204. <PluginTasks />
  205. {canManagement && (
  206. <InstallPluginDropdown
  207. onSwitchToMarketplaceTab={() => setActiveTab('discover')}
  208. />
  209. )}
  210. {
  211. canDebugger && (
  212. <DebugInfo />
  213. )
  214. }
  215. {
  216. canSetPermissions && (
  217. <Tooltip
  218. popupContent={t('plugin.privilege.title')}
  219. >
  220. <Button
  221. className='group h-full w-full p-2 text-components-button-secondary-text'
  222. onClick={setShowPluginSettingModal}
  223. >
  224. <RiEqualizer2Line className='h-4 w-4' />
  225. </Button>
  226. </Tooltip>
  227. )
  228. }
  229. </div>
  230. </div>
  231. </div>
  232. {isPluginsTab && (
  233. <>
  234. {plugins}
  235. {dragging && (
  236. <div
  237. className="absolute inset-0 m-0.5 rounded-2xl border-2 border-dashed border-components-dropzone-border-accent
  238. bg-[rgba(21,90,239,0.14)] p-2">
  239. </div>
  240. )}
  241. <div className={`flex items-center justify-center gap-2 py-4 ${dragging ? 'text-text-accent' : 'text-text-quaternary'}`}>
  242. <RiDragDropLine className="h-4 w-4" />
  243. <span className="system-xs-regular">{t('plugin.installModal.dropPluginToInstall')}</span>
  244. </div>
  245. {currentFile && (
  246. <InstallFromLocalPackage
  247. file={currentFile}
  248. onClose={removeFile ?? noop}
  249. onSuccess={noop}
  250. />
  251. )}
  252. <input
  253. ref={fileUploader}
  254. className="hidden"
  255. type="file"
  256. id="fileUploader"
  257. accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
  258. onChange={fileChangeHandle ?? noop}
  259. />
  260. </>
  261. )}
  262. {
  263. isExploringMarketplace && enable_marketplace && marketplace
  264. }
  265. {showPluginSettingModal && (
  266. <ReferenceSettingModal
  267. payload={referenceSetting!}
  268. onHide={setHidePluginSettingModal}
  269. onSave={setReferenceSettings}
  270. />
  271. )}
  272. {
  273. isShowInstallFromMarketplace && (
  274. <InstallFromMarketplace
  275. manifest={manifest! as PluginManifestInMarket}
  276. uniqueIdentifier={packageId}
  277. isBundle={!!bundleInfo}
  278. dependencies={dependencies}
  279. onClose={hideInstallFromMarketplace}
  280. onSuccess={hideInstallFromMarketplace}
  281. />
  282. )
  283. }
  284. </div>
  285. )
  286. }
  287. const PluginPageWithContext = (props: PluginPageProps) => {
  288. return (
  289. <PluginPageContextProvider>
  290. <PluginPage {...props} />
  291. </PluginPageContextProvider>
  292. )
  293. }
  294. export default PluginPageWithContext