Skip to content

Commit

Permalink
Add ability to reset provider preference
Browse files Browse the repository at this point in the history
  • Loading branch information
mbthiery committed Oct 25, 2023
1 parent b957506 commit 3744f2d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/context/usePreferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from "react"

Expand All @@ -17,15 +18,32 @@ const PreferencesContext = createContext<PreferencesContext>({
provider: undefined,
setProvider: () => undefined,
})

const PROVIDER_KEY = "provider"
const VERSION_KEY = "version"
// change version to reset provider preference
const VERSION = "3"

const getProvider = (providerLabel: string) => {
const getProvider = (providerLabel?: string) => {
return PROVIDERS.find((provider) => provider.label === providerLabel)
}

const getLocalValue = (key: string) => {
if (!window) return undefined
return localStorage.getItem(key)
}

export const PreferencesProvider = ({ children }: PropsWithChildren) => {
const localVersion = getLocalValue(VERSION_KEY)

useEffect(() => {
if (!!window) localStorage?.setItem(VERSION_KEY, VERSION)
}, [])

const [provider, setProvider] = useState(
!!window ? getProvider(localStorage.getItem(PROVIDER_KEY) || "") : undefined
localVersion === VERSION
? getProvider(getLocalValue(PROVIDER_KEY) || "")
: undefined
)

const setProviderCB = useCallback(
Expand Down

0 comments on commit 3744f2d

Please sign in to comment.