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.

detail-header.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import React, { useCallback, useMemo, useState } from 'react'
  2. import { useTheme } from 'next-themes'
  3. import { useTranslation } from 'react-i18next'
  4. import { useBoolean } from 'ahooks'
  5. import {
  6. RiArrowLeftRightLine,
  7. RiBugLine,
  8. RiCloseLine,
  9. RiHardDrive3Line,
  10. RiVerifiedBadgeLine,
  11. } from '@remixicon/react'
  12. import type { PluginDetail } from '../types'
  13. import { PluginSource, PluginType } from '../types'
  14. import Description from '../card/base/description'
  15. import Icon from '../card/base/card-icon'
  16. import Title from '../card/base/title'
  17. import OrgInfo from '../card/base/org-info'
  18. import { useGitHubReleases } from '../install-plugin/hooks'
  19. import PluginVersionPicker from '@/app/components/plugins/update-plugin/plugin-version-picker'
  20. import UpdateFromMarketplace from '@/app/components/plugins/update-plugin/from-market-place'
  21. import OperationDropdown from '@/app/components/plugins/plugin-detail-panel/operation-dropdown'
  22. import PluginInfo from '@/app/components/plugins/plugin-page/plugin-info'
  23. import ActionButton from '@/app/components/base/action-button'
  24. import Button from '@/app/components/base/button'
  25. import Badge from '@/app/components/base/badge'
  26. import Confirm from '@/app/components/base/confirm'
  27. import Tooltip from '@/app/components/base/tooltip'
  28. import Toast from '@/app/components/base/toast'
  29. import { BoxSparkleFill } from '@/app/components/base/icons/src/vender/plugin'
  30. import { Github } from '@/app/components/base/icons/src/public/common'
  31. import { uninstallPlugin } from '@/service/plugins'
  32. import { useGetLanguage, useI18N } from '@/context/i18n'
  33. import { useModalContext } from '@/context/modal-context'
  34. import { useProviderContext } from '@/context/provider-context'
  35. import { useInvalidateAllToolProviders } from '@/service/use-tools'
  36. import { API_PREFIX } from '@/config'
  37. import cn from '@/utils/classnames'
  38. import { getMarketplaceUrl } from '@/utils/var'
  39. import { PluginAuth } from '@/app/components/plugins/plugin-auth'
  40. import { AuthCategory } from '@/app/components/plugins/plugin-auth'
  41. import { useAllToolProviders } from '@/service/use-tools'
  42. import DeprecationNotice from '../base/deprecation-notice'
  43. const i18nPrefix = 'plugin.action'
  44. type Props = {
  45. detail: PluginDetail
  46. onHide: () => void
  47. onUpdate: (isDelete?: boolean) => void
  48. }
  49. const DetailHeader = ({
  50. detail,
  51. onHide,
  52. onUpdate,
  53. }: Props) => {
  54. const { t } = useTranslation()
  55. const { theme } = useTheme()
  56. const locale = useGetLanguage()
  57. const { locale: currentLocale } = useI18N()
  58. const { checkForUpdates, fetchReleases } = useGitHubReleases()
  59. const { setShowUpdatePluginModal } = useModalContext()
  60. const { refreshModelProviders } = useProviderContext()
  61. const invalidateAllToolProviders = useInvalidateAllToolProviders()
  62. const {
  63. installation_id,
  64. source,
  65. tenant_id,
  66. version,
  67. latest_unique_identifier,
  68. latest_version,
  69. meta,
  70. plugin_id,
  71. status,
  72. deprecated_reason,
  73. alternative_plugin_id,
  74. } = detail
  75. const { author, category, name, label, description, icon, verified, tool } = detail.declaration
  76. const isTool = category === PluginType.tool
  77. const providerBriefInfo = tool?.identity
  78. const providerKey = `${plugin_id}/${providerBriefInfo?.name}`
  79. const { data: collectionList = [] } = useAllToolProviders(isTool)
  80. const provider = useMemo(() => {
  81. return collectionList.find(collection => collection.name === providerKey)
  82. }, [collectionList, providerKey])
  83. const isFromGitHub = source === PluginSource.github
  84. const isFromMarketplace = source === PluginSource.marketplace
  85. const [isShow, setIsShow] = useState(false)
  86. const [targetVersion, setTargetVersion] = useState({
  87. version: latest_version,
  88. unique_identifier: latest_unique_identifier,
  89. })
  90. const hasNewVersion = useMemo(() => {
  91. if (isFromMarketplace)
  92. return !!latest_version && latest_version !== version
  93. return false
  94. }, [isFromMarketplace, latest_version, version])
  95. const detailUrl = useMemo(() => {
  96. if (isFromGitHub)
  97. return `https://github.com/${meta!.repo}`
  98. if (isFromMarketplace)
  99. return getMarketplaceUrl(`/plugins/${author}/${name}`, { language: currentLocale, theme })
  100. return ''
  101. }, [author, isFromGitHub, isFromMarketplace, meta, name, theme])
  102. const [isShowUpdateModal, {
  103. setTrue: showUpdateModal,
  104. setFalse: hideUpdateModal,
  105. }] = useBoolean(false)
  106. const handleUpdate = async () => {
  107. if (isFromMarketplace) {
  108. showUpdateModal()
  109. return
  110. }
  111. const owner = meta!.repo.split('/')[0] || author
  112. const repo = meta!.repo.split('/')[1] || name
  113. const fetchedReleases = await fetchReleases(owner, repo)
  114. if (fetchedReleases.length === 0) return
  115. const { needUpdate, toastProps } = checkForUpdates(fetchedReleases, meta!.version)
  116. Toast.notify(toastProps)
  117. if (needUpdate) {
  118. setShowUpdatePluginModal({
  119. onSaveCallback: () => {
  120. onUpdate()
  121. },
  122. payload: {
  123. type: PluginSource.github,
  124. category: detail.declaration.category,
  125. github: {
  126. originalPackageInfo: {
  127. id: detail.plugin_unique_identifier,
  128. repo: meta!.repo,
  129. version: meta!.version,
  130. package: meta!.package,
  131. releases: fetchedReleases,
  132. },
  133. },
  134. },
  135. })
  136. }
  137. }
  138. const handleUpdatedFromMarketplace = () => {
  139. onUpdate()
  140. hideUpdateModal()
  141. }
  142. const [isShowPluginInfo, {
  143. setTrue: showPluginInfo,
  144. setFalse: hidePluginInfo,
  145. }] = useBoolean(false)
  146. const [isShowDeleteConfirm, {
  147. setTrue: showDeleteConfirm,
  148. setFalse: hideDeleteConfirm,
  149. }] = useBoolean(false)
  150. const [deleting, {
  151. setTrue: showDeleting,
  152. setFalse: hideDeleting,
  153. }] = useBoolean(false)
  154. const handleDelete = useCallback(async () => {
  155. showDeleting()
  156. const res = await uninstallPlugin(installation_id)
  157. hideDeleting()
  158. if (res.success) {
  159. hideDeleteConfirm()
  160. onUpdate(true)
  161. if (PluginType.model.includes(category))
  162. refreshModelProviders()
  163. if (PluginType.tool.includes(category))
  164. invalidateAllToolProviders()
  165. }
  166. }, [showDeleting, installation_id, hideDeleting, hideDeleteConfirm, onUpdate, category, refreshModelProviders, invalidateAllToolProviders])
  167. // #plugin TODO# used in apps
  168. // const usedInApps = 3
  169. return (
  170. <div className={cn('shrink-0 border-b border-divider-subtle bg-components-panel-bg p-4 pb-3')}>
  171. <div className="flex">
  172. <div className='overflow-hidden rounded-xl border border-components-panel-border-subtle'>
  173. <Icon src={`${API_PREFIX}/workspaces/current/plugin/icon?tenant_id=${tenant_id}&filename=${icon}`} />
  174. </div>
  175. <div className="ml-3 w-0 grow">
  176. <div className="flex h-5 items-center">
  177. <Title title={label[locale]} />
  178. {verified && <RiVerifiedBadgeLine className="ml-0.5 h-4 w-4 shrink-0 text-text-accent" />}
  179. <PluginVersionPicker
  180. disabled={!isFromMarketplace}
  181. isShow={isShow}
  182. onShowChange={setIsShow}
  183. pluginID={plugin_id}
  184. currentVersion={version}
  185. onSelect={(state) => {
  186. setTargetVersion(state)
  187. handleUpdate()
  188. }}
  189. trigger={
  190. <Badge
  191. className={cn(
  192. 'mx-1',
  193. isShow && 'bg-state-base-hover',
  194. (isShow || isFromMarketplace) && 'hover:bg-state-base-hover',
  195. )}
  196. uppercase={false}
  197. text={
  198. <>
  199. <div>{isFromGitHub ? meta!.version : version}</div>
  200. {isFromMarketplace && <RiArrowLeftRightLine className='ml-1 h-3 w-3 text-text-tertiary' />}
  201. </>
  202. }
  203. hasRedCornerMark={hasNewVersion}
  204. />
  205. }
  206. />
  207. {(hasNewVersion || isFromGitHub) && (
  208. <Button variant='secondary-accent' size='small' className='!h-5' onClick={() => {
  209. if (isFromMarketplace) {
  210. setTargetVersion({
  211. version: latest_version,
  212. unique_identifier: latest_unique_identifier,
  213. })
  214. }
  215. handleUpdate()
  216. }}>{t('plugin.detailPanel.operation.update')}</Button>
  217. )}
  218. </div>
  219. <div className='mb-1 flex h-4 items-center justify-between'>
  220. <div className='mt-0.5 flex items-center'>
  221. <OrgInfo
  222. packageNameClassName='w-auto'
  223. orgName={author}
  224. packageName={name}
  225. />
  226. <div className='system-xs-regular ml-1 mr-0.5 text-text-quaternary'>·</div>
  227. {detail.source === PluginSource.marketplace && (
  228. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.marketplace')} >
  229. <div><BoxSparkleFill className='h-3.5 w-3.5 text-text-tertiary hover:text-text-accent' /></div>
  230. </Tooltip>
  231. )}
  232. {detail.source === PluginSource.github && (
  233. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.github')} >
  234. <div><Github className='h-3.5 w-3.5 text-text-secondary hover:text-text-primary' /></div>
  235. </Tooltip>
  236. )}
  237. {detail.source === PluginSource.local && (
  238. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.local')} >
  239. <div><RiHardDrive3Line className='h-3.5 w-3.5 text-text-tertiary' /></div>
  240. </Tooltip>
  241. )}
  242. {detail.source === PluginSource.debugging && (
  243. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.debugging')} >
  244. <div><RiBugLine className='h-3.5 w-3.5 text-text-tertiary hover:text-text-warning' /></div>
  245. </Tooltip>
  246. )}
  247. </div>
  248. </div>
  249. </div>
  250. <div className='flex gap-1'>
  251. <OperationDropdown
  252. source={detail.source}
  253. onInfo={showPluginInfo}
  254. onCheckVersion={handleUpdate}
  255. onRemove={showDeleteConfirm}
  256. detailUrl={detailUrl}
  257. />
  258. <ActionButton onClick={onHide}>
  259. <RiCloseLine className='h-4 w-4' />
  260. </ActionButton>
  261. </div>
  262. </div>
  263. {isFromMarketplace && (
  264. <DeprecationNotice
  265. status={status}
  266. deprecatedReason={deprecated_reason}
  267. alternativePluginId={alternative_plugin_id}
  268. alternativePluginURL={getMarketplaceUrl(`/plugins/${alternative_plugin_id}`, { language: currentLocale, theme })}
  269. className='mt-3'
  270. />
  271. )}
  272. <Description className='mb-2 mt-3 h-auto' text={description[locale]} descriptionLineRows={2}></Description>
  273. {
  274. category === PluginType.tool && (
  275. <PluginAuth
  276. pluginPayload={{
  277. provider: provider?.name || '',
  278. category: AuthCategory.tool,
  279. }}
  280. />
  281. )
  282. }
  283. {isShowPluginInfo && (
  284. <PluginInfo
  285. repository={isFromGitHub ? meta?.repo : ''}
  286. release={version}
  287. packageName={meta?.package || ''}
  288. onHide={hidePluginInfo}
  289. />
  290. )}
  291. {isShowDeleteConfirm && (
  292. <Confirm
  293. isShow
  294. title={t(`${i18nPrefix}.delete`)}
  295. content={
  296. <div>
  297. {t(`${i18nPrefix}.deleteContentLeft`)}<span className='system-md-semibold'>{label[locale]}</span>{t(`${i18nPrefix}.deleteContentRight`)}<br />
  298. {/* {usedInApps > 0 && t(`${i18nPrefix}.usedInApps`, { num: usedInApps })} */}
  299. </div>
  300. }
  301. onCancel={hideDeleteConfirm}
  302. onConfirm={handleDelete}
  303. isLoading={deleting}
  304. isDisabled={deleting}
  305. />
  306. )}
  307. {
  308. isShowUpdateModal && (
  309. <UpdateFromMarketplace
  310. payload={{
  311. category: detail.declaration.category,
  312. originalPackageInfo: {
  313. id: detail.plugin_unique_identifier,
  314. payload: detail.declaration,
  315. },
  316. targetPackageInfo: {
  317. id: targetVersion.unique_identifier,
  318. version: targetVersion.version,
  319. },
  320. }}
  321. onCancel={hideUpdateModal}
  322. onSave={handleUpdatedFromMarketplace}
  323. />
  324. )
  325. }
  326. </div>
  327. )
  328. }
  329. export default DetailHeader