grafana/public/app/features/plugins/admin/components/InstallControls/InstallControlsButton.tsx
Giuseppe Guerra dde4a03544
Catalog: Display badges for Angular plugins and disable install if Angular is disabled (#69084)
* Angular deprecation: Add Angular badge in plugin catalog page

* Angular deprecation: Add alert in plugin details page

* Angular deprecation: Disable install button if for Angular plugins

* removed extra console.log

* Add tests for Angular badge

* Add tests for PluginDetailsAngularDeprecation

* Add tests for InstallControlsButton

* Add tests for ExternallyManagedButton

* Table tests

* Catalog: Update angular deprecation message

* PR review feedback

* Update tests

* Update copy for angular tooltip and alert

* Update tests

* Fix test warnings

* Fix angularDetected not being set for remote catalog plugins

* Dynamic alert text based on grafana config

* Moved deprecation message to a separate function

* Removed unused Props in PluginAngularBadge
2023-06-28 09:58:45 +02:00

132 lines
4.7 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { AppEvents } from '@grafana/data';
import { config, locationService } from '@grafana/runtime';
import { Button, HorizontalGroup, ConfirmModal } from '@grafana/ui';
import appEvents from 'app/core/app_events';
import { useQueryParams } from 'app/core/hooks/useQueryParams';
import { removePluginFromNavTree } from 'app/core/reducers/navBarTree';
import { useDispatch } from 'app/types';
import { useInstallStatus, useUninstallStatus, useInstall, useUninstall, useUnsetInstall } from '../../state/hooks';
import { trackPluginInstalled, trackPluginUninstalled } from '../../tracking';
import { CatalogPlugin, PluginStatus, PluginTabIds, Version } from '../../types';
type InstallControlsButtonProps = {
plugin: CatalogPlugin;
pluginStatus: PluginStatus;
latestCompatibleVersion?: Version;
setNeedReload?: (needReload: boolean) => void;
};
export function InstallControlsButton({
plugin,
pluginStatus,
latestCompatibleVersion,
setNeedReload,
}: InstallControlsButtonProps) {
const dispatch = useDispatch();
const [queryParams] = useQueryParams();
const location = useLocation();
const { isInstalling, error: errorInstalling } = useInstallStatus();
const { isUninstalling, error: errorUninstalling } = useUninstallStatus();
const install = useInstall();
const uninstall = useUninstall();
const unsetInstall = useUnsetInstall();
const [isConfirmModalVisible, setIsConfirmModalVisible] = useState(false);
const showConfirmModal = () => setIsConfirmModalVisible(true);
const hideConfirmModal = () => setIsConfirmModalVisible(false);
const uninstallBtnText = isUninstalling ? 'Uninstalling' : 'Uninstall';
const trackingProps = {
plugin_id: plugin.id,
plugin_type: plugin.type,
path: location.pathname,
};
useEffect(() => {
return () => {
// Remove possible installation errors
unsetInstall();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const onInstall = async () => {
trackPluginInstalled(trackingProps);
const result = await install(plugin.id, latestCompatibleVersion?.version);
if (!errorInstalling && !('error' in result)) {
appEvents.emit(AppEvents.alertSuccess, [`Installed ${plugin.name}`]);
if (plugin.type === 'app') {
setNeedReload?.(true);
}
}
};
const onUninstall = async () => {
hideConfirmModal();
trackPluginUninstalled(trackingProps);
await uninstall(plugin.id);
if (!errorUninstalling) {
// If an app plugin is uninstalled we need to reset the active tab when the config / dashboards tabs are removed.
const activePageId = queryParams.page;
const isViewingAppConfigPage = activePageId !== PluginTabIds.OVERVIEW && activePageId !== PluginTabIds.VERSIONS;
if (isViewingAppConfigPage) {
locationService.replace(`${location.pathname}?page=${PluginTabIds.OVERVIEW}`);
}
appEvents.emit(AppEvents.alertSuccess, [`Uninstalled ${plugin.name}`]);
if (plugin.type === 'app') {
dispatch(removePluginFromNavTree({ pluginID: plugin.id }));
setNeedReload?.(false);
}
}
};
const onUpdate = async () => {
await install(plugin.id, latestCompatibleVersion?.version, true);
if (!errorInstalling) {
appEvents.emit(AppEvents.alertSuccess, [`Updated ${plugin.name}`]);
}
};
if (pluginStatus === PluginStatus.UNINSTALL) {
return (
<>
<ConfirmModal
isOpen={isConfirmModalVisible}
title={`Uninstall ${plugin.name}`}
body="Are you sure you want to uninstall this plugin?"
confirmText="Confirm"
icon="exclamation-triangle"
onConfirm={onUninstall}
onDismiss={hideConfirmModal}
/>
<HorizontalGroup align="flex-start" width="auto" height="auto">
<Button variant="destructive" disabled={isUninstalling} onClick={showConfirmModal}>
{uninstallBtnText}
</Button>
</HorizontalGroup>
</>
);
}
if (pluginStatus === PluginStatus.UPDATE) {
return (
<HorizontalGroup align="flex-start" width="auto" height="auto">
<Button disabled={isInstalling} onClick={onUpdate}>
{isInstalling ? 'Updating' : 'Update'}
</Button>
<Button variant="destructive" disabled={isUninstalling} onClick={onUninstall}>
{uninstallBtnText}
</Button>
</HorizontalGroup>
);
}
const shouldDisable = isInstalling || errorInstalling || (!config.angularSupportEnabled && plugin.angularDetected);
return (
<Button disabled={shouldDisable} onClick={onInstall}>
{isInstalling ? 'Installing' : 'Install'}
</Button>
);
}