InstallControlsButton.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React, { useState } from 'react';
  2. import { AppEvents } from '@grafana/data';
  3. import { Button, HorizontalGroup, ConfirmModal } from '@grafana/ui';
  4. import appEvents from 'app/core/app_events';
  5. import { useInstallStatus, useUninstallStatus, useInstall, useUninstall } from '../../state/hooks';
  6. import { CatalogPlugin, PluginStatus, Version } from '../../types';
  7. type InstallControlsButtonProps = {
  8. plugin: CatalogPlugin;
  9. pluginStatus: PluginStatus;
  10. latestCompatibleVersion?: Version;
  11. };
  12. export function InstallControlsButton({ plugin, pluginStatus, latestCompatibleVersion }: InstallControlsButtonProps) {
  13. const { isInstalling, error: errorInstalling } = useInstallStatus();
  14. const { isUninstalling, error: errorUninstalling } = useUninstallStatus();
  15. const install = useInstall();
  16. const uninstall = useUninstall();
  17. const [isConfirmModalVisible, setIsConfirmModalVisible] = useState(false);
  18. const showConfirmModal = () => setIsConfirmModalVisible(true);
  19. const hideConfirmModal = () => setIsConfirmModalVisible(false);
  20. const uninstallBtnText = isUninstalling ? 'Uninstalling' : 'Uninstall';
  21. const onInstall = async () => {
  22. await install(plugin.id, latestCompatibleVersion?.version);
  23. if (!errorInstalling) {
  24. appEvents.emit(AppEvents.alertSuccess, [`Installed ${plugin.name}`]);
  25. }
  26. };
  27. const onUninstall = async () => {
  28. hideConfirmModal();
  29. await uninstall(plugin.id);
  30. if (!errorUninstalling) {
  31. appEvents.emit(AppEvents.alertSuccess, [`Uninstalled ${plugin.name}`]);
  32. }
  33. };
  34. const onUpdate = async () => {
  35. await install(plugin.id, latestCompatibleVersion?.version, true);
  36. if (!errorInstalling) {
  37. appEvents.emit(AppEvents.alertSuccess, [`Updated ${plugin.name}`]);
  38. }
  39. };
  40. if (pluginStatus === PluginStatus.UNINSTALL) {
  41. return (
  42. <>
  43. <ConfirmModal
  44. isOpen={isConfirmModalVisible}
  45. title={`Uninstall ${plugin.name}`}
  46. body="Are you sure you want to uninstall this plugin?"
  47. confirmText="Confirm"
  48. icon="exclamation-triangle"
  49. onConfirm={onUninstall}
  50. onDismiss={hideConfirmModal}
  51. />
  52. <HorizontalGroup height="auto">
  53. <Button variant="destructive" disabled={isUninstalling} onClick={showConfirmModal}>
  54. {uninstallBtnText}
  55. </Button>
  56. </HorizontalGroup>
  57. </>
  58. );
  59. }
  60. if (pluginStatus === PluginStatus.UPDATE) {
  61. return (
  62. <HorizontalGroup height="auto">
  63. <Button disabled={isInstalling} onClick={onUpdate}>
  64. {isInstalling ? 'Updating' : 'Update'}
  65. </Button>
  66. <Button variant="destructive" disabled={isUninstalling} onClick={onUninstall}>
  67. {uninstallBtnText}
  68. </Button>
  69. </HorizontalGroup>
  70. );
  71. }
  72. return (
  73. <Button disabled={isInstalling} onClick={onInstall}>
  74. {isInstalling ? 'Installing' : 'Install'}
  75. </Button>
  76. );
  77. }