The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/library-panels/components/PanelLibraryOptionsGroup/PanelLibraryOptionsGroup.tsx

91 lines
2.8 KiB

import React, { useState } from 'react';
import { css } from 'emotion';
import { GrafanaTheme } from '@grafana/data';
import { Button, stylesFactory, 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';
interface Props {
panel: PanelModel;
searchQuery: string;
}
export const PanelLibraryOptionsGroup: React.FC<Props> = ({ panel, searchQuery }) => {
const styles = useStyles(getStyles);
const [showingAddPanelModal, setShowingAddPanelModal] = useState(false);
const dashboard = getDashboardSrv().getCurrent();
const dispatch = useDispatch();
const useLibraryPanel = async (panelInfo: LibraryPanelDTO) => {
const panelTypeChanged = panel.type !== panelInfo.model.type;
if (panelTypeChanged) {
await dispatch(changePanelPlugin(panel, panelInfo.model.type));
}
panel.restoreModel({
...panelInfo.model,
gridPos: panel.gridPos,
id: panel.id,
libraryPanel: toPanelModelLibraryPanel(panelInfo),
});
panel.hasChanged = false;
panel.refresh();
panel.events.publish(new PanelQueriesChangedEvent());
panel.events.publish(new PanelOptionsChangedEvent());
};
const onAddToPanelLibrary = () => {
setShowingAddPanelModal(true);
};
return (
<div className={styles.box}>
{!panel.libraryPanel && (
<div className={styles.addButtonWrapper}>
<Button icon="plus" onClick={onAddToPanelLibrary} variant="secondary" fullWidth>
Add current panel to library
</Button>
</div>
)}
<LibraryPanelsView
currentPanelId={panel.libraryPanel?.uid}
searchString={searchQuery}
onClickCard={useLibraryPanel}
showSecondaryActions
/>
{showingAddPanelModal && (
<AddLibraryPanelModal
panel={panel}
onDismiss={() => setShowingAddPanelModal(false)}
initialFolderId={dashboard.meta.folderId}
isOpen={showingAddPanelModal}
/>
)}
</div>
);
};
const getStyles = stylesFactory((theme: GrafanaTheme) => {
return {
box: css``,
addButtonWrapper: css`
padding-bottom: ${theme.spacing.md};
text-align: center;
`,
panelLibraryTitle: css`
display: flex;
gap: 10px;
`,
};
});