2023-02-09 10:58:20 +00:00
|
|
|
import { useAppBridge } from "@saleor/app-sdk/app-bridge";
|
|
|
|
import { useTheme } from "@saleor/macaw-ui";
|
|
|
|
import { memo, useEffect } from "react";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Macaw-ui stores its theme mode in memory and local storage. To synchronize App with Dashboard,
|
|
|
|
* Macaw must be informed about this change from AppBridge.
|
|
|
|
*
|
|
|
|
* If you are not using Macaw, you can remove this.
|
|
|
|
*/
|
|
|
|
function _ThemeSynchronizer() {
|
|
|
|
const { appBridgeState, appBridge } = useAppBridge();
|
|
|
|
const { setTheme, themeType } = useTheme();
|
|
|
|
|
2023-04-18 13:10:00 +00:00
|
|
|
/*
|
|
|
|
* todo - replace this hook to appBridge.subscribe and react only only on initial theme event
|
|
|
|
* useEffect(() =>{
|
|
|
|
* appBridge?.subscribe('theme',console.log)
|
|
|
|
* },[appBridge])
|
|
|
|
*/
|
2023-02-09 10:58:20 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!setTheme || !appBridgeState?.theme) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (themeType !== appBridgeState?.theme) {
|
|
|
|
setTheme(appBridgeState.theme);
|
|
|
|
/**
|
|
|
|
* Hack to fix macaw, which is going into infinite loop on light mode (probably de-sync local storage with react state)
|
|
|
|
* TODO Fix me when Macaw 2.0 is shipped
|
|
|
|
*/
|
|
|
|
window.localStorage.setItem("macaw-ui-theme", appBridgeState.theme);
|
|
|
|
}
|
|
|
|
}, [appBridgeState?.theme, setTheme, themeType]);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ThemeSynchronizer = memo(_ThemeSynchronizer);
|