blob: 35a4fed136acc038a5d8aab441bea3c1f6281a05 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import React, { createContext, useState, useEffect, useContext } from 'react'
import translations from 'configs/translations'
import useUser from 'hooks/useUser'
const SettingsContext = createContext()
const defaultSettings = { theme: 'green', language: 'en' }
export const SettingsProvider = ({ children }) => {
const { user } = useUser()
const [data, setData] = useState()
const setSettings = s => {
if (typeof window !== 'undefined') {
window.localStorage.setItem('loggedOutSettings', JSON.stringify(s(data)))
}
setData(s)
}
const t = key => data && data.language && translations && translations.en[key]
? translations[data.language][key]
? translations[data.language][key]
: translations.en[key]
: `*${key}*`
useEffect(() => {
const loggedOutSettings = JSON.parse(localStorage.getItem('loggedOutSettings'))
if (user && user.isLoggedIn) {
const settings = { theme: user.theme, language: user.language }
if (typeof window !== 'undefined') {
window.localStorage.setItem('loggedOutSettings', JSON.stringify(settings))
}
setData(settings)
} else if (loggedOutSettings) {
setData(loggedOutSettings)
} else {
setData(defaultSettings)
}
}, [user])
return (
<SettingsContext.Provider value={{ settings: data, setSettings, t }}>
{children}
</SettingsContext.Provider>
)
}
const useSettings = () => useContext(SettingsContext)
export default useSettings
|