import React, { FC, useEffect, useMemo, useReducer } from 'react'; import { LoadingState } from '@grafana/data'; import { Button, Modal, useStyles } from '@grafana/ui'; import { getModalStyles } from '../../styles'; import { LibraryElementDTO } from '../../types'; import { asyncDispatcher } from '../LibraryPanelsView/actions'; import { getConnectedDashboards } from './actions'; import { deleteLibraryPanelModalReducer, initialDeleteLibraryPanelModalState } from './reducer'; interface Props { libraryPanel: LibraryElementDTO; onConfirm: () => void; onDismiss: () => void; } export const DeleteLibraryPanelModal: FC = ({ libraryPanel, onDismiss, onConfirm }) => { const styles = useStyles(getModalStyles); const [{ dashboardTitles, loadingState }, dispatch] = useReducer( deleteLibraryPanelModalReducer, initialDeleteLibraryPanelModalState ); const asyncDispatch = useMemo(() => asyncDispatcher(dispatch), [dispatch]); useEffect(() => { asyncDispatch(getConnectedDashboards(libraryPanel)); }, [asyncDispatch, libraryPanel]); const connected = Boolean(dashboardTitles.length); const done = loadingState === LoadingState.Done; return ( {!done ? : null} {done ? (
{connected ? : null} {!connected ? : null}
) : null}
); }; const LoadingIndicator: FC = () => Loading library panel...; const Confirm: FC = () => { const styles = useStyles(getModalStyles); return
Do you want to delete this panel?
; }; const HasConnectedDashboards: FC<{ dashboardTitles: string[] }> = ({ dashboardTitles }) => { const styles = useStyles(getModalStyles); const suffix = dashboardTitles.length === 1 ? 'dashboard.' : 'dashboards.'; const message = `${dashboardTitles.length} ${suffix}`; if (dashboardTitles.length === 0) { return null; } return (

{'This library panel can not be deleted because it is connected to '} {message} {' Remove the library panel from the dashboards listed below and retry.'}

{dashboardTitles.map((title, i) => ( ))}
Dashboard name
{title}
); };