diff options
Diffstat (limited to 'packages/web-util/src/hooks')
-rw-r--r-- | packages/web-util/src/hooks/useLocalStorage.ts | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/packages/web-util/src/hooks/useLocalStorage.ts b/packages/web-util/src/hooks/useLocalStorage.ts index dd6c5def8..495c9b0f8 100644 --- a/packages/web-util/src/hooks/useLocalStorage.ts +++ b/packages/web-util/src/hooks/useLocalStorage.ts @@ -20,7 +20,12 @@ */ import { useEffect, useState } from "preact/hooks"; -import { localStorageMap, memoryMap } from "../utils/observable.js"; +import { + ObservableMap, + browserStorageMap, + localStorageMap, + memoryMap, +} from "../utils/observable.js"; export interface LocalStorageState { value?: string; @@ -29,8 +34,18 @@ export interface LocalStorageState { } const supportLocalStorage = typeof window !== "undefined"; +const supportBrowserStorage = + typeof chrome !== "undefined" && typeof chrome.storage !== "undefined"; -const storage = supportLocalStorage ? localStorageMap() : memoryMap<string>(); +const storage: ObservableMap<string, string> = (function buildStorage() { + if (supportBrowserStorage) { + return browserStorageMap(memoryMap<string>()); + } else if (supportLocalStorage) { + return localStorageMap(); + } else { + return memoryMap<string>(); + } +})(); export function useLocalStorage( key: string, |