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

EditorInitialization: Fix ESLint warnings for internal hooks #59118

Merged
merged 2 commits into from
Feb 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import {
* Data component used for initializing the editor and re-initializes
* when postId changes or on unmount.
*
* @param {number} postId The id of the post.
* @return {null} This is a data component so does not render any ui.
*/
export default function EditorInitialization( { postId } ) {
useBlockSelectionListener( postId );
useUpdatePostLinkListener( postId );
export default function EditorInitialization() {
useBlockSelectionListener();
useUpdatePostLinkListener();
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,19 @@ import {
/**
* This listener hook monitors for block selection and triggers the appropriate
* sidebar state.
*
* @param {number} postId The current post id.
*/
export const useBlockSelectionListener = ( postId ) => {
export const useBlockSelectionListener = () => {
const { hasBlockSelection, isEditorSidebarOpened, isDistractionFree } =
useSelect(
( select ) => {
const { get } = select( preferencesStore );
return {
hasBlockSelection:
!! select( blockEditorStore ).getBlockSelectionStart(),
isEditorSidebarOpened:
select( STORE_NAME ).isEditorSidebarOpened(),
isDistractionFree: get( 'core', 'distractionFree' ),
};
},
[ postId ]
);
useSelect( ( select ) => {
const { get } = select( preferencesStore );
return {
hasBlockSelection:
!! select( blockEditorStore ).getBlockSelectionStart(),
isEditorSidebarOpened:
select( STORE_NAME ).isEditorSidebarOpened(),
isDistractionFree: get( 'core', 'distractionFree' ),
};
}, [] );

const { openGeneralSidebar } = useDispatch( STORE_NAME );

Expand All @@ -49,29 +44,32 @@ export const useBlockSelectionListener = ( postId ) => {
} else {
openGeneralSidebar( 'edit-post/document' );
}
}, [ hasBlockSelection, isEditorSidebarOpened ] );
}, [
hasBlockSelection,
isDistractionFree,
isEditorSidebarOpened,
openGeneralSidebar,
] );
};

/**
* This listener hook monitors any change in permalink and updates the view
* post link in the admin bar.
*
* @param {number} postId
*/
export const useUpdatePostLinkListener = ( postId ) => {
export const useUpdatePostLinkListener = () => {
const { newPermalink } = useSelect(
( select ) => ( {
newPermalink: select( editorStore ).getCurrentPost().link,
} ),
[ postId ]
[]
);
const nodeToUpdate = useRef();

useEffect( () => {
nodeToUpdate.current =
document.querySelector( VIEW_AS_PREVIEW_LINK_SELECTOR ) ||
document.querySelector( VIEW_AS_LINK_SELECTOR );
}, [ postId ] );
}, [] );

useEffect( () => {
if ( ! newPermalink || ! nodeToUpdate.current ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ describe( 'listener hook tests', () => {
} );
describe( 'useBlockSelectionListener', () => {
const registry = createRegistry( mockStores );
const TestComponent = ( { postId } ) => {
useBlockSelectionListener( postId );
const TestComponent = () => {
useBlockSelectionListener();
return null;
};
const TestedOutput = () => {
return (
<RegistryProvider value={ registry }>
<TestComponent postId={ 10 } />
<TestComponent />
</RegistryProvider>
);
};
Expand Down Expand Up @@ -177,14 +177,14 @@ describe( 'listener hook tests', () => {

describe( 'useUpdatePostLinkListener', () => {
const registry = createRegistry( mockStores );
const TestComponent = ( { postId } ) => {
useUpdatePostLinkListener( postId );
const TestComponent = () => {
useUpdatePostLinkListener();
return null;
};
const TestedOutput = ( { postId = 10 } ) => {
const TestedOutput = () => {
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
return (
<RegistryProvider value={ registry }>
<TestComponent postId={ postId } />
<TestComponent />
</RegistryProvider>
);
};
Expand Down Expand Up @@ -222,7 +222,7 @@ describe( 'listener hook tests', () => {
} );
const { rerender } = render( <TestedOutput /> );

rerender( <TestedOutput id={ 20 } /> );
rerender( <TestedOutput /> );

expect( mockSelector ).toHaveBeenCalledTimes( 1 );
act( () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function Editor( {
>
<ErrorBoundary>
<CommandMenu />
<EditorInitialization postId={ currentPost.postId } />
<EditorInitialization />
<Layout initialPost={ initialPost } />
</ErrorBoundary>
<PostLockedModal />
Expand Down
Loading