2023-03-06 09:57:25 +00:00
|
|
|
import { AppActionsHandler } from "@dashboard/apps/components/AppFrame/appActionsHandler";
|
|
|
|
import { usePostToExtension } from "@dashboard/apps/components/AppFrame/usePostToExtension";
|
2023-02-27 15:35:35 +00:00
|
|
|
import { Actions, DispatchResponseEvent } from "@saleor/app-sdk/app-bridge";
|
|
|
|
import React, { useState } from "react";
|
2022-07-05 08:39:58 +00:00
|
|
|
|
2021-08-20 13:58:53 +00:00
|
|
|
export const useAppActions = (
|
2023-02-27 15:35:35 +00:00
|
|
|
frameEl: HTMLIFrameElement | null,
|
2022-06-21 09:36:55 +00:00
|
|
|
appOrigin: string,
|
2022-07-05 08:39:58 +00:00
|
|
|
appId: string,
|
2023-02-27 15:35:35 +00:00
|
|
|
appToken: string,
|
2021-08-20 13:58:53 +00:00
|
|
|
) => {
|
2023-02-27 15:35:35 +00:00
|
|
|
const postToExtension = usePostToExtension(frameEl, appOrigin);
|
|
|
|
|
2023-01-27 15:21:19 +00:00
|
|
|
const { handle: handleNotification } =
|
|
|
|
AppActionsHandler.useHandleNotificationAction();
|
|
|
|
const { handle: handleUpdateRouting } =
|
|
|
|
AppActionsHandler.useHandleUpdateRoutingAction(appId);
|
|
|
|
const { handle: handleRedirect } =
|
|
|
|
AppActionsHandler.useHandleRedirectAction(appId);
|
2023-02-27 15:35:35 +00:00
|
|
|
const { handle: handleNotifyReady } = AppActionsHandler.useNotifyReadyAction(
|
|
|
|
frameEl,
|
|
|
|
appOrigin,
|
|
|
|
appToken,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store if app has performed a handshake with Dashboard, to avoid sending events before that
|
|
|
|
*/
|
|
|
|
const [handshakeDone, setHandshakeDone] = useState(false);
|
2023-01-27 15:21:19 +00:00
|
|
|
|
|
|
|
const handleAction = (action: Actions | undefined): DispatchResponseEvent => {
|
2021-08-20 13:58:53 +00:00
|
|
|
switch (action?.type) {
|
2022-08-10 13:26:09 +00:00
|
|
|
case "notification": {
|
2023-01-27 15:21:19 +00:00
|
|
|
return handleNotification(action);
|
2022-08-10 13:26:09 +00:00
|
|
|
}
|
2021-08-20 13:58:53 +00:00
|
|
|
case "redirect": {
|
2023-01-27 15:21:19 +00:00
|
|
|
return handleRedirect(action);
|
2021-08-20 13:58:53 +00:00
|
|
|
}
|
2022-11-22 09:29:29 +00:00
|
|
|
case "updateRouting": {
|
2023-01-27 15:21:19 +00:00
|
|
|
return handleUpdateRouting(action);
|
|
|
|
}
|
2023-02-27 15:35:35 +00:00
|
|
|
/**
|
|
|
|
* Send handshake after app informs its ready and mounted
|
|
|
|
*/
|
2023-01-27 15:21:19 +00:00
|
|
|
case "notifyReady": {
|
2023-02-27 15:35:35 +00:00
|
|
|
const response = handleNotifyReady(action);
|
|
|
|
|
|
|
|
setHandshakeDone(true);
|
|
|
|
|
|
|
|
return response;
|
2022-11-22 09:29:29 +00:00
|
|
|
}
|
2021-08-20 13:58:53 +00:00
|
|
|
default: {
|
2022-08-10 13:26:09 +00:00
|
|
|
throw new Error("Unknown action type");
|
2021-08-20 13:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const handler = (event: MessageEvent<Actions>) => {
|
|
|
|
if (event.origin === appOrigin) {
|
2023-01-27 15:21:19 +00:00
|
|
|
const response = handleAction(event.data);
|
2021-08-20 13:58:53 +00:00
|
|
|
|
|
|
|
postToExtension(response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener("message", handler);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("message", handler);
|
|
|
|
};
|
2023-02-27 15:35:35 +00:00
|
|
|
}, [appOrigin, handleAction, postToExtension]);
|
2021-08-20 13:58:53 +00:00
|
|
|
|
|
|
|
return {
|
2023-02-27 15:35:35 +00:00
|
|
|
handshakeDone,
|
2022-06-21 09:36:55 +00:00
|
|
|
postToExtension,
|
2023-02-27 15:35:35 +00:00
|
|
|
setHandshakeDone,
|
2021-08-20 13:58:53 +00:00
|
|
|
};
|
|
|
|
};
|