2024-01-25 16:51:40 -08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { ThemeProvider } from 'styled-components';
|
2025-04-16 16:55:38 -07:00
|
|
|
|
2025-04-30 11:21:41 -04:00
|
|
|
import { loadThemeIdFromLocalStorage } from '@app/useSetAppTheme';
|
2025-04-16 16:55:38 -07:00
|
|
|
import defaultThemeConfig from '@conf/theme/theme_light.config.json';
|
|
|
|
import { Theme } from '@conf/theme/types';
|
|
|
|
import { CustomThemeContext } from '@src/customThemeContext';
|
2024-01-25 16:51:40 -08:00
|
|
|
|
2024-03-08 12:16:53 -08:00
|
|
|
interface Props {
|
|
|
|
children: React.ReactNode;
|
|
|
|
skipSetTheme?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CustomThemeProvider = ({ children, skipSetTheme }: Props) => {
|
2024-01-25 16:51:40 -08:00
|
|
|
const [currentTheme, setTheme] = useState<Theme>(defaultThemeConfig);
|
2025-04-30 11:21:41 -04:00
|
|
|
const customThemeId = loadThemeIdFromLocalStorage();
|
2024-01-25 16:51:40 -08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2025-04-30 11:21:41 -04:00
|
|
|
// use provided customThemeId and set in useSetAppTheme.tsx if it exists
|
|
|
|
if (customThemeId) return;
|
|
|
|
|
2024-01-25 16:51:40 -08:00
|
|
|
if (import.meta.env.DEV) {
|
|
|
|
import(/* @vite-ignore */ `./conf/theme/${import.meta.env.REACT_APP_THEME_CONFIG}`).then((theme) => {
|
|
|
|
setTheme(theme);
|
|
|
|
});
|
2024-03-08 12:16:53 -08:00
|
|
|
} else if (!skipSetTheme) {
|
2024-01-25 16:51:40 -08:00
|
|
|
// Send a request to the server to get the theme config.
|
|
|
|
fetch(`/assets/conf/theme/${import.meta.env.REACT_APP_THEME_CONFIG}`)
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((theme) => {
|
|
|
|
setTheme(theme);
|
|
|
|
});
|
|
|
|
}
|
2025-04-30 11:21:41 -04:00
|
|
|
}, [skipSetTheme, customThemeId]);
|
2024-01-25 16:51:40 -08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<CustomThemeContext.Provider value={{ theme: currentTheme, updateTheme: setTheme }}>
|
|
|
|
<ThemeProvider theme={currentTheme}>{children}</ThemeProvider>
|
|
|
|
</CustomThemeContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CustomThemeProvider;
|