Skip to content

Commit

Permalink
fix local storage build error by including the function directly rath…
Browse files Browse the repository at this point in the history
…er then importing it
  • Loading branch information
Sebastian Tilsch committed May 15, 2024
1 parent b008d1f commit fcb05f4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
1 change: 0 additions & 1 deletion packages/state-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"@slub/edb-ui-utils": "workspace:*",
"@slub/sparql-schema": "workspace:*",
"@reduxjs/toolkit": "^2.2.3",
"use-local-storage": "^3.0.0",
"json-schema": "^0.4.0",
"jsonld": "^8.3.2",
"react-redux": "^9.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/state-hooks/src/useLocalSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
UseLocalSettings,
} from "@slub/edb-core-types";
import { useAdbContext } from "./provider";
import useLocalStorage from "use-local-storage";
import { useLocalStorage } from "./useLocalStorage";

const defaultSparqlEndpoints: SparqlEndpoint[] = [
{
Expand Down
115 changes: 115 additions & 0 deletions packages/state-hooks/src/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { useEffect, useMemo, useRef, useState } from "react";

type Serializer<T> = (object: T | undefined) => string;
type Parser<T> = (val: string) => T | undefined;
type Setter<T> = React.Dispatch<React.SetStateAction<T | undefined>>;

type Options<T> = Partial<{
serializer: Serializer<T>;
parser: Parser<T>;
logger: (error: any) => void;
syncData: boolean;
}>;

export function useLocalStorage<T>(
key: string,
defaultValue: T,
options?: Options<T>,
): [T, Setter<T>];
export function useLocalStorage<T>(
key: string,
defaultValue?: T,
options?: Options<T>,
) {
const opts = useMemo(() => {
return {
serializer: JSON.stringify,
parser: JSON.parse,
logger: console.log,
syncData: true,
...options,
};
}, [options]);

const { serializer, parser, logger, syncData } = opts;

const rawValueRef = useRef<string | null>(null);

const [value, setValue] = useState(() => {
if (typeof window === "undefined") return defaultValue;

try {
rawValueRef.current = window.localStorage.getItem(key);
const res: T = rawValueRef.current
? parser(rawValueRef.current)
: defaultValue;
return res;
} catch (e) {
logger(e);
return defaultValue;
}
});

useEffect(() => {
if (typeof window === "undefined") return;

const updateLocalStorage = () => {
// Browser ONLY dispatch storage events to other tabs, NOT current tab.
// We need to manually dispatch storage event for current tab
if (value !== undefined) {
const newValue = serializer(value);
const oldValue = rawValueRef.current;
rawValueRef.current = newValue;
window.localStorage.setItem(key, newValue);
window.dispatchEvent(
new StorageEvent("storage", {
storageArea: window.localStorage,
url: window.location.href,
key,
newValue,
oldValue,
}),
);
} else {
window.localStorage.removeItem(key);
window.dispatchEvent(
new StorageEvent("storage", {
storageArea: window.localStorage,
url: window.location.href,
key,
}),
);
}
};

try {
updateLocalStorage();
} catch (e) {
logger(e);
}
}, [value]);

useEffect(() => {
if (!syncData) return;

const handleStorageChange = (e: StorageEvent) => {
if (e.key !== key || e.storageArea !== window.localStorage) return;

try {
if (e.newValue !== rawValueRef.current) {
rawValueRef.current = e.newValue;
setValue(e.newValue ? parser(e.newValue) : undefined);
}
} catch (e) {
logger(e);
}
};

if (typeof window === "undefined") return;

window.addEventListener("storage", handleStorageChange);
return () => window.removeEventListener("storage", handleStorageChange);
}, [key, syncData]);

return [value, setValue];
}

0 comments on commit fcb05f4

Please sign in to comment.