|
| 1 | +import React, { createContext, useState, useContext } from "react"; |
| 2 | +import { getLocalStorageTheme, setLocalStorageTheme } from './local-storage'; |
| 3 | + |
| 4 | +const themeDefinitions = { |
| 5 | + swiss: { |
| 6 | + stripeBgClass: "bg-red-600", |
| 7 | + occurrenceClosedHitBgColor: "bg-red-600", |
| 8 | + occurrenceClosedMissBgColor: "bg-gray-100", |
| 9 | + occurrenceOpenBgColor: "bg-white", |
| 10 | + buttonTextColor: "text-red-600", |
| 11 | + buttonBgColor: "bg-transparent", |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +const DEFAULT_THEME = 'swiss'; |
| 16 | +export const themeOptions = Object.keys(themeDefinitions); |
| 17 | + |
| 18 | +const ThemeContext = createContext(); |
| 19 | + |
| 20 | +export const ThemeProvider = ({ children }) => { |
| 21 | + debugger; |
| 22 | + const { theme: localStorageTheme } = getLocalStorageTheme() ?? {}; |
| 23 | + const initialThemeName = localStorageTheme ?? DEFAULT_THEME; |
| 24 | + const [themeName, setThemeName] = useState(initialThemeName); |
| 25 | + |
| 26 | + const saveTheme = (theme) => { |
| 27 | + if (themeOptions.includes(theme)) { |
| 28 | + setLocalStorageTheme({ theme }); |
| 29 | + setThemeName(theme); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + return ( |
| 34 | + <ThemeContext.Provider value={[themeDefinitions[themeName], saveTheme]}> |
| 35 | + {children} |
| 36 | + </ThemeContext.Provider> |
| 37 | + ); |
| 38 | +}; |
| 39 | + |
| 40 | +export const useTheme = () => useContext(ThemeContext); |
0 commit comments