Skip to content

Commit

Permalink
EditorInitialization: Fix ESLint warnings for internal hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Feb 26, 2024
1 parent a5721d9 commit 77be838
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
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 = () => {
return (
<RegistryProvider value={ registry }>
<TestComponent postId={ postId } />
<TestComponent />
</RegistryProvider>
);
};
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

0 comments on commit 77be838

Please sign in to comment.