Skip to content

Commit

Permalink
feat(app-shell): allow to enable StrictMode through shell (#3287)
Browse files Browse the repository at this point in the history
* feat(app-shell): allow to enable StrictMode through shell

* feat: add strict mode prop to custom view shell

* Create green-stingrays-compete.md
  • Loading branch information
tdeekens authored Nov 6, 2023
1 parent 41b15c7 commit e987dbf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-stingrays-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@commercetools-frontend/application-shell": minor
---

The `ApplicationShell` and `CustomViewShell` not support an optional `enableReactStrictMode` prop to render their `children` in [Strict Mode](https://react.dev/reference/react/StrictMode).
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type ReactNode, type SyntheticEvent, useEffect } from 'react';
import {
type ReactNode,
type SyntheticEvent,
useEffect,
StrictMode,
} from 'react';
import type { NormalizedCacheObject } from '@apollo/client';
import { ApolloClient } from '@apollo/client';
import type { TFlags } from '@flopflip/types';
Expand Down Expand Up @@ -41,10 +46,24 @@ type TApplicationShellProps = {
onRegisterErrorListeners?: (args: { dispatch: Dispatch }) => void;
onMenuItemClick?: (event: SyntheticEvent<HTMLAnchorElement>) => void;
disableRoutePermissionCheck?: boolean;
enableReactStrictMode?: boolean;
render?: () => JSX.Element;
children?: ReactNode;
};

type TStrictModeEnablementProps = {
enableReactStrictMode?: boolean;
children?: ReactNode;
};

const StrictModeEnablement = (props: TStrictModeEnablementProps) => {
if (props.enableReactStrictMode) {
return <StrictMode>{props.children}</StrictMode>;
} else {
return <>{props.children}</>;
}
};

const ApplicationShell = (props: TApplicationShellProps) => {
useEffect(() => {
props.onRegisterErrorListeners?.({
Expand All @@ -54,7 +73,7 @@ const ApplicationShell = (props: TApplicationShellProps) => {
}, []); // <-- run only once, when component mounts

return (
<>
<StrictModeEnablement enableReactStrictMode={props.enableReactStrictMode}>
<GlobalStyles />
<ApplicationShellProvider
apolloClient={props.apolloClient}
Expand Down Expand Up @@ -88,7 +107,7 @@ const ApplicationShell = (props: TApplicationShellProps) => {
return <RedirectToLogin />;
}}
</ApplicationShellProvider>
</>
</StrictModeEnablement>
);
};
ApplicationShell.displayName = 'ApplicationShell';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useRef,
useState,
Suspense,
StrictMode,
type ReactNode,
} from 'react';
import { PageUnauthorized } from '@commercetools-frontend/application-components';
Expand Down Expand Up @@ -45,11 +46,25 @@ type THostEventData = {
type TCustomViewShellProps = {
applicationMessages: TAsyncLocaleDataProps['applicationMessages'];
disableDevHost?: boolean;
enableReactStrictMode?: boolean;
children: ReactNode;
};

const browserLocale = getBrowserLocale(window);

type TStrictModeEnablementProps = {
enableReactStrictMode?: boolean;
children?: ReactNode;
};

function StrictModeEnablement(props: TStrictModeEnablementProps) {
if (props.enableReactStrictMode) {
return <StrictMode>{props.children}</StrictMode>;
} else {
return <>{props.children}</>;
}
}

function CustomViewShell(props: TCustomViewShellProps) {
const [hostContext, setHostContext] = useState<THostContext>();
const iFrameCommunicationPort = useRef<MessagePort>();
Expand Down Expand Up @@ -160,19 +175,23 @@ function CustomViewShell(props: TCustomViewShellProps) {
const CustomViewShellWrapper = (props: TCustomViewShellProps) => {
if (process.env.NODE_ENV === 'development' && !props.disableDevHost) {
return (
<Suspense fallback={<ApplicationLoader />}>
<CustomViewDevHost applicationMessages={props.applicationMessages}>
<CustomViewShell applicationMessages={props.applicationMessages}>
{props.children}
</CustomViewShell>
</CustomViewDevHost>
</Suspense>
<StrictModeEnablement enableReactStrictMode={props.enableReactStrictMode}>
<Suspense fallback={<ApplicationLoader />}>
<CustomViewDevHost applicationMessages={props.applicationMessages}>
<CustomViewShell applicationMessages={props.applicationMessages}>
{props.children}
</CustomViewShell>
</CustomViewDevHost>
</Suspense>
</StrictModeEnablement>
);
}
return (
<CustomViewShell applicationMessages={props.applicationMessages}>
{props.children}
</CustomViewShell>
<StrictModeEnablement enableReactStrictMode={props.enableReactStrictMode}>
<CustomViewShell applicationMessages={props.applicationMessages}>
{props.children}
</CustomViewShell>
</StrictModeEnablement>
);
};

Expand Down

2 comments on commit e987dbf

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for application-kit-custom-views ready!

✅ Preview
https://application-kit-custom-views-2xglxn2gx-commercetools.vercel.app

Built with commit e987dbf.
This pull request is being automatically deployed with vercel-action

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for merchant-center-application-kit ready!

✅ Preview
https://merchant-center-application-n5w3r6sio-commercetools.vercel.app

Built with commit e987dbf.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.