* [#451] added websocket connexion * [#451] added tests * [#451] add missing cleanup for web socket causing memory leaks
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
export function setSelectedCalendars(calendars: string[]) {
|
||||
try {
|
||||
localStorage.setItem("selectedCalendars", JSON.stringify(calendars));
|
||||
|
||||
window.dispatchEvent(
|
||||
new CustomEvent("selectedCalendarsChanged", {
|
||||
detail: calendars,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to save selected calendars:", error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function useSelectedCalendars(): string[] {
|
||||
const [calendars, setCalendars] = useState<string[]>(() => {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem("selectedCalendars") ?? "[]");
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const onStorage = (e: StorageEvent) => {
|
||||
if (e.key === "selectedCalendars") {
|
||||
try {
|
||||
setCalendars(JSON.parse(e.newValue ?? "[]"));
|
||||
} catch {
|
||||
setCalendars([]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const onLocalChange = (e: CustomEvent<string[]>) => {
|
||||
setCalendars(e.detail);
|
||||
};
|
||||
|
||||
window.addEventListener("storage", onStorage);
|
||||
window.addEventListener(
|
||||
"selectedCalendarsChanged",
|
||||
onLocalChange as EventListener
|
||||
);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("storage", onStorage);
|
||||
window.removeEventListener(
|
||||
"selectedCalendarsChanged",
|
||||
onLocalChange as EventListener
|
||||
);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return calendars;
|
||||
}
|
||||
Reference in New Issue
Block a user