import React, { FormEvent, useMemo, useReducer } from 'react'; import { useDebounce } from 'react-use'; import { css, cx } from 'emotion'; import { Button, Icon, Input, Pagination, stylesFactory, useStyles } from '@grafana/ui'; import { DateTimeInput, GrafanaTheme, LoadingState } from '@grafana/data'; import { LibraryPanelCard } from '../LibraryPanelCard/LibraryPanelCard'; import { LibraryPanelDTO } from '../../types'; import { changePage, changeSearchString, initialLibraryPanelsViewState, libraryPanelsViewReducer } from './reducer'; import { asyncDispatcher, deleteLibraryPanel, searchForLibraryPanels } from './actions'; interface LibraryPanelViewProps { className?: string; onCreateNewPanel?: () => void; children?: (panel: LibraryPanelDTO, i: number) => JSX.Element | JSX.Element[]; onClickCard?: (panel: LibraryPanelDTO) => void; formatDate?: (dateString: DateTimeInput, format?: string) => string; showSecondaryActions?: boolean; currentPanelId?: string; } export const LibraryPanelsView: React.FC = ({ children, className, onCreateNewPanel, onClickCard, formatDate, showSecondaryActions, currentPanelId: currentPanel, }) => { const styles = useStyles(getPanelViewStyles); const [ { libraryPanels, searchString, page, perPage, numberOfPages, loadingState, currentPanelId }, dispatch, ] = useReducer(libraryPanelsViewReducer, { ...initialLibraryPanelsViewState, currentPanelId: currentPanel, }); const asyncDispatch = useMemo(() => asyncDispatcher(dispatch), [dispatch]); useDebounce(() => asyncDispatch(searchForLibraryPanels({ searchString, page, perPage, currentPanelId })), 300, [ searchString, page, asyncDispatch, ]); const onSearchChange = (event: FormEvent) => asyncDispatch(changeSearchString({ searchString: event.currentTarget.value })); const onDelete = ({ uid }: LibraryPanelDTO) => asyncDispatch(deleteLibraryPanel(uid, { searchString, page, perPage })); const onPageChange = (page: number) => asyncDispatch(changePage({ page })); return (
} value={searchString} autoFocus onChange={onSearchChange} /> {/*