Skip to content

Code quality: Replace hardcoded store keys with store definitions #27088

Closed

Description

Description

With #26655 it's possible to use store definitions as param for select/useSelect/withSelect and dispatch/useDispatch/withDispatch API method calls exposed from @wordpress/data. It resolves the issue with the need to use unscoped import statements for packages that define stores used as explained in #8981 (comment):

To be clear, all packages that use a given datastore should import it in the entry point by convention. It's something that could be improved because if it's missed, it might indeed cause errors. At the moment, there is no simple way to validate it happens. Example:

import '@wordpress/core-data';
import '@wordpress/block-editor';
import '@wordpress/editor';
import '@wordpress/keyboard-shortcuts';
import '@wordpress/reusable-blocks';
import '@wordpress/viewport';
import '@wordpress/notices';

The next step is to use newly exposed store definitions from the corresponding packages similar to how it was applied to the store exposef from @wordpress/viewport in #26655. Example:

diff --git a/packages/interface/src/components/complementary-area/index.js b/packages/interface/src/components/complementary-area/index.js
index 56ad63c601e1..f35fcf9d14bc 100644
--- a/packages/interface/src/components/complementary-area/index.js
+++ b/packages/interface/src/components/complementary-area/index.js
@@ -11,6 +11,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
 import { __ } from '@wordpress/i18n';
 import { check, starEmpty, starFilled } from '@wordpress/icons';
 import { useEffect, useRef } from '@wordpress/element';
+import { store as viewportStore } from '@wordpress/viewport';
 
 /**
  * Internal dependencies
@@ -104,10 +105,8 @@ function ComplementaryArea( {
 				isActive: _activeArea === identifier,
 				isPinned: isItemPinned( scope, identifier ),
 				activeArea: _activeArea,
-				isSmall: select( 'core/viewport' ).isViewportMatch(
-					'< medium'
-				),
-				isLarge: select( 'core/viewport' ).isViewportMatch( 'large' ),
+				isSmall: select( viewportStore ).isViewportMatch( '< medium' ),
+				isLarge: select( viewportStore ).isViewportMatch( 'large' ),
 			};
 		},
 		[ identifier, scope ]

The goal is to make it simpler to maintain dependencies between packages to avoid bugs when consuming from npm. The other benefit is that we will prevent cyclic dependencies in the early stages of development.

Tasks

This is the list of store names that should be replaced with their newly introduced store definitions:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

[Package] Data/packages/data[Status] In ProgressTracking issues with work in progress[Type] Code QualityIssues or PRs that relate to code quality[Type] Tracking IssueTactical breakdown of efforts across the codebase and/or tied to Overview issues.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions