action.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // eslint-disable-next-line react-hooks/exhaustive-deps
  97. }, [installationId, onDelete])
  98. return (
  99. <div className='flex space-x-1'>
  100. {/* Only plugin installed from GitHub need to check if it's the new version */}
  101. {isShowFetchNewVersion
  102. && (
  103. <Tooltip popupContent={t(`${i18nPrefix}.checkForUpdates`)}>
  104. <ActionButton onClick={handleFetchNewVersion}>
  105. <RiLoopLeftLine className='h-4 w-4 text-text-tertiary' />
  106. </ActionButton>
  107. </Tooltip>
  108. )
  109. }
  110. {
  111. isShowInfo
  112. && (
  113. <Tooltip popupContent={t(`${i18nPrefix}.pluginInfo`)}>
  114. <ActionButton onClick={showPluginInfo}>
  115. <RiInformation2Line className='h-4 w-4 text-text-tertiary' />
  116. </ActionButton>
  117. </Tooltip>
  118. )
  119. }
  120. {
  121. isShowDelete
  122. && (
  123. <Tooltip popupContent={t(`${i18nPrefix}.delete`)}>
  124. <ActionButton
  125. className='text-text-tertiary hover:bg-state-destructive-hover hover:text-text-destructive'
  126. onClick={showDeleteConfirm}
  127. >
  128. <RiDeleteBinLine className='h-4 w-4' />
  129. </ActionButton>
  130. </Tooltip>
  131. )
  132. }
  133. {isShowPluginInfo && (
  134. <PluginInfo
  135. repository={meta!.repo}
  136. release={meta!.version}
  137. packageName={meta!.package}
  138. onHide={hidePluginInfo}
  139. />
  140. )}
  141. <Confirm
  142. isShow={isShowDeleteConfirm}
  143. title={t(`${i18nPrefix}.delete`)}
  144. content={
  145. <div>
  146. {t(`${i18nPrefix}.deleteContentLeft`)}<span className='system-md-semibold'>{pluginName}</span>{t(`${i18nPrefix}.deleteContentRight`)}<br />
  147. {/* // todo: add usedInApps */}
  148. {/* {usedInApps > 0 && t(`${i18nPrefix}.usedInApps`, { num: usedInApps })} */}
  149. </div>
  150. }
  151. onCancel={hideDeleteConfirm}
  152. onConfirm={handleDelete}
  153. isLoading={deleting}
  154. isDisabled={deleting}
  155. />
  156. </div>
  157. )
  158. }
  159. export default React.memo(Action)