diff --git a/public/app/features/library-panels/components/ChangeLibraryPanelModal/ChangeLibraryPanelModal.tsx b/public/app/features/library-panels/components/ChangeLibraryPanelModal/ChangeLibraryPanelModal.tsx new file mode 100644 index 00000000000..af0053aba36 --- /dev/null +++ b/public/app/features/library-panels/components/ChangeLibraryPanelModal/ChangeLibraryPanelModal.tsx @@ -0,0 +1,28 @@ +import React from 'react'; +import { ConfirmModal } from '@grafana/ui'; + +import { PanelModel } from '../../../dashboard/state'; +import { isPanelModelLibraryPanel } from '../../guard'; + +export interface ChangeLibraryPanelModalProps { + panel: PanelModel; + onConfirm: () => void; + onDismiss: () => void; +} + +export const ChangeLibraryPanelModal = ({ onConfirm, onDismiss, panel }: ChangeLibraryPanelModalProps): JSX.Element => { + const isLibraryPanel = isPanelModelLibraryPanel(panel); + const title = `${isLibraryPanel ? 'Changing' : 'Change to'} library panel`; + const body = `Changing ${isLibraryPanel ? '' : 'to a'} library panel will remove any changes since last save.`; + return ( + + ); +}; diff --git a/public/app/features/library-panels/components/PanelLibraryOptionsGroup/PanelLibraryOptionsGroup.tsx b/public/app/features/library-panels/components/PanelLibraryOptionsGroup/PanelLibraryOptionsGroup.tsx index f84626ff783..abb05f603c0 100644 --- a/public/app/features/library-panels/components/PanelLibraryOptionsGroup/PanelLibraryOptionsGroup.tsx +++ b/public/app/features/library-panels/components/PanelLibraryOptionsGroup/PanelLibraryOptionsGroup.tsx @@ -1,40 +1,48 @@ -import React, { useState } from 'react'; +import React, { FC, useState } from 'react'; +import { useDispatch } from 'react-redux'; import { css } from '@emotion/css'; import { GrafanaTheme } from '@grafana/data'; -import { Button, stylesFactory, useStyles } from '@grafana/ui'; +import { Button, useStyles } from '@grafana/ui'; + import { PanelModel } from 'app/features/dashboard/state'; import { AddLibraryPanelModal } from '../AddLibraryPanelModal/AddLibraryPanelModal'; import { LibraryPanelsView } from '../LibraryPanelsView/LibraryPanelsView'; import { PanelOptionsChangedEvent, PanelQueriesChangedEvent } from 'app/types/events'; import { LibraryPanelDTO } from '../../types'; import { toPanelModelLibraryPanel } from '../../utils'; -import { useDispatch } from 'react-redux'; import { changePanelPlugin } from 'app/features/dashboard/state/actions'; import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv'; +import { ChangeLibraryPanelModal } from '../ChangeLibraryPanelModal/ChangeLibraryPanelModal'; interface Props { panel: PanelModel; searchQuery: string; } -export const PanelLibraryOptionsGroup: React.FC = ({ panel, searchQuery }) => { +export const PanelLibraryOptionsGroup: FC = ({ panel, searchQuery }) => { const styles = useStyles(getStyles); const [showingAddPanelModal, setShowingAddPanelModal] = useState(false); + const [changeToPanel, setChangeToPanel] = useState(undefined); const dashboard = getDashboardSrv().getCurrent(); const dispatch = useDispatch(); - const useLibraryPanel = async (panelInfo: LibraryPanelDTO) => { - const panelTypeChanged = panel.type !== panelInfo.model.type; + const useLibraryPanel = async () => { + if (!changeToPanel) { + return; + } + setChangeToPanel(undefined); + + const panelTypeChanged = panel.type !== changeToPanel.model.type; if (panelTypeChanged) { - await dispatch(changePanelPlugin(panel, panelInfo.model.type)); + await dispatch(changePanelPlugin(panel, changeToPanel.model.type)); } panel.restoreModel({ - ...panelInfo.model, + ...changeToPanel.model, gridPos: panel.gridPos, id: panel.id, - libraryPanel: toPanelModelLibraryPanel(panelInfo), + libraryPanel: toPanelModelLibraryPanel(changeToPanel), }); panel.hasChanged = false; @@ -47,6 +55,14 @@ export const PanelLibraryOptionsGroup: React.FC = ({ panel, searchQuery } setShowingAddPanelModal(true); }; + const onChangeLibraryPanel = (panel: LibraryPanelDTO) => { + setChangeToPanel(panel); + }; + + const onDismissChangeToPanel = () => { + setChangeToPanel(undefined); + }; + return (
{!panel.libraryPanel && ( @@ -60,7 +76,7 @@ export const PanelLibraryOptionsGroup: React.FC = ({ panel, searchQuery } @@ -72,11 +88,15 @@ export const PanelLibraryOptionsGroup: React.FC = ({ panel, searchQuery } isOpen={showingAddPanelModal} /> )} + + {changeToPanel && ( + + )}
); }; -const getStyles = stylesFactory((theme: GrafanaTheme) => { +const getStyles = (theme: GrafanaTheme) => { return { box: css``, addButtonWrapper: css` @@ -88,4 +108,4 @@ const getStyles = stylesFactory((theme: GrafanaTheme) => { gap: 10px; `, }; -}); +};