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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 } 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. const i18nPrefix = 'plugin.action'
  40. type Props = {
  41. detail: PluginDetail
  42. onHide: () => void
  43. onUpdate: (isDelete?: boolean) => void
  44. }
  45. const DetailHeader = ({
  46. detail,
  47. onHide,
  48. onUpdate,
  49. }: Props) => {
  50. const { t } = useTranslation()
  51. const { theme } = useTheme()
  52. const locale = useGetLanguage()
  53. const { checkForUpdates, fetchReleases } = useGitHubReleases()
  54. const { setShowUpdatePluginModal } = useModalContext()
  55. const { refreshModelProviders } = useProviderContext()
  56. const invalidateAllToolProviders = useInvalidateAllToolProviders()
  57. const {
  58. installation_id,
  59. source,
  60. tenant_id,
  61. version,
  62. latest_unique_identifier,
  63. latest_version,
  64. meta,
  65. plugin_id,
  66. } = detail
  67. const { author, category, name, label, description, icon, verified } = detail.declaration
  68. const isFromGitHub = source === PluginSource.github
  69. const isFromMarketplace = source === PluginSource.marketplace
  70. const [isShow, setIsShow] = useState(false)
  71. const [targetVersion, setTargetVersion] = useState({
  72. version: latest_version,
  73. unique_identifier: latest_unique_identifier,
  74. })
  75. const hasNewVersion = useMemo(() => {
  76. if (isFromMarketplace)
  77. return !!latest_version && latest_version !== version
  78. return false
  79. }, [isFromMarketplace, latest_version, version])
  80. const detailUrl = useMemo(() => {
  81. if (isFromGitHub)
  82. return `https://github.com/${meta!.repo}`
  83. if (isFromMarketplace)
  84. return getMarketplaceUrl(`/plugins/${author}/${name}`, { theme })
  85. return ''
  86. }, [author, isFromGitHub, isFromMarketplace, meta, name, theme])
  87. const [isShowUpdateModal, {
  88. setTrue: showUpdateModal,
  89. setFalse: hideUpdateModal,
  90. }] = useBoolean(false)
  91. const handleUpdate = async () => {
  92. if (isFromMarketplace) {
  93. showUpdateModal()
  94. return
  95. }
  96. const owner = meta!.repo.split('/')[0] || author
  97. const repo = meta!.repo.split('/')[1] || name
  98. const fetchedReleases = await fetchReleases(owner, repo)
  99. if (fetchedReleases.length === 0) return
  100. const { needUpdate, toastProps } = checkForUpdates(fetchedReleases, meta!.version)
  101. Toast.notify(toastProps)
  102. if (needUpdate) {
  103. setShowUpdatePluginModal({
  104. onSaveCallback: () => {
  105. onUpdate()
  106. },
  107. payload: {
  108. type: PluginSource.github,
  109. category: detail.declaration.category,
  110. github: {
  111. originalPackageInfo: {
  112. id: detail.plugin_unique_identifier,
  113. repo: meta!.repo,
  114. version: meta!.version,
  115. package: meta!.package,
  116. releases: fetchedReleases,
  117. },
  118. },
  119. },
  120. })
  121. }
  122. }
  123. const handleUpdatedFromMarketplace = () => {
  124. onUpdate()
  125. hideUpdateModal()
  126. }
  127. const [isShowPluginInfo, {
  128. setTrue: showPluginInfo,
  129. setFalse: hidePluginInfo,
  130. }] = useBoolean(false)
  131. const [isShowDeleteConfirm, {
  132. setTrue: showDeleteConfirm,
  133. setFalse: hideDeleteConfirm,
  134. }] = useBoolean(false)
  135. const [deleting, {
  136. setTrue: showDeleting,
  137. setFalse: hideDeleting,
  138. }] = useBoolean(false)
  139. const handleDelete = useCallback(async () => {
  140. showDeleting()
  141. const res = await uninstallPlugin(installation_id)
  142. hideDeleting()
  143. if (res.success) {
  144. hideDeleteConfirm()
  145. onUpdate(true)
  146. if (PluginType.model.includes(category))
  147. refreshModelProviders()
  148. if (PluginType.tool.includes(category))
  149. invalidateAllToolProviders()
  150. }
  151. }, [showDeleting, installation_id, hideDeleting, hideDeleteConfirm, onUpdate, category, refreshModelProviders, invalidateAllToolProviders])
  152. // #plugin TODO# used in apps
  153. // const usedInApps = 3
  154. return (
  155. <div className={cn('shrink-0 border-b border-divider-subtle bg-components-panel-bg p-4 pb-3')}>
  156. <div className="flex">
  157. <div className='overflow-hidden rounded-xl border border-components-panel-border-subtle'>
  158. <Icon src={`${API_PREFIX}/workspaces/current/plugin/icon?tenant_id=${tenant_id}&filename=${icon}`} />
  159. </div>
  160. <div className="ml-3 w-0 grow">
  161. <div className="flex h-5 items-center">
  162. <Title title={label[locale]} />
  163. {verified && <RiVerifiedBadgeLine className="ml-0.5 h-4 w-4 shrink-0 text-text-accent" />}
  164. <PluginVersionPicker
  165. disabled={!isFromMarketplace}
  166. isShow={isShow}
  167. onShowChange={setIsShow}
  168. pluginID={plugin_id}
  169. currentVersion={version}
  170. onSelect={(state) => {
  171. setTargetVersion(state)
  172. handleUpdate()
  173. }}
  174. trigger={
  175. <Badge
  176. className={cn(
  177. 'mx-1',
  178. isShow && 'bg-state-base-hover',
  179. (isShow || isFromMarketplace) && 'hover:bg-state-base-hover',
  180. )}
  181. uppercase={false}
  182. text={
  183. <>
  184. <div>{isFromGitHub ? meta!.version : version}</div>
  185. {isFromMarketplace && <RiArrowLeftRightLine className='ml-1 h-3 w-3 text-text-tertiary' />}
  186. </>
  187. }
  188. hasRedCornerMark={hasNewVersion}
  189. />
  190. }
  191. />
  192. {(hasNewVersion || isFromGitHub) && (
  193. <Button variant='secondary-accent' size='small' className='!h-5' onClick={() => {
  194. if (isFromMarketplace) {
  195. setTargetVersion({
  196. version: latest_version,
  197. unique_identifier: latest_unique_identifier,
  198. })
  199. }
  200. handleUpdate()
  201. }}>{t('plugin.detailPanel.operation.update')}</Button>
  202. )}
  203. </div>
  204. <div className='mb-1 flex h-4 items-center justify-between'>
  205. <div className='mt-0.5 flex items-center'>
  206. <OrgInfo
  207. packageNameClassName='w-auto'
  208. orgName={author}
  209. packageName={name}
  210. />
  211. <div className='system-xs-regular ml-1 mr-0.5 text-text-quaternary'>·</div>
  212. {detail.source === PluginSource.marketplace && (
  213. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.marketplace')} >
  214. <div><BoxSparkleFill className='h-3.5 w-3.5 text-text-tertiary hover:text-text-accent' /></div>
  215. </Tooltip>
  216. )}
  217. {detail.source === PluginSource.github && (
  218. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.github')} >
  219. <div><Github className='h-3.5 w-3.5 text-text-secondary hover:text-text-primary' /></div>
  220. </Tooltip>
  221. )}
  222. {detail.source === PluginSource.local && (
  223. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.local')} >
  224. <div><RiHardDrive3Line className='h-3.5 w-3.5 text-text-tertiary' /></div>
  225. </Tooltip>
  226. )}
  227. {detail.source === PluginSource.debugging && (
  228. <Tooltip popupContent={t('plugin.detailPanel.categoryTip.debugging')} >
  229. <div><RiBugLine className='h-3.5 w-3.5 text-text-tertiary hover:text-text-warning' /></div>
  230. </Tooltip>
  231. )}
  232. </div>
  233. </div>
  234. </div>
  235. <div className='flex gap-1'>
  236. <OperationDropdown
  237. source={detail.source}
  238. onInfo={showPluginInfo}
  239. onCheckVersion={handleUpdate}
  240. onRemove={showDeleteConfirm}
  241. detailUrl={detailUrl}
  242. />
  243. <ActionButton onClick={onHide}>
  244. <RiCloseLine className='h-4 w-4' />
  245. </ActionButton>
  246. </div>
  247. </div>
  248. <Description className='mt-3' text={description[locale]} descriptionLineRows={2}></Description>
  249. {isShowPluginInfo && (
  250. <PluginInfo
  251. repository={isFromGitHub ? meta?.repo : ''}
  252. release={version}
  253. packageName={meta?.package || ''}
  254. onHide={hidePluginInfo}
  255. />
  256. )}
  257. {isShowDeleteConfirm && (
  258. <Confirm
  259. isShow
  260. title={t(`${i18nPrefix}.delete`)}
  261. content={
  262. <div>
  263. {t(`${i18nPrefix}.deleteContentLeft`)}<span className='system-md-semibold'>{label[locale]}</span>{t(`${i18nPrefix}.deleteContentRight`)}<br />
  264. {/* {usedInApps > 0 && t(`${i18nPrefix}.usedInApps`, { num: usedInApps })} */}
  265. </div>
  266. }
  267. onCancel={hideDeleteConfirm}
  268. onConfirm={handleDelete}
  269. isLoading={deleting}
  270. isDisabled={deleting}
  271. />
  272. )}
  273. {
  274. isShowUpdateModal && (
  275. <UpdateFromMarketplace
  276. payload={{
  277. category: detail.declaration.category,
  278. originalPackageInfo: {
  279. id: detail.plugin_unique_identifier,
  280. payload: detail.declaration,
  281. },
  282. targetPackageInfo: {
  283. id: targetVersion.unique_identifier,
  284. version: targetVersion.version,
  285. },
  286. }}
  287. onCancel={hideUpdateModal}
  288. onSave={handleUpdatedFromMarketplace}
  289. />
  290. )
  291. }
  292. </div>
  293. )
  294. }
  295. export default DetailHeader