DeleteDashboardModal.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { css } from '@emotion/css';
  2. import { sumBy } from 'lodash';
  3. import React from 'react';
  4. import { connect, ConnectedProps } from 'react-redux';
  5. import useAsyncFn from 'react-use/lib/useAsyncFn';
  6. import { Modal, ConfirmModal, Button } from '@grafana/ui';
  7. import { config } from 'app/core/config';
  8. import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
  9. import { cleanUpDashboardAndVariables } from 'app/features/dashboard/state/actions';
  10. import { useDashboardDelete } from './useDashboardDelete';
  11. type DeleteDashboardModalProps = {
  12. hideModal(): void;
  13. dashboard: DashboardModel;
  14. };
  15. const mapDispatchToProps = {
  16. cleanUpDashboardAndVariables,
  17. };
  18. const connector = connect(null, mapDispatchToProps);
  19. type Props = DeleteDashboardModalProps & ConnectedProps<typeof connector>;
  20. const DeleteDashboardModalUnconnected: React.FC<Props> = ({ hideModal, cleanUpDashboardAndVariables, dashboard }) => {
  21. const isProvisioned = dashboard.meta.provisioned;
  22. const { onDeleteDashboard } = useDashboardDelete(dashboard.uid, cleanUpDashboardAndVariables);
  23. const [, onConfirm] = useAsyncFn(async () => {
  24. await onDeleteDashboard();
  25. hideModal();
  26. }, [hideModal]);
  27. const modalBody = getModalBody(dashboard.panels, dashboard.title);
  28. if (isProvisioned) {
  29. return <ProvisionedDeleteModal hideModal={hideModal} provisionedId={dashboard.meta.provisionedExternalId!} />;
  30. }
  31. return (
  32. <ConfirmModal
  33. isOpen={true}
  34. body={modalBody}
  35. onConfirm={onConfirm}
  36. onDismiss={hideModal}
  37. title="Delete"
  38. icon="trash-alt"
  39. confirmText="Delete"
  40. />
  41. );
  42. };
  43. const getModalBody = (panels: PanelModel[], title: string) => {
  44. const totalAlerts = sumBy(panels, (panel) => (panel.alert ? 1 : 0));
  45. return totalAlerts > 0 && !config.unifiedAlertingEnabled ? (
  46. <>
  47. <p>Do you want to delete this dashboard?</p>
  48. <p>
  49. This dashboard contains {totalAlerts} alert{totalAlerts > 1 ? 's' : ''}. Deleting this dashboard also deletes
  50. those alerts.
  51. </p>
  52. </>
  53. ) : (
  54. <>
  55. <p>Do you want to delete this dashboard?</p>
  56. <p>{title}</p>
  57. </>
  58. );
  59. };
  60. const ProvisionedDeleteModal = ({ hideModal, provisionedId }: { hideModal(): void; provisionedId: string }) => (
  61. <Modal
  62. isOpen={true}
  63. title="Cannot delete provisioned dashboard"
  64. icon="trash-alt"
  65. onDismiss={hideModal}
  66. className={css`
  67. width: 500px;
  68. `}
  69. >
  70. <p>
  71. This dashboard is managed by Grafana provisioning and cannot be deleted. Remove the dashboard from the config file
  72. to delete it.
  73. </p>
  74. <p>
  75. <i>
  76. See{' '}
  77. <a
  78. className="external-link"
  79. href="https://grafana.com/docs/grafana/latest/administration/provisioning/#dashboards"
  80. target="_blank"
  81. rel="noreferrer"
  82. >
  83. documentation
  84. </a>{' '}
  85. for more information about provisioning.
  86. </i>
  87. <br />
  88. File path: {provisionedId}
  89. </p>
  90. <Modal.ButtonRow>
  91. <Button variant="primary" onClick={hideModal}>
  92. OK
  93. </Button>
  94. </Modal.ButtonRow>
  95. </Modal>
  96. );
  97. export const DeleteDashboardModal = connector(DeleteDashboardModalUnconnected);