import React, { useCallback, useState } from 'react'; import { useAsync, useDebounce } from 'react-use'; import { Button, Icon, Input, Modal, useStyles } from '@grafana/ui'; import { getConnectedDashboards } from '../../state/api'; import { getModalStyles } from '../../styles'; import { PanelModelWithLibraryPanel } from '../../types'; import { usePanelSave } from '../../utils/usePanelSave'; interface Props { panel: PanelModelWithLibraryPanel; folderId: number; isUnsavedPrompt?: boolean; onConfirm: () => void; onDismiss: () => void; onDiscard: () => void; } export const SaveLibraryPanelModal: React.FC = ({ panel, folderId, isUnsavedPrompt, onDismiss, onConfirm, onDiscard, }) => { const [searchString, setSearchString] = useState(''); const dashState = useAsync(async () => { const searchHits = await getConnectedDashboards(panel.libraryPanel.uid); if (searchHits.length > 0) { return searchHits.map((dash) => dash.title); } return []; }, [panel.libraryPanel.uid]); const [filteredDashboards, setFilteredDashboards] = useState([]); useDebounce( () => { if (!dashState.value) { return setFilteredDashboards([]); } return setFilteredDashboards( dashState.value.filter((dashName) => dashName.toLowerCase().includes(searchString.toLowerCase())) ); }, 300, [dashState.value, searchString] ); const { saveLibraryPanel } = usePanelSave(); const styles = useStyles(getModalStyles); const discardAndClose = useCallback(() => { onDiscard(); }, [onDiscard]); const title = isUnsavedPrompt ? 'Unsaved library panel changes' : 'Save library panel'; return (

{'This update will affect '} {panel.libraryPanel.meta.connectedDashboards}{' '} {panel.libraryPanel.meta.connectedDashboards === 1 ? 'dashboard' : 'dashboards'}. The following dashboards using the panel will be affected:

} placeholder="Search affected dashboards" value={searchString} onChange={(e) => setSearchString(e.currentTarget.value)} /> {dashState.loading ? (

Loading connected dashboards...

) : ( {filteredDashboards.map((dashName, i) => ( ))}
Dashboard name
{dashName}
)} {isUnsavedPrompt && ( )}
); };