import { css } from '@emotion/css'; import React, { FC, useCallback, useState } from 'react'; import { useDispatch } from 'react-redux'; import { GrafanaTheme2, PanelPluginMeta } from '@grafana/data'; import { Button, useStyles2, VerticalGroup } from '@grafana/ui'; import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv'; import { PanelModel } from 'app/features/dashboard/state'; import { changeToLibraryPanel } from 'app/features/panel/state/actions'; import { PanelTypeFilter } from '../../../../core/components/PanelTypeFilter/PanelTypeFilter'; import { LibraryElementDTO } from '../../types'; import { AddLibraryPanelModal } from '../AddLibraryPanelModal/AddLibraryPanelModal'; import { ChangeLibraryPanelModal } from '../ChangeLibraryPanelModal/ChangeLibraryPanelModal'; import { LibraryPanelsView } from '../LibraryPanelsView/LibraryPanelsView'; interface Props { panel: PanelModel; searchQuery: string; } export const PanelLibraryOptionsGroup: FC = ({ panel, searchQuery }) => { const styles = useStyles2(getStyles); const [showingAddPanelModal, setShowingAddPanelModal] = useState(false); const [changeToPanel, setChangeToPanel] = useState(undefined); const [panelFilter, setPanelFilter] = useState([]); const onPanelFilterChange = useCallback( (plugins: PanelPluginMeta[]) => { setPanelFilter(plugins.map((p) => p.id)); }, [setPanelFilter] ); const dashboard = getDashboardSrv().getCurrent(); const dispatch = useDispatch(); const useLibraryPanel = async () => { if (!changeToPanel) { return; } setChangeToPanel(undefined); dispatch(changeToLibraryPanel(panel, changeToPanel)); }; const onAddToPanelLibrary = () => { setShowingAddPanelModal(true); }; const onChangeLibraryPanel = (panel: LibraryElementDTO) => { setChangeToPanel(panel); }; const onDismissChangeToPanel = () => { setChangeToPanel(undefined); }; return ( {!panel.libraryPanel && ( )}
{showingAddPanelModal && ( setShowingAddPanelModal(false)} initialFolderId={dashboard?.meta.folderId} isOpen={showingAddPanelModal} /> )} {changeToPanel && ( )}
); }; const getStyles = (theme: GrafanaTheme2) => { return { libraryPanelsView: css` width: 100%; `, }; };