Skip to content

Commit e55bd0f

Browse files
authored
Merge pull request #383 from qa-guru/QAGDEV-732
QAGDEV-732 - Слетает тёмная тема при обновлении страницы
2 parents 46001ed + 6e42300 commit e55bd0f

File tree

4 files changed

+52
-31
lines changed

4 files changed

+52
-31
lines changed

src/app/providers/with-context.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ export const withContext = (component: () => ReactNode) => {
88
return function WithContextComponent() {
99
const { settings } = useSettings();
1010

11-
const theme = createCustomTheme({
12-
theme: settings.theme,
13-
responsiveFontSizes: settings.responsiveFontSizes,
14-
});
11+
const theme = createCustomTheme(settings);
1512

1613
return <ThemeProvider theme={theme}>{component()}</ThemeProvider>;
1714
};

src/cache.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,32 @@ import { initialSettings, themeSettingsTypes } from "theme";
44
import { THEMES } from "theme/constans";
55
import { Maybe, UserRole } from "api/graphql/generated/graphql";
66

7+
// Функция для загрузки настроек из localStorage
8+
const loadSettingsFromStorage = (): themeSettingsTypes => {
9+
try {
10+
const storedSettings = window.localStorage.getItem("settings");
11+
if (storedSettings) {
12+
const parsedSettings = JSON.parse(storedSettings);
13+
// Проверяем, что загруженные настройки валидны
14+
if (parsedSettings && typeof parsedSettings.theme === "string") {
15+
return parsedSettings;
16+
}
17+
}
18+
} catch (error) {
19+
// Silent fail - fallback to initial settings
20+
}
21+
return initialSettings;
22+
};
23+
724
export const userIdVar = makeVar<Maybe<string>>(null);
825
export const userRolesVar = makeVar<Maybe<Maybe<UserRole>[]>>([]);
926
export const commentVar = makeVar<Maybe<string | undefined>>(null);
10-
export const settingsVar = makeVar<themeSettingsTypes>(initialSettings);
27+
28+
// Инициализируем настройки из localStorage
29+
const savedSettings = loadSettingsFromStorage();
30+
export const settingsVar = makeVar<themeSettingsTypes>(savedSettings);
1131
export const lightThemeVar = makeVar<boolean>(
12-
initialSettings.theme === THEMES.LIGHT
32+
savedSettings.theme === THEMES.LIGHT
1333
);
1434

1535
export const cache = new InMemoryCache();
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
import { useEffect, useState } from "react";
2-
31
import { themeSettingsTypes } from "theme";
42

53
export const useLocalStorage = (
64
key: string,
75
initialValue: themeSettingsTypes
86
) => {
9-
const [data, setData] = useState(initialValue);
10-
11-
useEffect(() => {
12-
const getData = window.localStorage.getItem(key);
13-
if (getData) {
14-
setData(JSON.parse(getData));
15-
}
16-
}, [key]);
17-
187
const storeData = (updateValue: themeSettingsTypes) => {
19-
setData(updateValue);
20-
window.localStorage.setItem(key, JSON.stringify(updateValue));
8+
try {
9+
window.localStorage.setItem(key, JSON.stringify(updateValue));
10+
} catch (error) {
11+
// Silent fail
12+
}
2113
};
2214

23-
return { data, storeData };
15+
return { storeData };
2416
};

src/theme/index.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createTheme, responsiveFontSizes } from "@mui/material";
1+
import { createTheme, responsiveFontSizes, ThemeOptions } from "@mui/material";
22
import merge from "lodash/merge";
33

44
import components from "./components";
@@ -16,7 +16,7 @@ export const initialSettings: themeSettingsTypes = {
1616
responsiveFontSizes: true,
1717
};
1818

19-
const baseOptions = {
19+
const baseOptions: ThemeOptions = {
2020
typography: {
2121
fontFamily: "'Montserrat', sans-serif",
2222
h1: {
@@ -56,20 +56,32 @@ const baseOptions = {
5656
};
5757

5858
export const createCustomTheme = (settings: themeSettingsTypes) => {
59-
let themeOptions: object = themesOptions[settings.theme];
59+
// Валидируем настройки
60+
if (!settings || typeof settings.theme !== "string") {
61+
settings = { ...initialSettings };
62+
}
63+
64+
let themeOptions: ThemeOptions = themesOptions[
65+
settings.theme
66+
] as unknown as ThemeOptions;
6067

6168
if (!themeOptions) {
62-
themeOptions = themesOptions[THEMES.LIGHT];
69+
themeOptions = themesOptions[THEMES.LIGHT] as unknown as ThemeOptions;
6370
}
6471

65-
const mergedThemeOptions = merge({}, baseOptions, themeOptions);
72+
try {
73+
const mergedThemeOptions = merge({}, baseOptions, themeOptions);
6674

67-
let theme = createTheme(mergedThemeOptions);
75+
let theme = createTheme(mergedThemeOptions);
6876

69-
theme.components = components(theme);
70-
if (settings.responsiveFontSizes) {
71-
theme = responsiveFontSizes(theme);
72-
}
77+
theme.components = components(theme);
78+
if (settings.responsiveFontSizes !== false) {
79+
theme = responsiveFontSizes(theme);
80+
}
7381

74-
return theme;
82+
return theme;
83+
} catch (error) {
84+
// Возвращаем базовую светлую тему в случае ошибки
85+
return createTheme(themesOptions[THEMES.LIGHT] as unknown as ThemeOptions);
86+
}
7587
};

0 commit comments

Comments
 (0)