From 4a4ce6421ffca8f3c6cd63d50fc505838afa9a1e Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 23 Apr 2024 12:26:32 +0300 Subject: [PATCH 1/7] Core data: getEditedEntityRecord: do not return empty object --- packages/core-data/src/selectors.ts | 16 +++++++++---- .../edit-site/src/components/editor/index.js | 23 ++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index 4a135f343a097..bb6f74f6bd124 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -846,10 +846,18 @@ export const getEditedEntityRecord = createSelector( kind: string, name: string, recordId: EntityRecordKey - ): ET.Updatable< EntityRecord > | undefined => ( { - ...getRawEntityRecord( state, kind, name, recordId ), - ...getEntityRecordEdits( state, kind, name, recordId ), - } ), + ): ET.Updatable< EntityRecord > | undefined => { + const raw = getRawEntityRecord( state, kind, name, recordId ); + const edited = getEntityRecordEdits( state, kind, name, recordId ); + // Never return an empty object. + if ( ! raw && ! edited ) { + return; + } + return { + ...raw, + ...edited, + }; + }, ( state: State, kind: string, diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index d6773e5a24d89..cf5d39bbf7926 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -85,13 +85,34 @@ const interfaceLabels = { const ANIMATION_DURATION = 0.25; -export default function Editor( { isLoading, onClick } ) { +export default function Editor( props ) { const { record: editedPost, getTitle, isLoaded: hasLoadedPost, } = useEditedEntityRecord(); + if ( ! editedPost ) { + return null; + } + + return ( + + ); +} + +function EditorWithEntity( { + isLoading, + onClick, + editedPost, + getTitle, + hasLoadedPost, +} ) { const { type: editedPostType } = editedPost; const isLargeViewport = useViewportMatch( 'medium' ); From f7319d7959bb877b011ff183d82e2b3b3463afc0 Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 23 Apr 2024 15:05:27 +0300 Subject: [PATCH 2/7] Minimal change --- .../edit-site/src/components/editor/index.js | 27 ++----------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index cf5d39bbf7926..6c3ff9c7ffe40 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -85,36 +85,13 @@ const interfaceLabels = { const ANIMATION_DURATION = 0.25; -export default function Editor( props ) { +export default function Editor( { isLoading, onClick } ) { const { record: editedPost, getTitle, isLoaded: hasLoadedPost, } = useEditedEntityRecord(); - if ( ! editedPost ) { - return null; - } - - return ( - - ); -} - -function EditorWithEntity( { - isLoading, - onClick, - editedPost, - getTitle, - hasLoadedPost, -} ) { - const { type: editedPostType } = editedPost; - const isLargeViewport = useViewportMatch( 'medium' ); const disableMotion = useReducedMotion(); @@ -193,7 +170,7 @@ function EditorWithEntity( { // translators: A breadcrumb trail for the Admin document title. %1$s: title of template being edited, %2$s: type of template (Template or Template Part). __( '%1$s ‹ %2$s' ), getTitle(), - POST_TYPE_LABELS[ editedPostType ] ?? + POST_TYPE_LABELS[ editedPost.type ] ?? POST_TYPE_LABELS[ TEMPLATE_POST_TYPE ] ); } From 923d1a37a047cbda06d6a402f8039405c6ee318c Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 23 Apr 2024 15:12:12 +0300 Subject: [PATCH 3/7] Guard another --- .../src/components/block-editor/use-site-editor-settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/block-editor/use-site-editor-settings.js b/packages/edit-site/src/components/block-editor/use-site-editor-settings.js index 8e02f06d37edf..6de31c4130295 100644 --- a/packages/edit-site/src/components/block-editor/use-site-editor-settings.js +++ b/packages/edit-site/src/components/block-editor/use-site-editor-settings.js @@ -133,7 +133,7 @@ export function useSpecificEditorSettings() { ); const _context = getEditedPostContext(); return { - templateSlug: _record.slug, + templateSlug: _record?.slug, canvasMode: getCanvasMode(), settings: getSettings(), postWithTemplate: _context?.postId, From d2fb21736d4b7d6a7aec5b1cdc351251fdf615b9 Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 23 Apr 2024 15:23:27 +0300 Subject: [PATCH 4/7] Fix unit tests --- packages/components/tsconfig.json | 2 +- packages/core-data/src/hooks/test/use-entity-record.js | 2 +- packages/editor/src/store/selectors.js | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/components/tsconfig.json b/packages/components/tsconfig.json index 51c3da801cafb..7d78d020d9da1 100644 --- a/packages/components/tsconfig.json +++ b/packages/components/tsconfig.json @@ -41,7 +41,7 @@ "src/**/*.ios.js", "src/**/*.native.js", "src/**/react-native-*", - "src/**/stories/**/*.js", // only exclude js files, tsx files should be checked + "src/**/stories", // only exclude js files, tsx files should be checked "src/**/test/**/*.js" // only exclude js files, ts{x} files should be checked ] } diff --git a/packages/core-data/src/hooks/test/use-entity-record.js b/packages/core-data/src/hooks/test/use-entity-record.js index 49964339a4f2b..aa041089032e7 100644 --- a/packages/core-data/src/hooks/test/use-entity-record.js +++ b/packages/core-data/src/hooks/test/use-entity-record.js @@ -45,7 +45,7 @@ describe( 'useEntityRecord', () => { expect( data ).toEqual( { edit: expect.any( Function ), - editedRecord: {}, + editedRecord: undefined, hasEdits: false, edits: {}, record: undefined, diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index d5848ed8f08e9..c9a9b47b8cb68 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -527,6 +527,11 @@ export const isEditedPostEmpty = createRegistrySelector( postType, postId ); + + if ( ! record ) { + return true; + } + if ( typeof record.content !== 'function' ) { return ! record.content; } From 31383d6764a6066fb3af25b5daac6cfb7caa89f7 Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 23 Apr 2024 15:24:27 +0300 Subject: [PATCH 5/7] Restore tsconfig --- packages/components/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/components/tsconfig.json b/packages/components/tsconfig.json index 7d78d020d9da1..51c3da801cafb 100644 --- a/packages/components/tsconfig.json +++ b/packages/components/tsconfig.json @@ -41,7 +41,7 @@ "src/**/*.ios.js", "src/**/*.native.js", "src/**/react-native-*", - "src/**/stories", // only exclude js files, tsx files should be checked + "src/**/stories/**/*.js", // only exclude js files, tsx files should be checked "src/**/test/**/*.js" // only exclude js files, ts{x} files should be checked ] } From 3087897ea3bab29c44c4c53fa03bcc96d1e2b0a2 Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 23 Apr 2024 15:58:46 +0300 Subject: [PATCH 6/7] Return false so trying to get a property doesn't error --- packages/core-data/src/hooks/test/use-entity-record.js | 2 +- packages/core-data/src/selectors.ts | 6 +++--- .../src/components/block-editor/use-site-editor-settings.js | 2 +- packages/edit-site/src/components/editor/index.js | 4 +++- packages/editor/src/store/selectors.js | 5 ----- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/core-data/src/hooks/test/use-entity-record.js b/packages/core-data/src/hooks/test/use-entity-record.js index aa041089032e7..1bfb13f38ac15 100644 --- a/packages/core-data/src/hooks/test/use-entity-record.js +++ b/packages/core-data/src/hooks/test/use-entity-record.js @@ -45,7 +45,7 @@ describe( 'useEntityRecord', () => { expect( data ).toEqual( { edit: expect.any( Function ), - editedRecord: undefined, + editedRecord: false, hasEdits: false, edits: {}, record: undefined, diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index bb6f74f6bd124..14980db442cdc 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -846,12 +846,12 @@ export const getEditedEntityRecord = createSelector( kind: string, name: string, recordId: EntityRecordKey - ): ET.Updatable< EntityRecord > | undefined => { + ): ET.Updatable< EntityRecord > | false => { const raw = getRawEntityRecord( state, kind, name, recordId ); const edited = getEntityRecordEdits( state, kind, name, recordId ); // Never return an empty object. if ( ! raw && ! edited ) { - return; + return false; } return { ...raw, @@ -1272,7 +1272,7 @@ export function getReferenceByDistinctEdits( state ) { export function __experimentalGetTemplateForLink( state: State, link: string -): Optional< ET.Updatable< ET.WpTemplate > > | null { +): Optional< ET.Updatable< ET.WpTemplate > > | null | false { const records = getEntityRecords< ET.WpTemplate >( state, 'postType', diff --git a/packages/edit-site/src/components/block-editor/use-site-editor-settings.js b/packages/edit-site/src/components/block-editor/use-site-editor-settings.js index 6de31c4130295..8e02f06d37edf 100644 --- a/packages/edit-site/src/components/block-editor/use-site-editor-settings.js +++ b/packages/edit-site/src/components/block-editor/use-site-editor-settings.js @@ -133,7 +133,7 @@ export function useSpecificEditorSettings() { ); const _context = getEditedPostContext(); return { - templateSlug: _record?.slug, + templateSlug: _record.slug, canvasMode: getCanvasMode(), settings: getSettings(), postWithTemplate: _context?.postId, diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index 6c3ff9c7ffe40..d6773e5a24d89 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -92,6 +92,8 @@ export default function Editor( { isLoading, onClick } ) { isLoaded: hasLoadedPost, } = useEditedEntityRecord(); + const { type: editedPostType } = editedPost; + const isLargeViewport = useViewportMatch( 'medium' ); const disableMotion = useReducedMotion(); @@ -170,7 +172,7 @@ export default function Editor( { isLoading, onClick } ) { // translators: A breadcrumb trail for the Admin document title. %1$s: title of template being edited, %2$s: type of template (Template or Template Part). __( '%1$s ‹ %2$s' ), getTitle(), - POST_TYPE_LABELS[ editedPost.type ] ?? + POST_TYPE_LABELS[ editedPostType ] ?? POST_TYPE_LABELS[ TEMPLATE_POST_TYPE ] ); } diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index c9a9b47b8cb68..d5848ed8f08e9 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -527,11 +527,6 @@ export const isEditedPostEmpty = createRegistrySelector( postType, postId ); - - if ( ! record ) { - return true; - } - if ( typeof record.content !== 'function' ) { return ! record.content; } From 52232e964626a1bca1a106f16b3d8f67bcfb65c0 Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 23 Apr 2024 16:03:47 +0300 Subject: [PATCH 7/7] Update docs --- docs/reference-guides/data/data-core.md | 2 +- packages/core-data/README.md | 2 +- packages/core-data/src/selectors.ts | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/reference-guides/data/data-core.md b/docs/reference-guides/data/data-core.md index b80703dcc67b1..e740998a23623 100644 --- a/docs/reference-guides/data/data-core.md +++ b/docs/reference-guides/data/data-core.md @@ -178,7 +178,7 @@ _Parameters_ _Returns_ -- `undefined< EntityRecord > | undefined`: The entity record, merged with its edits. +- `undefined< EntityRecord > | false`: The entity record, merged with its edits. ### getEmbedPreview diff --git a/packages/core-data/README.md b/packages/core-data/README.md index 6677a32df08dc..444bce35ee521 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -499,7 +499,7 @@ _Parameters_ _Returns_ -- `undefined< EntityRecord > | undefined`: The entity record, merged with its edits. +- `undefined< EntityRecord > | false`: The entity record, merged with its edits. ### getEmbedPreview diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index 14980db442cdc..d238438e10eb1 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -849,7 +849,10 @@ export const getEditedEntityRecord = createSelector( ): ET.Updatable< EntityRecord > | false => { const raw = getRawEntityRecord( state, kind, name, recordId ); const edited = getEntityRecordEdits( state, kind, name, recordId ); - // Never return an empty object. + // Never return a non-falsy empty object. Unfortunately we can't return + // undefined or null because we were previously returning an empty + // object, so trying to read properties from the result would throw. + // Using false here is a workaround to avoid breaking changes. if ( ! raw && ! edited ) { return false; }