import { css } from '@emotion/css'; import React, { useReducer } from 'react'; import { GrafanaTheme2, PanelPluginMeta, SelectableValue } from '@grafana/data'; import { HorizontalGroup, useStyles2, VerticalGroup, FilterInput } from '@grafana/ui'; import { FolderFilter } from '../../../../core/components/FolderFilter/FolderFilter'; import { PanelTypeFilter } from '../../../../core/components/PanelTypeFilter/PanelTypeFilter'; import { SortPicker } from '../../../../core/components/Select/SortPicker'; import { DEFAULT_PER_PAGE_PAGINATION } from '../../../../core/constants'; import { FolderInfo } from '../../../../types'; import { LibraryElementDTO } from '../../types'; import { LibraryPanelsView } from '../LibraryPanelsView/LibraryPanelsView'; import { folderFilterChanged, initialLibraryPanelsSearchState, libraryPanelsSearchReducer, panelFilterChanged, searchChanged, sortChanged, } from './reducer'; export enum LibraryPanelsSearchVariant { Tight = 'tight', Spacious = 'spacious', } export interface LibraryPanelsSearchProps { onClick: (panel: LibraryElementDTO) => void; variant?: LibraryPanelsSearchVariant; showSort?: boolean; showPanelFilter?: boolean; showFolderFilter?: boolean; showSecondaryActions?: boolean; currentPanelId?: string; currentFolderId?: number; perPage?: number; } export const LibraryPanelsSearch = ({ onClick, variant = LibraryPanelsSearchVariant.Spacious, currentPanelId, currentFolderId, perPage = DEFAULT_PER_PAGE_PAGINATION, showPanelFilter = false, showFolderFilter = false, showSort = false, showSecondaryActions = false, }: LibraryPanelsSearchProps): JSX.Element => { const styles = useStyles2(getStyles); const [{ sortDirection, panelFilter, folderFilter, searchQuery }, dispatch] = useReducer(libraryPanelsSearchReducer, { ...initialLibraryPanelsSearchState, folderFilter: currentFolderId ? [currentFolderId.toString(10)] : [], }); const onFilterChange = (searchString: string) => dispatch(searchChanged(searchString)); const onSortChange = (sorting: SelectableValue) => dispatch(sortChanged(sorting)); const onFolderFilterChange = (folders: FolderInfo[]) => dispatch(folderFilterChanged(folders)); const onPanelFilterChange = (plugins: PanelPluginMeta[]) => dispatch(panelFilterChanged(plugins)); if (variant === LibraryPanelsSearchVariant.Spacious) { return (
{showSort && ( )} {showFolderFilter && } {showPanelFilter && }
); } return (
{showSort && } {showFolderFilter && } {showPanelFilter && }
); }; function getStyles(theme: GrafanaTheme2) { return { container: css` width: 100%; overflow-y: auto; padding: ${theme.spacing(1)}; `, buttonRow: css` display: flex; justify-content: space-between; width: 100%; margin-top: ${theme.spacing(2)}; // Clear types link `, tightButtonRow: css` display: flex; justify-content: space-between; width: 100%; margin-top: ${theme.spacing(4)}; // Clear types link `, tightFilter: css` flex-grow: 1; `, tightSortFilter: css` flex-grow: 1; padding: ${theme.spacing(0, 0, 0, 0.5)}; `, libraryPanelsView: css` width: 100%; `, }; }