dify/web/app/components/plugins/plugin-item/action.tsx

160 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-10-10 17:47:04 +08:00
'use client'
import type { FC } from 'react'
import React, { useCallback } from 'react'
import { type MetaData, PluginSource } from '../types'
2024-10-10 17:47:04 +08:00
import { RiDeleteBinLine, RiInformation2Line, RiLoopLeftLine } from '@remixicon/react'
2024-10-16 11:30:04 +08:00
import { useBoolean } from 'ahooks'
2024-10-16 15:13:19 +08:00
import { useTranslation } from 'react-i18next'
2024-10-16 11:30:04 +08:00
import PluginInfo from '../plugin-page/plugin-info'
import ActionButton from '../../base/action-button'
2024-10-16 15:13:19 +08:00
import Tooltip from '../../base/tooltip'
import Confirm from '../../base/confirm'
import { uninstallPlugin } from '@/service/plugins'
import { useGitHubReleases } from '../install-plugin/hooks'
import Toast from '@/app/components/base/toast'
import { useModalContext } from '@/context/modal-context'
import { useInvalidateInstalledPluginList } from '@/service/use-plugins'
2024-10-16 15:13:19 +08:00
const i18nPrefix = 'plugin.action'
2024-10-10 17:47:04 +08:00
type Props = {
author: string
installationId: string
pluginUniqueIdentifier: string
2024-10-16 15:13:19 +08:00
pluginName: string
usedInApps: number
2024-10-10 17:47:04 +08:00
isShowFetchNewVersion: boolean
isShowInfo: boolean
isShowDelete: boolean
onDelete: () => void
meta?: MetaData
2024-10-10 17:47:04 +08:00
}
const Action: FC<Props> = ({
author,
installationId,
pluginUniqueIdentifier,
2024-10-16 15:13:19 +08:00
pluginName,
2024-10-10 17:47:04 +08:00
isShowFetchNewVersion,
isShowInfo,
isShowDelete,
onDelete,
meta,
2024-10-10 17:47:04 +08:00
}) => {
2024-10-16 15:13:19 +08:00
const { t } = useTranslation()
2024-10-16 11:30:04 +08:00
const [isShowPluginInfo, {
setTrue: showPluginInfo,
setFalse: hidePluginInfo,
}] = useBoolean(false)
const [deleting, {
setTrue: showDeleting,
setFalse: hideDeleting,
}] = useBoolean(false)
const { checkForUpdates, fetchReleases } = useGitHubReleases()
const { setShowUpdatePluginModal } = useModalContext()
const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
2024-10-10 17:47:04 +08:00
const handleFetchNewVersion = async () => {
const fetchedReleases = await fetchReleases(author, pluginName)
if (fetchedReleases.length === 0) return
const { needUpdate, toastProps } = checkForUpdates(fetchedReleases, meta!.version)
Toast.notify(toastProps)
if (needUpdate) {
setShowUpdatePluginModal({
onSaveCallback: () => {
invalidateInstalledPluginList()
},
payload: {
type: PluginSource.github,
github: {
originalPackageInfo: {
id: pluginUniqueIdentifier,
repo: meta!.repo,
version: meta!.version,
package: meta!.package,
releases: fetchedReleases,
},
},
},
})
}
}
2024-10-16 11:30:04 +08:00
2024-10-16 15:13:19 +08:00
const [isShowDeleteConfirm, {
setTrue: showDeleteConfirm,
setFalse: hideDeleteConfirm,
}] = useBoolean(false)
const handleDelete = useCallback(async () => {
showDeleting()
const res = await uninstallPlugin(installationId)
hideDeleting()
if (res.success) {
hideDeleteConfirm()
onDelete()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [installationId, onDelete])
2024-10-10 17:47:04 +08:00
return (
<div className='flex space-x-1'>
2024-10-16 16:28:07 +08:00
{/* Only plugin installed from GitHub need to check if it's the new version */}
2024-10-10 17:47:04 +08:00
{isShowFetchNewVersion
2024-10-16 15:13:19 +08:00
&& (
<Tooltip popupContent={t(`${i18nPrefix}.checkForUpdates`)}>
<ActionButton onClick={handleFetchNewVersion}>
<RiLoopLeftLine className='w-4 h-4 text-text-tertiary' />
</ActionButton>
</Tooltip>
)
2024-10-10 17:47:04 +08:00
}
{
isShowInfo
2024-10-16 15:13:19 +08:00
&& (
<Tooltip popupContent={t(`${i18nPrefix}.pluginInfo`)}>
<ActionButton onClick={showPluginInfo}>
<RiInformation2Line className='w-4 h-4 text-text-tertiary' />
</ActionButton>
</Tooltip>
)
2024-10-10 17:47:04 +08:00
}
{
isShowDelete
2024-10-16 15:13:19 +08:00
&& (
<Tooltip popupContent={t(`${i18nPrefix}.delete`)}>
<ActionButton
className='hover:bg-state-destructive-hover text-text-tertiary hover:text-text-destructive'
onClick={showDeleteConfirm}
>
<RiDeleteBinLine className='w-4 h-4' />
</ActionButton>
</Tooltip>
)
2024-10-10 17:47:04 +08:00
}
2024-10-16 11:30:04 +08:00
{isShowPluginInfo && (
<PluginInfo
repository={meta!.repo}
release={meta!.version}
packageName={meta!.package}
2024-10-16 11:30:04 +08:00
onHide={hidePluginInfo}
/>
)}
<Confirm
isShow={isShowDeleteConfirm}
title={t(`${i18nPrefix}.delete`)}
content={
<div>
{t(`${i18nPrefix}.deleteContentLeft`)}<span className='system-md-semibold'>{pluginName}</span>{t(`${i18nPrefix}.deleteContentRight`)}<br />
{/* // todo: add usedInApps */}
{/* {usedInApps > 0 && t(`${i18nPrefix}.usedInApps`, { num: usedInApps })} */}
</div>
}
onCancel={hideDeleteConfirm}
onConfirm={handleDelete}
isLoading={deleting}
isDisabled={deleting}
/>
2024-10-10 17:47:04 +08:00
</div>
)
}
export default React.memo(Action)