import { useCallback , useMemo } from 'react' ; import { EuiDataGridColumn } from '@elastic/eui' ; import useUpdateEffect from 'react-use/lib/useUpdateEffect' ; import { useDataGrid } from '../../../../../components/data_grid' ; import { getDefaultExplorationPageUrlState , useExplorationUrlState , } from '../../hooks/use_exploration_url_state' ; import { INIT_MAX_COLUMNS } from '../../../../../components/data_grid/common' ; export const useExplorationDataGrid = ( columns , defaultVisibleColumnsCount = INIT_MAX_COLUMNS , defaultVisibleColumnsFilter ? ) => { const [ pageUrlState , setPageUrlState ] = useExplorationUrlState ( ) ; const dataGrid = useDataGrid ( columns , 25 , defaultVisibleColumnsCount , defaultVisibleColumnsFilter ) ; dataGrid . pagination = useMemo ( ( ) => ( { pageSize : pageUrlState . pageSize , pageIndex : pageUrlState . pageIndex , } ) , [ pageUrlState . pageSize , pageUrlState . pageIndex ] ) ; dataGrid . setPagination = useCallback ( ( u ) => { setPageUrlState ( { ... u } ) ; } , [ setPageUrlState ] ) ; dataGrid . onChangePage = useCallback ( ( pageIndex ) => { setPageUrlState ( { pageIndex } ) ; } , [ setPageUrlState ] ) ; dataGrid . onChangeItemsPerPage = useCallback ( ( pageSize ) => { setPageUrlState ( { pageSize } ) ; } , [ setPageUrlState ] ) ; dataGrid . resetPagination = useCallback ( ( ) => { const a = getDefaultExplorationPageUrlState ( ) ; setPageUrlState ( { pageSize : a . pageSize , pageIndex : a . pageIndex } ) ; } , [ setPageUrlState ] ) ; useUpdateEffect ( function resetPaginationOnQueryChange ( ) { dataGrid . resetPagination ( ) ; } , [ pageUrlState . queryText ] ) ; return dataGrid ; } ;