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

Edit Site: Move loading logic to a separate hook #50511

Merged
merged 4 commits into from
May 10, 2023
Merged
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
67 changes: 35 additions & 32 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useEffect, useMemo, useRef, useState } from '@wordpress/element';
import { useEffect, useMemo, useState } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { Notice } from '@wordpress/components';
import { EntityProvider, store as coreStore } from '@wordpress/core-data';
Expand Down Expand Up @@ -50,6 +50,39 @@ const interfaceLabels = {
footer: __( 'Editor footer' ),
};

function useIsSiteEditorLoading() {
const { isLoaded: hasLoadedPost } = useEditedEntityRecord();
const { hasResolvingSelectors } = useSelect( ( select ) => {
return {
hasResolvingSelectors: select( coreStore ).hasResolvingSelectors(),
};
} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more optimization opportunity: this useSelect will return a new value every time any selector starts or stops resolving, even long after the editor is fully loaded. That will trigger a rerender of the Editor component that uses the useIsSiteEditorLoading hook.

You can make the return value more stable by including the loaded value as dependency:

const [ loaded, setLoaded ] = useState( false );
const inLoadingPause = useSelect( ( select ) => {
  const hasResolvingSelectors = select( coreStore ).hasResolvingSelectors();
  return ! loaded && ! hasResolvingSelectors;
}, [ loaded ] );

This will guarantee a stable false return value once the loaded state is achieved. No more renderers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll follow up in another PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #50546

const [ loaded, setLoaded ] = useState( false );
const inLoadingPause = ! loaded && ! hasResolvingSelectors;

useEffect( () => {
if ( inLoadingPause ) {
/*
* We're using an arbitrary 1s timeout here to catch brief moments
* without any resolving selectors that would result in displaying
* brief flickers of loading state and loaded state.
*
* It's worth experimenting with different values, since this also
* adds 1s of artificial delay after loading has finished.
*/
const timeout = setTimeout( () => {
setLoaded( true );
}, 1000 );

return () => {
clearTimeout( timeout );
};
}
}, [ inLoadingPause ] );

return ! loaded || ! hasLoadedPost;
}

export default function Editor() {
const {
record: editedPost,
Expand Down Expand Up @@ -153,37 +186,7 @@ export default function Editor() {
// action in <URlQueryController> from double-announcing.
useTitle( hasLoadedPost && title );

const { hasResolvingSelectors } = useSelect( ( select ) => {
return {
hasResolvingSelectors: select( coreStore ).hasResolvingSelectors(),
};
} );
const [ loaded, setLoaded ] = useState( false );
const timeoutRef = useRef( null );

useEffect( () => {
if ( ! hasResolvingSelectors && ! loaded ) {
clearTimeout( timeoutRef.current );

/*
* We're using an arbitrary 1s timeout here to catch brief moments
* without any resolving selectors that would result in displaying
* brief flickers of loading state and loaded state.
*
* It's worth experimenting with different values, since this also
* adds 1s of artificial delay after loading has finished.
*/
timeoutRef.current = setTimeout( () => {
setLoaded( true );
}, 1000 );

return () => {
clearTimeout( timeoutRef.current );
};
}
}, [ loaded, hasResolvingSelectors ] );

const isLoading = ! loaded || ! hasLoadedPost;
const isLoading = useIsSiteEditorLoading();

return (
<>
Expand Down