1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-11-15 01:34:57 +01:00
market/src/providers/UserPreferences.tsx
Jamie Hewitt 7672d78aee
Refactoring Market for new Ocean.js build (#481)
* removing imports from utils folder

* lib bump

* update status args

Co-authored-by: mihaisc <mihai.scarlat@smartcontrol.ro>
2021-04-13 15:13:55 +02:00

128 lines
3.2 KiB
TypeScript

import React, {
createContext,
useContext,
ReactElement,
ReactNode,
useState,
useEffect
} from 'react'
import { Logger, LogLevel, ConfigHelperConfig } from '@oceanprotocol/lib'
import { useOcean } from './Ocean'
import { isBrowser } from '../utils'
interface UserPreferencesValue {
debug: boolean
currency: string
locale: string
bookmarks: {
[network: string]: string[]
}
setDebug: (value: boolean) => void
setCurrency: (value: string) => void
addBookmark: (did: string) => void
removeBookmark: (did: string) => void
}
const UserPreferencesContext = createContext(null)
const localStorageKey = 'ocean-user-preferences'
function getLocalStorage(): UserPreferencesValue {
const storageParsed =
isBrowser && JSON.parse(window.localStorage.getItem(localStorageKey))
return storageParsed
}
function setLocalStorage(values: Partial<UserPreferencesValue>) {
return (
isBrowser &&
window.localStorage.setItem(localStorageKey, JSON.stringify(values))
)
}
function UserPreferencesProvider({
children
}: {
children: ReactNode
}): ReactElement {
const { config } = useOcean()
const networkName = (config as ConfigHelperConfig).network
const localStorage = getLocalStorage()
// Set default values from localStorage
const [debug, setDebug] = useState<boolean>(localStorage?.debug || false)
const [currency, setCurrency] = useState<string>(
localStorage?.currency || 'EUR'
)
const [locale, setLocale] = useState<string>()
const [bookmarks, setBookmarks] = useState(localStorage?.bookmarks || {})
// Write values to localStorage on change
useEffect(() => {
setLocalStorage({ debug, currency, bookmarks })
}, [debug, currency, bookmarks])
// Set ocean.js log levels, default: Error
useEffect(() => {
debug === true
? Logger.setLevel(LogLevel.Verbose)
: Logger.setLevel(LogLevel.Error)
}, [debug])
// Get locale always from user's browser
useEffect(() => {
if (!window) return
setLocale(window.navigator.language)
}, [])
function addBookmark(didToAdd: string): void {
const newPinned = {
...bookmarks,
[networkName]: [didToAdd].concat(bookmarks[networkName])
}
setBookmarks(newPinned)
}
function removeBookmark(didToAdd: string): void {
const newPinned = {
...bookmarks,
[networkName]: bookmarks[networkName].filter(
(did: string) => did !== didToAdd
)
}
setBookmarks(newPinned)
}
// Bookmarks old data structure migration
useEffect(() => {
if (!bookmarks.length) return
const newPinned = { mainnet: bookmarks as any }
setBookmarks(newPinned)
}, [bookmarks])
return (
<UserPreferencesContext.Provider
value={
{
debug,
currency,
locale,
bookmarks,
setDebug,
setCurrency,
addBookmark,
removeBookmark
} as UserPreferencesValue
}
>
{children}
</UserPreferencesContext.Provider>
)
}
// Helper hook to access the provider values
const useUserPreferences = (): UserPreferencesValue =>
useContext(UserPreferencesContext)
export { UserPreferencesProvider, useUserPreferences, UserPreferencesValue }