summaryrefslogtreecommitdiff
path: root/web/src/hooks/PersistentStorage.ts
diff options
context:
space:
mode:
authorJames Elliott <james-d-elliott@users.noreply.github.com>2023-01-22 19:58:07 +1100
committerGitHub <noreply@github.com>2023-01-22 19:58:07 +1100
commita566c16d08677b03d3c6b66fd348ceec0bd23dda (patch)
tree95f348ca1e32b339e49582be7bdaa7d0908a3818 /web/src/hooks/PersistentStorage.ts
parentdf52b1b4c493650b050be7944d96fccc86c59cb3 (diff)
feat(web): privacy policy url (#4625)
This allows users to customize a privacy policy URL at the bottom of the login view. Closes #2639
Diffstat (limited to 'web/src/hooks/PersistentStorage.ts')
-rw-r--r--web/src/hooks/PersistentStorage.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/web/src/hooks/PersistentStorage.ts b/web/src/hooks/PersistentStorage.ts
new file mode 100644
index 000000000..ce129a271
--- /dev/null
+++ b/web/src/hooks/PersistentStorage.ts
@@ -0,0 +1,60 @@
+import { useEffect, useState } from "react";
+
+interface PersistentStorage {
+ getItem(key: string): string | null;
+ setItem(key: string, value: any): void;
+}
+
+class LocalStorage implements PersistentStorage {
+ getItem(key: string) {
+ const item = localStorage.getItem(key);
+
+ if (item === null) return undefined;
+
+ if (item === "null") return null;
+ if (item === "undefined") return undefined;
+
+ try {
+ return JSON.parse(item);
+ } catch {}
+
+ return item;
+ }
+ setItem(key: string, value: any) {
+ if (value === undefined) {
+ localStorage.removeItem(key);
+ } else {
+ localStorage.setItem(key, JSON.stringify(value));
+ }
+ }
+}
+
+class MockStorage implements PersistentStorage {
+ getItem() {
+ return null;
+ }
+ setItem() {}
+}
+
+const persistentStorage = window?.localStorage ? new LocalStorage() : new MockStorage();
+
+export function usePersistentStorageValue<T>(key: string, initialValue?: T) {
+ const [value, setValue] = useState<T>(() => {
+ const valueFromStorage = persistentStorage.getItem(key);
+
+ if (typeof initialValue === "object" && !Array.isArray(initialValue) && initialValue !== null) {
+ return {
+ ...initialValue,
+ ...valueFromStorage,
+ };
+ }
+
+ return valueFromStorage || initialValue;
+ });
+
+ useEffect(() => {
+ persistentStorage.setItem(key, value);
+ }, [key, value]);
+
+ return [value, setValue] as const;
+}