Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback } from 'react'
  4. import { type MetaData, PluginSource } from '../types'
  5. import { RiDeleteBinLine, RiInformation2Line, RiLoopLeftLine } from '@remixicon/react'
  6. import { useBoolean } from 'ahooks'
  7. import { useTranslation } from 'react-i18next'
  8. import PluginInfo from '../plugin-page/plugin-info'
  9. import ActionButton from '../../base/action-button'
  10. import Tooltip from '../../base/tooltip'
  11. import Confirm from '../../base/confirm'
  12. import { uninstallPlugin } from '@/service/plugins'
  13. import { useGitHubReleases } from '../install-plugin/hooks'
  14. import Toast from '@/app/components/base/toast'
  15. import { useModalContext } from '@/context/modal-context'
  16. import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
  17. import type { PluginType } from '@/app/components/plugins/types'
  18. const i18nPrefix = 'plugin.action'
  19. type Props = {
  20. author: string
  21. installationId: string
  22. pluginUniqueIdentifier: string
  23. pluginName: string
  24. category: PluginType
  25. usedInApps: number
  26. isShowFetchNewVersion: boolean
  27. isShowInfo: boolean
  28. isShowDelete: boolean
  29. onDelete: () => void
  30. meta?: MetaData
  31. }
  32. const Action: FC<Props> = ({
  33. author,
  34. installationId,
  35. pluginUniqueIdentifier,
  36. pluginName,
  37. category,
  38. isShowFetchNewVersion,
  39. isShowInfo,
  40. isShowDelete,
  41. onDelete,
  42. meta,
  43. }) => {
  44. const { t } = useTranslation()
  45. const [isShowPluginInfo, {
  46. setTrue: showPluginInfo,
  47. setFalse: hidePluginInfo,
  48. }] = useBoolean(false)
  49. const [deleting, {
  50. setTrue: showDeleting,
  51. setFalse: hideDeleting,
  52. }] = useBoolean(false)
  53. const { checkForUpdates, fetchReleases } = useGitHubReleases()
  54. const { setShowUpdatePluginModal } = useModalContext()
  55. const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
  56. const handleFetchNewVersion = async () => {
  57. const owner = meta!.repo.split('/')[0] || author
  58. const repo = meta!.repo.split('/')[1] || pluginName
  59. const fetchedReleases = await fetchReleases(owner, repo)
  60. if (fetchedReleases.length === 0) return
  61. const { needUpdate, toastProps } = checkForUpdates(fetchedReleases, meta!.version)
  62. Toast.notify(toastProps)
  63. if (needUpdate) {
  64. setShowUpdatePluginModal({
  65. onSaveCallback: () => {
  66. invalidateInstalledPluginList()
  67. },
  68. payload: {
  69. type: PluginSource.github,
  70. category,
  71. github: {
  72. originalPackageInfo: {
  73. id: pluginUniqueIdentifier,
  74. repo: meta!.repo,
  75. version: meta!.version,
  76. package: meta!.package,
  77. releases: fetchedReleases,
  78. },
  79. },
  80. },
  81. })
  82. }
  83. }
  84. const [isShowDeleteConfirm, {
  85. setTrue: showDeleteConfirm,
  86. setFalse: hideDeleteConfirm,
  87. }] = useBoolean(false)
  88. const handleDelete = useCallback(async () => {
  89. showDeleting()
  90. const res = await uninstallPlugin(installationId)
  91. hideDeleting()
  92. if (res.success) {
  93. hideDeleteConfirm()
  94. onDelete()
  95. }
  96. }, [installationId, onDelete])
  97. return (
  98. <div className='flex space-x-1'>
  99. {/* Only plugin installed from GitHub need to check if it's the new version */}
  100. {isShowFetchNewVersion
  101. && (
  102. <Tooltip popupContent={t(`${i18nPrefix}.checkForUpdates`)}>
  103. <ActionButton onClick={handleFetchNewVersion}>
  104. <RiLoopLeftLine className='h-4 w-4 text-text-tertiary' />
  105. </ActionButton>
  106. </Tooltip>
  107. )
  108. }
  109. {
  110. isShowInfo
  111. && (
  112. <Tooltip popupContent={t(`${i18nPrefix}.pluginInfo`)}>
  113. <ActionButton onClick={showPluginInfo}>
  114. <RiInformation2Line className='h-4 w-4 text-text-tertiary' />
  115. </ActionButton>
  116. </Tooltip>
  117. )
  118. }
  119. {
  120. isShowDelete
  121. && (
  122. <Tooltip popupContent={t(`${i18nPrefix}.delete`)}>
  123. <ActionButton
  124. className='text-text-tertiary hover:bg-state-destructive-hover hover:text-text-destructive'
  125. onClick={showDeleteConfirm}
  126. >
  127. <RiDeleteBinLine className='h-4 w-4' />
  128. </ActionButton>
  129. </Tooltip>
  130. )
  131. }
  132. {isShowPluginInfo && (
  133. <PluginInfo
  134. repository={meta!.repo}
  135. release={meta!.version}
  136. packageName={meta!.package}
  137. onHide={hidePluginInfo}
  138. />
  139. )}
  140. <Confirm
  141. isShow={isShowDeleteConfirm}
  142. title={t(`${i18nPrefix}.delete`)}
  143. content={
  144. <div>
  145. {t(`${i18nPrefix}.deleteContentLeft`)}<span className='system-md-semibold'>{pluginName}</span>{t(`${i18nPrefix}.deleteContentRight`)}<br />
  146. {/* // todo: add usedInApps */}
  147. {/* {usedInApps > 0 && t(`${i18nPrefix}.usedInApps`, { num: usedInApps })} */}
  148. </div>
  149. }
  150. onCancel={hideDeleteConfirm}
  151. onConfirm={handleDelete}
  152. isLoading={deleting}
  153. isDisabled={deleting}
  154. />
  155. </div>
  156. )
  157. }
  158. export default React.memo(Action)