Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

detail-header.tsx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. import { AutoUpdateLine } from '../../base/icons/src/vender/system'
  44. import { convertUTCDaySecondsToLocalSeconds, timeOfDayToDayjs } from '../reference-setting-modal/auto-update-setting/utils'
  45. import useReferenceSetting from '../plugin-page/use-reference-setting'
  46. import { AUTO_UPDATE_MODE } from '../reference-setting-modal/auto-update-setting/types'
  47. import { useAppContext } from '@/context/app-context'
  48. import { useGlobalPublicStore } from '@/context/global-public-context'
  49. const i18nPrefix = 'plugin.action'
  50. type Props = {
  51. detail: PluginDetail
  52. onHide: () => void
  53. onUpdate: (isDelete?: boolean) => void
  54. }
  55. const DetailHeader = ({
  56. detail,
  57. onHide,
  58. onUpdate,
  59. }: Props) => {
  60. const { t } = useTranslation()
  61. const { userProfile: { timezone } } = useAppContext()
  62. const { theme } = useTheme()
  63. const locale = useGetLanguage()
  64. const { locale: currentLocale } = useI18N()
  65. const { checkForUpdates, fetchReleases } = useGitHubReleases()
  66. const { setShowUpdatePluginModal } = useModalContext()
  67. const { refreshModelProviders } = useProviderContext()
  68. const invalidateAllToolProviders = useInvalidateAllToolProviders()
  69. const { enable_marketplace } = useGlobalPublicStore(s => s.systemFeatures)
  70. const {
  71. installation_id,
  72. source,
  73. tenant_id,
  74. version,
  75. latest_unique_identifier,
  76. latest_version,
  77. meta,
  78. plugin_id,
  79. status,
  80. deprecated_reason,
  81. alternative_plugin_id,
  82. } = detail
  83. const { author, category, name, label, description, icon, verified, tool } = detail.declaration
  84. const isTool = category === PluginType.tool
  85. const providerBriefInfo = tool?.identity
  86. const providerKey = `${plugin_id}/${providerBriefInfo?.name}`
  87. const { data: collectionList = [] } = useAllToolProviders(isTool)
  88. const provider = useMemo(() => {
  89. return collectionList.find(collection => collection.name === providerKey)
  90. }, [collectionList, providerKey])
  91. const isFromGitHub = source === PluginSource.github
  92. const isFromMarketplace = source === PluginSource.marketplace
  93. const [isShow, setIsShow] = useState(false)
  94. const [targetVersion, setTargetVersion] = useState({
  95. version: latest_version,
  96. unique_identifier: latest_unique_identifier,
  97. })
  98. const hasNewVersion = useMemo(() => {
  99. if (isFromMarketplace)
  100. return !!latest_version && latest_version !== version
  101. return false
  102. }, [isFromMarketplace, latest_version, version])
  103. const detailUrl = useMemo(() => {
  104. if (isFromGitHub)
  105. return `https://github.com/${meta!.repo}`
  106. if (isFromMarketplace)
  107. return getMarketplaceUrl(`/plugins/${author}/${name}`, { language: currentLocale, theme })
  108. return ''
  109. }, [author, isFromGitHub, isFromMarketplace, meta, name, theme])
  110. const [isShowUpdateModal, {
  111. setTrue: showUpdateModal,
  112. setFalse: hideUpdateModal,
  113. }] = useBoolean(false)
  114. const { referenceSetting } = useReferenceSetting()
  115. const { auto_upgrade: autoUpgradeInfo } = referenceSetting || {}
  116. const isAutoUpgradeEnabled = useMemo(() => {
  117. if (!enable_marketplace)
  118. return false
  119. if (!autoUpgradeInfo || !isFromMarketplace)
  120. return false
  121. if(autoUpgradeInfo.strategy_setting === 'disabled')
  122. return false
  123. if(autoUpgradeInfo.upgrade_mode === AUTO_UPDATE_MODE.update_all)
  124. return true
  125. if(autoUpgradeInfo.upgrade_mode === AUTO_UPDATE_MODE.partial && autoUpgradeInfo.include_plugins.includes(plugin_id))
  126. return true
  127. if(autoUpgradeInfo.upgrade_mode === AUTO_UPDATE_MODE.exclude && !autoUpgradeInfo.exclude_plugins.includes(plugin_id))
  128. return true
  129. return false
  130. }, [autoUpgradeInfo, plugin_id, isFromMarketplace])
  131. const [isDowngrade, setIsDowngrade] = useState(false)
  132. const handleUpdate = async (isDowngrade?: boolean) => {
  133. if (isFromMarketplace) {
  134. setIsDowngrade(!!isDowngrade)
  135. showUpdateModal()
  136. return
  137. }
  138. const owner = meta!.repo.split('/')[0] || author
  139. const repo = meta!.repo.split('/')[1] || name
  140. const fetchedReleases = await fetchReleases(owner, repo)
  141. if (fetchedReleases.length === 0) return
  142. const { needUpdate, toastProps } = checkForUpdates(fetchedReleases, meta!.version)
  143. Toast.notify(toastProps)
  144. if (needUpdate) {
  145. setShowUpdatePluginModal({
  146. onSaveCallback: () => {
  147. onUpdate()
  148. },
  149. payload: {
  150. type: PluginSource.github,
  151. category: detail.declaration.category,
  152. github: {
  153. originalPackageInfo: {
  154. id: detail.plugin_unique_identifier,
  155. repo: meta!.repo,
  156. version: meta!.version,
  157. package: meta!.package,
  158. releases: fetchedReleases,
  159. },
  160. },
  161. },
  162. })
  163. }
  164. }
  165. const handleUpdatedFromMarketplace = () => {
  166. onUpdate()
  167. hideUpdateModal()
  168. }
  169. const [isShowPluginInfo, {
  170. setTrue: showPluginInfo,
  171. setFalse: hidePluginInfo,
  172. }] = useBoolean(false)
  173. const [isShowDeleteConfirm, {
  174. setTrue: showDeleteConfirm,
  175. setFalse: hideDeleteConfirm,
  176. }] = useBoolean(false)
  177. const [deleting, {
  178. setTrue: showDeleting,
  179. setFalse: hideDeleting,
  180. }] = useBoolean(false)
  181. const handleDelete = useCallback(async () => {
  182. showDeleting()
  183. const res = await uninstallPlugin(installation_id)
  184. hideDeleting()
  185. if (res.success) {
  186. hideDeleteConfirm()
  187. onUpdate(true)
  188. if (PluginType.model.includes(category))
  189. refreshModelProviders()
  190. if (PluginType.tool.includes(category))
  191. invalidateAllToolProviders()
  192. }
  193. }, [showDeleting, installation_id, hideDeleting, hideDeleteConfirm, onUpdate, category, refreshModelProviders, invalidateAllToolProviders])
  194. return (
  195. <div className={cn('shrink-0 border-b border-divider-subtle bg-components-panel-bg p-4 pb-3')}>
  196. <div className="flex">
  197. <div className='overflow-hidden rounded-xl border border-components-panel-border-subtle'>
  198. <Icon src={`${API_PREFIX}/workspaces/current/plugin/icon?tenant_id=${tenant_id}&filename=${icon}`} />
  199. </div>
  200. <div className="ml-3 w-0 grow">
  201. <div className="flex h-5 items-center">
  202. <Title title={label[locale]} />
  203. {verified && <RiVerifiedBadgeLine className="ml-0.5 h-4 w-4 shrink-0 text-text-accent" />}
  204. <PluginVersionPicker
  205. disabled={!isFromMarketplace}
  206. isShow={isShow}
  207. onShowChange={setIsShow}
  208. pluginID={plugin_id}
  209. currentVersion={version}
  210. onSelect={(state) => {
  211. setTargetVersion(state)
  212. handleUpdate(state.isDowngrade)
  213. }}
  214. trigger={
  215. <Badge
  216. className={cn(
  217. 'mx-1',
  218. isShow && 'bg-state-base-hover',
  219. (isShow || isFromMarketplace) && 'hover:bg-state-base-hover',
  220. )}
  221. uppercase={false}
  222. text={
  223. <>
  224. <div>{isFromGitHub ? meta!.version : version}</div>
  225. {isFromMarketplace && <RiArrowLeftRightLine className='ml-1 h-3 w-3 text-text-tertiary' />}
  226. </>
  227. }
  228. hasRedCornerMark={hasNewVersion}
  229. />
  230. }
  231. />
  232. {/* Auto update info */}
  233. {isAutoUpgradeEnabled && (
  234. <Tooltip popupContent={t('plugin.autoUpdate.nextUpdateTime', { time: timeOfDayToDayjs(convertUTCDaySecondsToLocalSeconds(autoUpgradeInfo?.upgrade_time_of_day || 0, timezone!)).format('hh:mm A') })}>
  235. {/* add a a div to fix tooltip hover not show problem */}
  236. <div>
  237. <Badge className='mr-1 cursor-pointer px-1'>
  238. <AutoUpdateLine className='size-3' />
  239. </Badge>
  240. </div>
  241. </Tooltip>
  242. )}
  243. {(hasNewVersion || isFromGitHub) && (
  244. <Button variant='secondary-accent' size='small' className='!h-5' onClick={() => {
  245. if (isFromMarketplace) {
  246. setTargetVersion({
  247. version: latest_version,
  248. unique_identifier: latest_unique_identifier,
  249. })
  250. }
  251. handleUpdate()
  252. }}>{t('plugin.detailPanel.operation.update')}</Button>
  253. )}
  254. </div>
  255. <div className='mb-1 flex h-4 items-center justify-between'>
  256. <div className='mt-0.5 flex items-center'>
  257. <OrgInfo
  258. packageNameClassName='w-auto'
  259. orgName={author}
  260. packageName={name}
  261. />
  262. <div className='system-xs-regular ml-1 mr-0.5 text-text-quaternary'>·</div>
  263. {detail.source === PluginSource.marketplace && (
  264. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.marketplace')} >
  265. <div><BoxSparkleFill className='h-3.5 w-3.5 text-text-tertiary hover:text-text-accent' /></div>
  266. </Tooltip>
  267. )}
  268. {detail.source === PluginSource.github && (
  269. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.github')} >
  270. <div><Github className='h-3.5 w-3.5 text-text-secondary hover:text-text-primary' /></div>
  271. </Tooltip>
  272. )}
  273. {detail.source === PluginSource.local && (
  274. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.local')} >
  275. <div><RiHardDrive3Line className='h-3.5 w-3.5 text-text-tertiary' /></div>
  276. </Tooltip>
  277. )}
  278. {detail.source === PluginSource.debugging && (
  279. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.debugging')} >
  280. <div><RiBugLine className='h-3.5 w-3.5 text-text-tertiary hover:text-text-warning' /></div>
  281. </Tooltip>
  282. )}
  283. </div>
  284. </div>
  285. </div>
  286. <div className='flex gap-1'>
  287. <OperationDropdown
  288. source={detail.source}
  289. onInfo={showPluginInfo}
  290. onCheckVersion={handleUpdate}
  291. onRemove={showDeleteConfirm}
  292. detailUrl={detailUrl}
  293. />
  294. <ActionButton onClick={onHide}>
  295. <RiCloseLine className='h-4 w-4' />
  296. </ActionButton>
  297. </div>
  298. </div>
  299. {isFromMarketplace && (
  300. <DeprecationNotice
  301. status={status}
  302. deprecatedReason={deprecated_reason}
  303. alternativePluginId={alternative_plugin_id}
  304. alternativePluginURL={getMarketplaceUrl(`/plugins/${alternative_plugin_id}`, { language: currentLocale, theme })}
  305. className='mt-3'
  306. />
  307. )}
  308. <Description className='mb-2 mt-3 h-auto' text={description[locale]} descriptionLineRows={2}></Description>
  309. {
  310. category === PluginType.tool && (
  311. <PluginAuth
  312. pluginPayload={{
  313. provider: provider?.name || '',
  314. category: AuthCategory.tool,
  315. }}
  316. />
  317. )
  318. }
  319. {isShowPluginInfo && (
  320. <PluginInfo
  321. repository={isFromGitHub ? meta?.repo : ''}
  322. release={version}
  323. packageName={meta?.package || ''}
  324. onHide={hidePluginInfo}
  325. />
  326. )}
  327. {isShowDeleteConfirm && (
  328. <Confirm
  329. isShow
  330. title={t(`${i18nPrefix}.delete`)}
  331. content={
  332. <div>
  333. {t(`${i18nPrefix}.deleteContentLeft`)}<span className='system-md-semibold'>{label[locale]}</span>{t(`${i18nPrefix}.deleteContentRight`)}<br />
  334. {/* {usedInApps > 0 && t(`${i18nPrefix}.usedInApps`, { num: usedInApps })} */}
  335. </div>
  336. }
  337. onCancel={hideDeleteConfirm}
  338. onConfirm={handleDelete}
  339. isLoading={deleting}
  340. isDisabled={deleting}
  341. />
  342. )}
  343. {
  344. isShowUpdateModal && (
  345. <UpdateFromMarketplace
  346. pluginId={plugin_id}
  347. payload={{
  348. category: detail.declaration.category,
  349. originalPackageInfo: {
  350. id: detail.plugin_unique_identifier,
  351. payload: detail.declaration,
  352. },
  353. targetPackageInfo: {
  354. id: targetVersion.unique_identifier,
  355. version: targetVersion.version,
  356. },
  357. }}
  358. onCancel={hideUpdateModal}
  359. onSave={handleUpdatedFromMarketplace}
  360. isShowDowngradeWarningModal={isDowngrade && isAutoUpgradeEnabled}
  361. />
  362. )
  363. }
  364. </div>
  365. )
  366. }
  367. export default DetailHeader