fix: 'Starred' section should open if collapsed and starred item is added

This commit is contained in:
Tom Moor
2024-04-25 20:27:12 -04:00
parent f0f6b729d4
commit 79899f3543
4 changed files with 29 additions and 2 deletions

View File

@@ -9,6 +9,23 @@ type Options = {
listen?: boolean;
};
/**
* Set a value in local storage and emit storage event to trigger render of any
* listening mounted components.
*
* @param key Key to store value under
* @param value Value to store
*/
export function setPersistedState<T extends Primitive | object>(
key: string,
value: T
) {
Storage.set(key, value);
window.dispatchEvent(
new StorageEvent("storage", { key, newValue: JSON.stringify(value) })
);
}
/**
* A hook with the same API as `useState` that persists its value locally and
* syncs the value between browser tabs.
@@ -49,7 +66,7 @@ export default function usePersistedState<T extends Primitive | object>(
// Listen to the key changing in other tabs so we can keep UI in sync
useEventListener("storage", (event: StorageEvent) => {
if (options?.listen && event.key === key && event.newValue) {
if (options?.listen !== false && event.key === key && event.newValue) {
setStoredValue(JSON.parse(event.newValue));
}
});