File size: 1,153 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { usePrevious } from '@wordpress/compose';
import { useEffect, useRef } from 'react';
import type { View } from '@wordpress/dataviews';

// DataViews' pagination always resets when the search component is mounted, even though the search term has not changed.
// This is a bug which has a fix in https://github.com/WordPress/gutenberg/pull/61307.
// This is a workaround until the above fix is released.
// Here, we restore the page to the previous page if it is unintentionally changed by the above bug.
export function useInitializeDataViewsPage< V extends View >(
	dataViewsState: V,
	setDataViewsState: ( state: V ) => void
) {
	const prevPage = usePrevious( dataViewsState.page );
	const prevSearch = usePrevious( dataViewsState.search );

	const done = useRef( false );

	useEffect( () => {
		if ( prevPage === 1 ) {
			done.current = true;
		}
		if ( done.current ) {
			return;
		}

		if ( dataViewsState.search === prevSearch && dataViewsState.page !== prevPage ) {
			setDataViewsState( {
				...dataViewsState,
				page: prevPage,
			} );
			done.current = true;
		}
	}, [ dataViewsState, prevPage, prevSearch, setDataViewsState ] );
}