import { css } from '@emotion/css'; import { useMemo, useReducer } from 'react'; import { useDebounce } from 'react-use'; import { GrafanaTheme2, LoadingState } from '@grafana/data'; import { EmptyState, Pagination, Stack, TextLink, useStyles2 } from '@grafana/ui'; import { Trans, t } from 'app/core/internationalization'; import { LibraryElementDTO } from '../../types'; import { LibraryPanelCard } from '../LibraryPanelCard/LibraryPanelCard'; import { asyncDispatcher, deleteLibraryPanel, searchForLibraryPanels } from './actions'; import { changePage, initialLibraryPanelsViewState, libraryPanelsViewReducer } from './reducer'; interface LibraryPanelViewProps { onClickCard: (panel: LibraryElementDTO) => void; showSecondaryActions?: boolean; currentPanelId?: string; searchString: string; sortDirection?: string; panelFilter?: string[]; folderFilter?: string[]; perPage?: number; } export const LibraryPanelsView = ({ onClickCard, searchString, sortDirection, panelFilter, folderFilter, showSecondaryActions, currentPanelId: currentPanel, perPage: propsPerPage = 40, }: LibraryPanelViewProps) => { const styles = useStyles2(getPanelViewStyles); const [{ libraryPanels, page, perPage, numberOfPages, loadingState, currentPanelId }, dispatch] = useReducer( libraryPanelsViewReducer, { ...initialLibraryPanelsViewState, currentPanelId: currentPanel, perPage: propsPerPage, } ); const asyncDispatch = useMemo(() => asyncDispatcher(dispatch), [dispatch]); useDebounce( () => asyncDispatch( searchForLibraryPanels({ searchString, sortDirection, panelFilter, folderFilterUIDs: folderFilter, page, perPage, currentPanelId, }) ), 300, [searchString, sortDirection, panelFilter, folderFilter, page, asyncDispatch] ); const onDelete = ({ uid }: LibraryElementDTO) => asyncDispatch( deleteLibraryPanel(uid, { searchString, sortDirection, panelFilter, folderFilterUIDs: folderFilter, page, perPage, }) ); const onPageChange = (page: number) => asyncDispatch(changePage({ page })); const hasFilter = searchString || panelFilter?.length || folderFilter?.length; if (!hasFilter && loadingState === LoadingState.Done && libraryPanels.length < 1) { return ( Create a library panel from any existing dashboard panel through the panel context menu.{' '} Learn more ); } return ( {loadingState === LoadingState.Loading ? ( <> ) : libraryPanels.length < 1 ? ( ) : ( libraryPanels?.map((item, i) => ( )) )} {libraryPanels.length ? (
) : null}
); }; const getPanelViewStyles = (theme: GrafanaTheme2) => { return { pagination: css({ alignSelf: 'center', marginTop: theme.spacing(1), }), }; };