Skip to content

[FIX]: #1529 Watch Local Storage Changes #1652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions client/packages/lowcoder/src/comps/hooks/localStorageComp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,28 @@ const APP_STORE_NAMESPACE = "lowcoder_app_local_storage";
const LocalStorageCompBase = withViewFn(
simpleMultiComp({ values: stateComp<JSONObject>({}) }),
(comp) => {
const originStore = localStorage.getItem(APP_STORE_NAMESPACE) || "{}";
let parseStore = {};
try {
parseStore = JSON.parse(originStore);
} catch (e) {
log.error("application local storage invalid");
}

// add custom event listener to update values reactively
useEffect(() => {
const value = comp.children.values.value;
if (!isEqual(value, parseStore)) {
log.info(value, parseStore);
comp.children.values.dispatchChangeValueAction(parseStore);
}
}, [parseStore]);
const handler = () => {
try {
const raw = localStorage.getItem(APP_STORE_NAMESPACE) || "{}";
const parsed = JSON.parse(raw);
comp.children.values.dispatchChangeValueAction(parsed);
} catch (e) {
log.error("Failed to parse localStorage:", e);
}
};

// Add listener on mount
window.addEventListener("lowcoder-localstorage-updated", handler);

// Run once on mount to initialize
handler();

return () => {
window.removeEventListener("lowcoder-localstorage-updated", handler);
};
}, []);

return null;
}
Expand Down Expand Up @@ -62,6 +69,8 @@ LocalStorageComp = withMethodExposing(LocalStorageComp, [
parseStore[key] = value;
localStorage.setItem(APP_STORE_NAMESPACE, JSON.stringify(parseStore));
comp.children.values.dispatchChangeValueAction(parseStore);

window.dispatchEvent(new CustomEvent("lowcoder-localstorage-updated"));
} catch (e) {
localStorage.setItem(APP_STORE_NAMESPACE, "{}");
}
Expand All @@ -83,6 +92,9 @@ LocalStorageComp = withMethodExposing(LocalStorageComp, [
delete parseStore[key];
localStorage.setItem(APP_STORE_NAMESPACE, JSON.stringify(parseStore));
comp.children.values.dispatchChangeValueAction(parseStore);

// Trigger update
window.dispatchEvent(new CustomEvent("lowcoder-localstorage-updated"));
} catch (e) {
localStorage.setItem(APP_STORE_NAMESPACE, "{}");
}
Expand All @@ -98,6 +110,9 @@ LocalStorageComp = withMethodExposing(LocalStorageComp, [
execute: (comp) => {
localStorage.removeItem(APP_STORE_NAMESPACE);
comp.children.values.dispatchChangeValueAction({});

// Trigger update
window.dispatchEvent(new CustomEvent("lowcoder-localstorage-updated"));
},
},
]);
Loading