From b9acfe6214cd613d355700ee16da1dbd0a521084 Mon Sep 17 00:00:00 2001 From: Lukasz Ostrowski Date: Mon, 27 Feb 2023 16:35:35 +0100 Subject: [PATCH] Improve AppPage (#3216) * WIP - reduce rerenders and change how dashboard sends events to app * Cleanup * Refactor * Add loader behind the iframe * Fix linter --- src/apps/components/AppFrame/AppFrame.tsx | 101 ++++++++---------- .../components/AppFrame/appActionsHandler.ts | 34 ++++-- src/apps/components/AppFrame/styles.ts | 10 ++ src/apps/components/AppFrame/useAppActions.ts | 43 +++++--- .../AppFrame/useAppDashboardUpdates.ts | 58 ++++++++++ .../components/AppFrame/usePostToExtension.ts | 22 ++++ .../components/AppPage/AppPage.stories.tsx | 2 +- src/apps/components/AppPage/AppPage.tsx | 7 +- src/apps/components/AppPage/index.ts | 1 - src/apps/components/AppPage/styles.ts | 1 + src/apps/index.tsx | 2 +- src/apps/views/App/{App.tsx => AppView.tsx} | 32 +++--- src/apps/views/App/index.ts | 3 +- src/apps/views/AppSettings/AppSettings.tsx | 2 +- src/new-apps/index.tsx | 10 +- 15 files changed, 219 insertions(+), 109 deletions(-) create mode 100644 src/apps/components/AppFrame/useAppDashboardUpdates.ts create mode 100644 src/apps/components/AppFrame/usePostToExtension.ts rename src/apps/views/App/{App.tsx => AppView.tsx} (68%) diff --git a/src/apps/components/AppFrame/AppFrame.tsx b/src/apps/components/AppFrame/AppFrame.tsx index 58b1312da..9ff2a11b4 100644 --- a/src/apps/components/AppFrame/AppFrame.tsx +++ b/src/apps/components/AppFrame/AppFrame.tsx @@ -1,16 +1,14 @@ +import { useAppDashboardUpdates } from "@dashboard/apps/components/AppFrame/useAppDashboardUpdates"; import { AppDetailsUrlQueryParams, - getAppDeepPathFromDashboardUrl, prepareFeatureFlagsList, resolveAppIframeUrl, } from "@dashboard/apps/urls"; import { useAllFlags } from "@dashboard/hooks/useFlags"; -import useLocale from "@dashboard/hooks/useLocale"; -import useShop from "@dashboard/hooks/useShop"; +import { CircularProgress } from "@material-ui/core"; import { useTheme } from "@saleor/macaw-ui"; import clsx from "clsx"; -import React, { useEffect } from "react"; -import { useLocation } from "react-router"; +import React from "react"; import { useStyles } from "./styles"; import { useAppActions } from "./useAppActions"; @@ -39,46 +37,36 @@ export const AppFrame: React.FC = ({ onError, refetch, }) => { - const shop = useShop(); - const frameRef = React.useRef(null); + const frameRef = React.useRef(null); const { themeType } = useTheme(); const classes = useStyles(); const appOrigin = getOrigin(src); const flags = useAllFlags(); - const { postToExtension } = useAppActions(frameRef, appOrigin, appId); - const location = useLocation(); - const { locale } = useLocale(); - useEffect(() => { - postToExtension({ - type: "localeChanged", - payload: { - locale, - }, - }); - }, [locale, postToExtension]); + /** + * React on messages from App + */ + const { postToExtension, handshakeDone, setHandshakeDone } = useAppActions( + frameRef.current, + appOrigin, + appId, + appToken, + ); - useEffect(() => { - postToExtension({ - type: "theme", - payload: { - theme: themeType, - }, - }); - }, [themeType, postToExtension]); - - useEffect(() => { - postToExtension({ - type: "redirect", - payload: { - path: getAppDeepPathFromDashboardUrl(location.pathname, appId), - }, - }); - }, [location.pathname]); + /** + * Listen to Dashboard context like theme or locale and inform app about it + */ + useAppDashboardUpdates(frameRef.current, appOrigin, handshakeDone, appId); useTokenRefresh(appToken, refetch); const handleLoad = () => { + /** + * @deprecated + * + * Move handshake to notifyReady, so app is requesting token after it's ready to receive it + * Currently handshake it 2 times, for compatibility + */ postToExtension({ type: "handshake", payload: { @@ -86,34 +74,33 @@ export const AppFrame: React.FC = ({ version: 1, }, }); - postToExtension({ - type: "theme", - payload: { - theme: themeType, - }, - }); + + setHandshakeDone(true); if (onLoad) { onLoad(); } }; - if (!shop?.domain.host) { - return null; - } - return ( -