Add whenInViewContext
higher-order component (HOC) to allow simplification of code that should only run inside some view contexts
#8723
Labels
P2
Low priority
Type: Enhancement
Improvement of an existing feature
Type: Infrastructure
Engineering infrastructure & tooling
Feature Description
There are places in the codebase where we don't want to render a component or want to render a fallback in certain view contexts. Mostly, this is when we don't want a component to render on a view-only dashboard. Example:
site-kit-wp/assets/js/components/notifications/GA4AdSenseLinkedNotification.js
Lines 115 to 121 in 4cce042
Because some selectors will actually error on view-only dashboards (because a view-only user doesn't have access to the API routes/etc.), we have to guard every selector call, which is wasteful.
Similar to our
whenActive
higher-order component, we should create awhenInViewContext
HOC that allows us to only render a component in certain view contexts, and use that component to refactor existing components likeGA4AdSenseLinkedNotification
.Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
whenInViewContext
HOC should be created that behaves similarly to thewhenActive
andwhenScopesGranted
HOCs, except it should render/not render based on arguments supplied:allViewOnly
, which whentrue
should render the component whenuseViewOnly()
returnstrue
allNonViewOnly
which whentrue
should render the component whenuseViewOnly()
returnsfalse
includeList
which is an array of view contexts that the component should render when presentexcludeList
which is an array of view contexts that the component should render when not presentincludeList
andexcludeList
) should throw an errorGA4AdSenseLinkedNotification.js
component should be refactored to use this new HOC and remove any (now-redundant) checks from itsuseSelect
calls (example:site-kit-wp/assets/js/components/notifications/GA4AdSenseLinkedNotification.js
Lines 62 to 64 in de09a10
Implementation Brief
whenInViewContext HOC
whenInViewContext
, in the fileassets/js/util/whenInViewContext.js
:invariant
call, with the props:!(!! includeList && !! excludeList)
and the message: 'include and exclude lists cannot both be provided'.invariant
call, with the props:!(!! allViewOnly && !! allNonViewOnly)
and the message: 'view only and non view only lists cannot both be true'.( WrappedComponent ) => {
:function WhenInViewContext( props ) {
const isViewOnly = useViewOnly();
props.allViewOnly
istrue
andisViewOnly
isfalse
, returnnull
.props.allNonViewOnly
istrue
andisViewOnly
istrue
, returnnull
.const viewContext = useViewContext();
!! includeList
and! includeList.includes( viewContext )
, returnnull
.!! excludeList
andincludeList.includes( viewContext )
, returnnull
.<WrappedComponent { ...props } />;
Update GA4AdSenseLinkedNotification
assets/js/components/notifications/GA4AdSenseLinkedNotification.js
, wrapping the export of the component with the newwhenInViewContext
HOC, passing the props:{ allViewOnly: true }
.Test Coverage
assets/js/util/whenInViewContext.test.js
, to test each case in the AC + the invariant conditions.QA Brief
Changelog entry
The text was updated successfully, but these errors were encountered: