Skip to content

Commit

Permalink
refactor: update save state receivers (#172)
Browse files Browse the repository at this point in the history
The new data structure is that the save state always exists for a
layout, but the history is null/"" if we should just be using the
published data.

Also standardizes the theme/layout save state receivers.
  • Loading branch information
benlife5 authored Dec 17, 2024
1 parent b8e5d86 commit 7eae66e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/internal/hooks/layout/useMessageReceivers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ export const useLayoutMessageReceivers = () => {
const [layoutSaveStateFetched, setLayoutSaveStateFetched] =
useState<boolean>(false); // needed because saveState can be empty

useReceiveMessage("getSaveState", TARGET_ORIGINS, (send, payload) => {
let layoutSaveState;
if (payload) {
layoutSaveState = {
...payload,
useReceiveMessage("getLayoutSaveState", TARGET_ORIGINS, (send, payload) => {
let receivedLayoutSaveState;
if (payload?.history) {
receivedLayoutSaveState = {
hash: payload.hash,
history: jsonFromEscapedJsonString(payload.history),
} as LayoutSaveState;
}
devLogger.logData("SAVE_STATE", layoutSaveState);
setLayoutSaveState(layoutSaveState);
devLogger.logData("LAYOUT_SAVE_STATE", receivedLayoutSaveState);
setLayoutSaveState(receivedLayoutSaveState);
setLayoutSaveStateFetched(true);
send({ status: "success", payload: { message: "saveState received" } });
send({
status: "success",
payload: { message: "layoutSaveState received" },
});
});

return {
Expand Down
12 changes: 10 additions & 2 deletions src/internal/hooks/theme/useMessageReceivers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DevLogger } from "../../../utils/devLogger.ts";
import { ThemeSaveState } from "../../types/themeData.ts";
import { useReceiveMessage, TARGET_ORIGINS } from "../useMessage.ts";
import { useCommonMessageSenders } from "../useMessageSenders.ts";
import { jsonFromEscapedJsonString } from "../../utils/jsonFromEscapedJsonString.ts";

const devLogger = new DevLogger();

Expand All @@ -22,8 +23,15 @@ export const useThemeMessageReceivers = () => {
useState<boolean>(false); // needed because themeSaveState can be empty

useReceiveMessage("getThemeSaveState", TARGET_ORIGINS, (send, payload) => {
devLogger.logData("THEME_SAVE_STATE", payload);
setThemeSaveState(payload as ThemeSaveState);
let receivedThemeSaveState;
if (payload?.history) {
receivedThemeSaveState = {
hash: payload.hash,
history: jsonFromEscapedJsonString(payload.history),
} as ThemeSaveState;
}
devLogger.logData("THEME_SAVE_STATE", receivedThemeSaveState);
setThemeSaveState(receivedThemeSaveState);
setThemeSaveStateFetched(true);
send({
status: "success",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/devLogger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type DevLoggerPrefix =
| "TEMPLATE_METADATA"
| "PUCK_CONFIG"
| "SAVE_STATE"
| "LAYOUT_SAVE_STATE"
| "THEME_SAVE_STATE"
| "VISUAL_CONFIGURATION_DATA"
| "THEME_DATA"
Expand Down

0 comments on commit 7eae66e

Please sign in to comment.