Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.tsx 9.1KB

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