PanelLibraryOptionsGroup.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { css } from '@emotion/css';
  2. import React, { FC, useCallback, useState } from 'react';
  3. import { useDispatch } from 'react-redux';
  4. import { GrafanaTheme2, PanelPluginMeta } from '@grafana/data';
  5. import { Button, useStyles2, VerticalGroup } from '@grafana/ui';
  6. import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
  7. import { PanelModel } from 'app/features/dashboard/state';
  8. import { changeToLibraryPanel } from 'app/features/panel/state/actions';
  9. import { PanelTypeFilter } from '../../../../core/components/PanelTypeFilter/PanelTypeFilter';
  10. import { LibraryElementDTO } from '../../types';
  11. import { AddLibraryPanelModal } from '../AddLibraryPanelModal/AddLibraryPanelModal';
  12. import { ChangeLibraryPanelModal } from '../ChangeLibraryPanelModal/ChangeLibraryPanelModal';
  13. import { LibraryPanelsView } from '../LibraryPanelsView/LibraryPanelsView';
  14. interface Props {
  15. panel: PanelModel;
  16. searchQuery: string;
  17. }
  18. export const PanelLibraryOptionsGroup: FC<Props> = ({ panel, searchQuery }) => {
  19. const styles = useStyles2(getStyles);
  20. const [showingAddPanelModal, setShowingAddPanelModal] = useState(false);
  21. const [changeToPanel, setChangeToPanel] = useState<LibraryElementDTO | undefined>(undefined);
  22. const [panelFilter, setPanelFilter] = useState<string[]>([]);
  23. const onPanelFilterChange = useCallback(
  24. (plugins: PanelPluginMeta[]) => {
  25. setPanelFilter(plugins.map((p) => p.id));
  26. },
  27. [setPanelFilter]
  28. );
  29. const dashboard = getDashboardSrv().getCurrent();
  30. const dispatch = useDispatch();
  31. const useLibraryPanel = async () => {
  32. if (!changeToPanel) {
  33. return;
  34. }
  35. setChangeToPanel(undefined);
  36. dispatch(changeToLibraryPanel(panel, changeToPanel));
  37. };
  38. const onAddToPanelLibrary = () => {
  39. setShowingAddPanelModal(true);
  40. };
  41. const onChangeLibraryPanel = (panel: LibraryElementDTO) => {
  42. setChangeToPanel(panel);
  43. };
  44. const onDismissChangeToPanel = () => {
  45. setChangeToPanel(undefined);
  46. };
  47. return (
  48. <VerticalGroup spacing="md">
  49. {!panel.libraryPanel && (
  50. <VerticalGroup align="center">
  51. <Button icon="plus" onClick={onAddToPanelLibrary} variant="secondary" fullWidth>
  52. Create new library panel
  53. </Button>
  54. </VerticalGroup>
  55. )}
  56. <PanelTypeFilter onChange={onPanelFilterChange} />
  57. <div className={styles.libraryPanelsView}>
  58. <LibraryPanelsView
  59. currentPanelId={panel.libraryPanel?.uid}
  60. searchString={searchQuery}
  61. panelFilter={panelFilter}
  62. onClickCard={onChangeLibraryPanel}
  63. showSecondaryActions
  64. />
  65. </div>
  66. {showingAddPanelModal && (
  67. <AddLibraryPanelModal
  68. panel={panel}
  69. onDismiss={() => setShowingAddPanelModal(false)}
  70. initialFolderId={dashboard?.meta.folderId}
  71. isOpen={showingAddPanelModal}
  72. />
  73. )}
  74. {changeToPanel && (
  75. <ChangeLibraryPanelModal panel={panel} onDismiss={onDismissChangeToPanel} onConfirm={useLibraryPanel} />
  76. )}
  77. </VerticalGroup>
  78. );
  79. };
  80. const getStyles = (theme: GrafanaTheme2) => {
  81. return {
  82. libraryPanelsView: css`
  83. width: 100%;
  84. `,
  85. };
  86. };