2023-02-27 15:35:35 +00:00
|
|
|
import { useAppDashboardUpdates } from "@dashboard/apps/components/AppFrame/useAppDashboardUpdates";
|
2022-10-26 11:19:20 +00:00
|
|
|
import {
|
|
|
|
AppDetailsUrlQueryParams,
|
2023-01-16 13:55:38 +00:00
|
|
|
prepareFeatureFlagsList,
|
2022-11-17 14:10:33 +00:00
|
|
|
resolveAppIframeUrl,
|
2023-01-16 09:45:12 +00:00
|
|
|
} from "@dashboard/apps/urls";
|
2023-01-16 13:55:38 +00:00
|
|
|
import { useAllFlags } from "@dashboard/hooks/useFlags";
|
2023-02-27 15:35:35 +00:00
|
|
|
import { CircularProgress } from "@material-ui/core";
|
2021-08-20 13:58:53 +00:00
|
|
|
import { useTheme } from "@saleor/macaw-ui";
|
2022-06-08 06:44:28 +00:00
|
|
|
import clsx from "clsx";
|
2023-02-27 15:35:35 +00:00
|
|
|
import React from "react";
|
2021-08-20 13:58:53 +00:00
|
|
|
|
|
|
|
import { useStyles } from "./styles";
|
|
|
|
import { useAppActions } from "./useAppActions";
|
2022-07-12 10:30:53 +00:00
|
|
|
import useTokenRefresh from "./useTokenRefresh";
|
2021-08-20 13:58:53 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
src: string;
|
|
|
|
appToken: string;
|
2022-04-25 16:06:45 +00:00
|
|
|
appId: string;
|
2022-06-08 06:44:28 +00:00
|
|
|
className?: string;
|
2022-10-26 11:19:20 +00:00
|
|
|
params?: AppDetailsUrlQueryParams;
|
2022-07-12 10:30:53 +00:00
|
|
|
refetch?: () => void;
|
2021-08-20 13:58:53 +00:00
|
|
|
onLoad?(): void;
|
|
|
|
onError?(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getOrigin = (url: string) => new URL(url).origin;
|
|
|
|
|
|
|
|
export const AppFrame: React.FC<Props> = ({
|
|
|
|
src,
|
|
|
|
appToken,
|
2022-04-25 16:06:45 +00:00
|
|
|
appId,
|
2022-06-08 06:44:28 +00:00
|
|
|
className,
|
2022-10-26 11:19:20 +00:00
|
|
|
params = {},
|
2021-08-20 13:58:53 +00:00
|
|
|
onLoad,
|
2022-06-21 09:36:55 +00:00
|
|
|
onError,
|
2022-07-12 10:30:53 +00:00
|
|
|
refetch,
|
2021-08-20 13:58:53 +00:00
|
|
|
}) => {
|
2023-02-27 15:35:35 +00:00
|
|
|
const frameRef = React.useRef<HTMLIFrameElement | null>(null);
|
2022-09-13 07:15:48 +00:00
|
|
|
const { themeType } = useTheme();
|
2021-08-20 13:58:53 +00:00
|
|
|
const classes = useStyles();
|
|
|
|
const appOrigin = getOrigin(src);
|
2023-01-16 13:55:38 +00:00
|
|
|
const flags = useAllFlags();
|
2022-09-15 06:18:43 +00:00
|
|
|
|
2023-02-27 15:35:35 +00:00
|
|
|
/**
|
|
|
|
* React on messages from App
|
|
|
|
*/
|
|
|
|
const { postToExtension, handshakeDone, setHandshakeDone } = useAppActions(
|
|
|
|
frameRef.current,
|
|
|
|
appOrigin,
|
|
|
|
appId,
|
|
|
|
appToken,
|
|
|
|
);
|
2022-09-13 07:15:48 +00:00
|
|
|
|
2023-02-27 15:35:35 +00:00
|
|
|
/**
|
|
|
|
* Listen to Dashboard context like theme or locale and inform app about it
|
|
|
|
*/
|
|
|
|
useAppDashboardUpdates(frameRef.current, appOrigin, handshakeDone, appId);
|
2021-08-20 13:58:53 +00:00
|
|
|
|
2022-07-12 10:30:53 +00:00
|
|
|
useTokenRefresh(appToken, refetch);
|
|
|
|
|
2021-08-20 13:58:53 +00:00
|
|
|
const handleLoad = () => {
|
2023-02-27 15:35:35 +00:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*
|
|
|
|
* Move handshake to notifyReady, so app is requesting token after it's ready to receive it
|
|
|
|
* Currently handshake it 2 times, for compatibility
|
|
|
|
*/
|
2021-08-20 13:58:53 +00:00
|
|
|
postToExtension({
|
|
|
|
type: "handshake",
|
|
|
|
payload: {
|
|
|
|
token: appToken,
|
2022-06-21 09:36:55 +00:00
|
|
|
version: 1,
|
|
|
|
},
|
2021-08-20 13:58:53 +00:00
|
|
|
});
|
2023-02-27 15:35:35 +00:00
|
|
|
|
|
|
|
setHandshakeDone(true);
|
2021-08-20 13:58:53 +00:00
|
|
|
|
|
|
|
if (onLoad) {
|
|
|
|
onLoad();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-02-27 15:35:35 +00:00
|
|
|
<>
|
|
|
|
<div className={classes.loader}>
|
|
|
|
<CircularProgress color="primary" />
|
|
|
|
</div>
|
|
|
|
<iframe
|
|
|
|
ref={frameRef}
|
|
|
|
src={resolveAppIframeUrl(appId, src, {
|
|
|
|
...params,
|
|
|
|
featureFlags: prepareFeatureFlagsList(flags),
|
|
|
|
theme: themeType,
|
|
|
|
})}
|
|
|
|
onError={onError}
|
|
|
|
onLoad={handleLoad}
|
|
|
|
className={clsx(classes.iframe, className, {
|
|
|
|
[classes.iframeHidden]: !handshakeDone,
|
|
|
|
})}
|
|
|
|
sandbox="allow-same-origin allow-forms allow-scripts"
|
|
|
|
/>
|
|
|
|
</>
|
2021-08-20 13:58:53 +00:00
|
|
|
);
|
|
|
|
};
|