import { useCallback, useMemo, useState, } from 'react' import { RiCheckboxCircleFill, RiErrorWarningFill, RiInstallLine, RiLoaderLine, } from '@remixicon/react' import { useTranslation } from 'react-i18next' import { usePluginTaskStatus } from './hooks' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import Button from '@/app/components/base/button' import ProgressCircle from '@/app/components/base/progress-bar/progress-circle' import CardIcon from '@/app/components/plugins/card/base/card-icon' import cn from '@/utils/classnames' import { useGetLanguage } from '@/context/i18n' import useGetIcon from '@/app/components/plugins/install-plugin/base/use-get-icon' import DownloadingIcon from '@/app/components/header/plugins-nav/downloading-icon' import Tooltip from '@/app/components/base/tooltip' const PluginTasks = () => { const { t } = useTranslation() const language = useGetLanguage() const [open, setOpen] = useState(false) const { errorPlugins, successPlugins, runningPlugins, runningPluginsLength, successPluginsLength, errorPluginsLength, totalPluginsLength, isInstalling, isInstallingWithSuccess, isInstallingWithError, isSuccess, isFailed, handleClearErrorPlugin, } = usePluginTaskStatus() const { getIconUrl } = useGetIcon() const handleClearAllWithModal = useCallback(async () => { // Clear all completed plugins (success and error) but keep running ones const completedPlugins = [...successPlugins, ...errorPlugins] // Clear all completed plugins individually for (const plugin of completedPlugins) await handleClearErrorPlugin(plugin.taskId, plugin.plugin_unique_identifier) // Only close modal if no plugins are still installing if (runningPluginsLength === 0) setOpen(false) }, [successPlugins, errorPlugins, handleClearErrorPlugin, runningPluginsLength]) const handleClearErrorsWithModal = useCallback(async () => { // Clear only error plugins, not all plugins for (const plugin of errorPlugins) await handleClearErrorPlugin(plugin.taskId, plugin.plugin_unique_identifier) // Only close modal if no plugins are still installing if (runningPluginsLength === 0) setOpen(false) }, [errorPlugins, handleClearErrorPlugin, runningPluginsLength]) const handleClearSingleWithModal = useCallback(async (taskId: string, pluginId: string) => { await handleClearErrorPlugin(taskId, pluginId) // Only close modal if no plugins are still installing if (runningPluginsLength === 0) setOpen(false) }, [handleClearErrorPlugin, runningPluginsLength]) const tip = useMemo(() => { if (isInstallingWithError) return t('plugin.task.installingWithError', { installingLength: runningPluginsLength, successLength: successPluginsLength, errorLength: errorPluginsLength }) if (isInstallingWithSuccess) return t('plugin.task.installingWithSuccess', { installingLength: runningPluginsLength, successLength: successPluginsLength }) if (isInstalling) return t('plugin.task.installing') if (isFailed) return t('plugin.task.installedError', { errorLength: errorPluginsLength }) if (isSuccess) return t('plugin.task.installSuccess', { successLength: successPluginsLength }) return t('plugin.task.installed') }, [ errorPluginsLength, isFailed, isInstalling, isInstallingWithError, isInstallingWithSuccess, isSuccess, runningPluginsLength, successPluginsLength, t, ]) // Show icon if there are any plugin tasks (completed, running, or failed) // Only hide when there are absolutely no plugin tasks if (totalPluginsLength === 0) return null return (
{ if (isFailed || isInstalling || isInstallingWithSuccess || isInstallingWithError || isSuccess) setOpen(v => !v) }} >
{ (isInstalling || isInstallingWithError) && ( ) } { !(isInstalling || isInstallingWithError) && ( ) }
{ (isInstalling || isInstallingWithSuccess) && ( ) } { isInstallingWithError && ( ) } { (isSuccess || (successPluginsLength > 0 && runningPluginsLength === 0 && errorPluginsLength === 0)) && ( ) } { isFailed && ( ) }
{/* Running Plugins */} {runningPlugins.length > 0 && ( <>
{t('plugin.task.installing')} ({runningPlugins.length})
{runningPlugins.map(runningPlugin => (
{runningPlugin.labels[language]}
{t('plugin.task.installing')}
))}
)} {/* Success Plugins */} {successPlugins.length > 0 && ( <>
{t('plugin.task.installed')} ({successPlugins.length})
{successPlugins.map(successPlugin => (
{successPlugin.labels[language]}
{successPlugin.message || t('plugin.task.installed')}
))}
)} {/* Error Plugins */} {errorPlugins.length > 0 && ( <>
{t('plugin.task.installError', { errorLength: errorPlugins.length })}
{errorPlugins.map(errorPlugin => (
{errorPlugin.labels[language]}
{errorPlugin.message}
))}
)}
) } export default PluginTasks