From 77be838613f9f3088dc41334c9d8e40961907c0b Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 16 Feb 2024 09:54:48 +0400 Subject: [PATCH 1/2] EditorInitialization: Fix ESLint warnings for internal hooks --- .../components/editor-initialization/index.js | 7 ++-- .../editor-initialization/listener-hooks.js | 42 +++++++++---------- .../test/listener-hooks.js | 14 +++---- packages/edit-post/src/editor.js | 2 +- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/packages/edit-post/src/components/editor-initialization/index.js b/packages/edit-post/src/components/editor-initialization/index.js index 0ba9cc863f473..bf61d569fc81c 100644 --- a/packages/edit-post/src/components/editor-initialization/index.js +++ b/packages/edit-post/src/components/editor-initialization/index.js @@ -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; } diff --git a/packages/edit-post/src/components/editor-initialization/listener-hooks.js b/packages/edit-post/src/components/editor-initialization/listener-hooks.js index 73872b4d7110e..a5534135f9cfb 100644 --- a/packages/edit-post/src/components/editor-initialization/listener-hooks.js +++ b/packages/edit-post/src/components/editor-initialization/listener-hooks.js @@ -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 ); @@ -49,21 +44,24 @@ 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(); @@ -71,7 +69,7 @@ export const useUpdatePostLinkListener = ( postId ) => { nodeToUpdate.current = document.querySelector( VIEW_AS_PREVIEW_LINK_SELECTOR ) || document.querySelector( VIEW_AS_LINK_SELECTOR ); - }, [ postId ] ); + }, [] ); useEffect( () => { if ( ! newPermalink || ! nodeToUpdate.current ) { diff --git a/packages/edit-post/src/components/editor-initialization/test/listener-hooks.js b/packages/edit-post/src/components/editor-initialization/test/listener-hooks.js index 5087d303fafe1..a294336ea1f1e 100644 --- a/packages/edit-post/src/components/editor-initialization/test/listener-hooks.js +++ b/packages/edit-post/src/components/editor-initialization/test/listener-hooks.js @@ -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 ( - + ); }; @@ -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 ( - + ); }; diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index f179ee6156e63..fd44d3dae4ca4 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -96,7 +96,7 @@ function Editor( { > - + From 8f47de9bb84a4aa41f965de07102d824adfa7b7c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 26 Feb 2024 18:33:21 +0400 Subject: [PATCH 2/2] Feedback --- .../src/components/editor-initialization/test/listener-hooks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-post/src/components/editor-initialization/test/listener-hooks.js b/packages/edit-post/src/components/editor-initialization/test/listener-hooks.js index a294336ea1f1e..b9fd01cd5a224 100644 --- a/packages/edit-post/src/components/editor-initialization/test/listener-hooks.js +++ b/packages/edit-post/src/components/editor-initialization/test/listener-hooks.js @@ -222,7 +222,7 @@ describe( 'listener hook tests', () => { } ); const { rerender } = render( ); - rerender( ); + rerender( ); expect( mockSelector ).toHaveBeenCalledTimes( 1 ); act( () => {