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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  | 
import React, { createContext, useCallback, useContext, useEffect, useState } from "react";
import { Theme, ThemeProvider } from "@mui/material";
import { LocalStorageThemeName } from "@constants/LocalStorage";
import { localStorageAvailable } from "@services/LocalStorage";
import * as themes from "@themes/index";
import { getTheme } from "@utils/Configuration";
const MediaQueryDarkMode = "(prefers-color-scheme: dark)";
export const ThemeContext = createContext<ValueProps | null>(null);
export interface Props {
    children: React.ReactNode;
}
export interface ValueProps {
    theme: Theme;
    themeName: string;
    setThemeName: (value: string) => void;
}
export default function ThemeContextProvider(props: Props) {
    const [theme, setTheme] = useState(GetCurrentTheme());
    const [themeName, setThemeName] = useState(GetCurrentThemeName());
    const isLocalStorageAvailable = localStorageAvailable();
    useEffect(() => {
        if (themeName === themes.ThemeNameAuto) {
            const query = window.matchMedia(MediaQueryDarkMode);
            if (query.addEventListener) {
                query.addEventListener("change", mediaQueryListener);
                return () => {
                    query.removeEventListener("change", mediaQueryListener);
                };
            }
        }
        setTheme(ThemeFromName(themeName));
    }, [themeName]);
    useEffect(() => {
        window.addEventListener("storage", storageListener);
        return () => {
            window.removeEventListener("storage", storageListener);
        };
    }, []);
    const storageListener = (ev: StorageEvent): any => {
        if (ev.key !== LocalStorageThemeName) {
            return;
        }
        if (ev.newValue && ev.newValue !== "") {
            setThemeName(ev.newValue);
        } else {
            setThemeName(getUserThemeName());
        }
    };
    const mediaQueryListener = (ev: MediaQueryListEvent) => {
        setTheme(ev.matches ? themes.Dark : themes.Light);
    };
    const callback = useCallback(
        (name: string) => {
            setThemeName(name);
            if (isLocalStorageAvailable) {
                window.localStorage.setItem(LocalStorageThemeName, name);
            }
        },
        [isLocalStorageAvailable],
    );
    return (
        <ThemeContext.Provider
            value={{
                theme,
                themeName,
                setThemeName: callback,
            }}
        >
            <ThemeWrapper>{props.children}</ThemeWrapper>
        </ThemeContext.Provider>
    );
}
export function useThemeContext() {
    const context = useContext(ThemeContext);
    if (!context) {
        throw new Error("useThemeContext must be used within a ThemeContextProvider");
    }
    return context;
}
function ThemeWrapper(props: Props) {
    const { theme } = useThemeContext();
    return <ThemeProvider theme={theme}>{props.children}</ThemeProvider>;
}
function GetCurrentThemeName() {
    if (localStorageAvailable()) {
        const local = window.localStorage.getItem(LocalStorageThemeName);
        if (local) {
            return local;
        }
    }
    return getTheme();
}
function GetCurrentTheme() {
    return ThemeFromName(GetCurrentThemeName());
}
function ThemeFromName(name: string) {
    switch (name) {
        case themes.ThemeNameLight:
            return themes.Light;
        case themes.ThemeNameDark:
            return themes.Dark;
        case themes.ThemeNameGrey:
            return themes.Grey;
        case themes.ThemeNameAuto:
            return window.matchMedia(MediaQueryDarkMode).matches ? themes.Dark : themes.Light;
        default:
            return window.matchMedia(MediaQueryDarkMode).matches ? themes.Dark : themes.Light;
    }
}
const getUserThemeName = () => {
    if (localStorageAvailable()) {
        const value = window.localStorage.getItem(LocalStorageThemeName);
        if (value) {
            return value;
        }
    }
    return getTheme();
};
  |