Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

detail-header.tsx 14KB

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