Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands: Try debouncing search for post-type navigation #58810

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core-commands/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@babel/runtime": "^7.16.0",
"@wordpress/block-editor": "file:../block-editor",
"@wordpress/commands": "file:../commands",
"@wordpress/compose": "file:../compose",
"@wordpress/core-data": "file:../core-data",
"@wordpress/data": "file:../data",
"@wordpress/element": "file:../element",
Expand Down
35 changes: 29 additions & 6 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { useCommandLoader } from '@wordpress/commands';
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { useMemo, useEffect, useState } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import {
Expand All @@ -16,6 +16,7 @@ import {
} from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { getQueryArg, addQueryArgs, getPath } from '@wordpress/url';
import { useDebounce } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -33,17 +34,35 @@ const icons = {
wp_template_part: symbolFilled,
};

function useDebouncedValue( value ) {
const [ debouncedValue, setDebouncedValue ] = useState( '' );
const debounced = useDebounce( setDebouncedValue, 250 );

useEffect( () => {
debounced( value );
return () => debounced.cancel();
}, [ debounced, value ] );

return debouncedValue;
}

const getNavigationCommandLoaderPerPostType = ( postType ) =>
function useNavigationCommandLoader( { search } ) {
const history = useHistory();
const isBlockBasedTheme = useIsBlockBasedTheme();
const delayedSearch = useDebouncedValue( search );
const { records, isLoading } = useSelect(
( select ) => {
const { getEntityRecords } = select( coreStore );
if ( ! delayedSearch ) {
return {
isLoading: false,
};
}

const query = {
search: !! search ? search : undefined,
search: delayedSearch,
per_page: 10,
orderby: search ? 'relevance' : 'date',
orderby: 'relevance',
status: [
'publish',
'future',
Expand All @@ -53,14 +72,18 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
],
};
return {
records: getEntityRecords( 'postType', postType, query ),
records: select( coreStore ).getEntityRecords(
'postType',
postType,
query
),
isLoading: ! select( coreStore ).hasFinishedResolution(
'getEntityRecords',
[ 'postType', postType, query ]
),
};
},
[ search ]
[ delayedSearch ]
);

const commands = useMemo( () => {
Expand Down
Loading