Skip to content

Commit 544769e

Browse files
authored
Merge pull request #72 from bathini79/feat/60-store-colorschema-in-localstorage
feat(ui): Added And Received ColorScheme from LocalStorage #60 #45
2 parents 3f72cc2 + f41aff9 commit 544769e

2 files changed

Lines changed: 36 additions & 23 deletions

File tree

platform/firecamp-ui/src/components/theme/FirecampThemeProvider.tsx

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,52 +75,64 @@ export const useFirecampStyle = createStyles((theme) => ({
7575
}));
7676

7777
const FirecampThemeProvider: FC<IFirecampThemeProvider> = ({
78-
themeVariant = EFirecampThemeVariant.LightPrimary,
78+
themeVariant,
7979
children,
8080
...props
8181
}) => {
82-
const [colorScheme, setColorScheme] = useState<ColorScheme>('light');
83-
const [themeColor, updatethemeColor] = useState<EFirecampThemeVariant>(
84-
EFirecampThemeVariant.LightPrimary
85-
);
8682

87-
useEffect(() => {
88-
updateTheme(themeVariant);
89-
}, [themeVariant]);
83+
const _initialize = () => {
84+
// on first time load set theme from local storage, if not found then set default
85+
let themeStored = localStorage.getItem('theme') as EFirecampThemeVariant;
86+
if (!Object.values(EFirecampThemeVariant).includes(themeStored)) {
87+
themeStored = EFirecampThemeVariant.LightPrimary
88+
}
89+
return themeStored;
90+
}
9091

91-
// update theme colorScheme & primaryColor
92-
const updateTheme = (color: EFirecampThemeVariant) => {
93-
// update the primary color
94-
updatethemeColor(color);
92+
const [colorScheme, setColorScheme] = useState<ColorScheme>('light');
93+
const [theme, setTheme] = useState<EFirecampThemeVariant>(_initialize);
94+
95+
// update theme
96+
const updateTheme = (theme: EFirecampThemeVariant) => {
9597

9698
// update the color schema
9799
setColorScheme(
98100
[
99101
EFirecampThemeVariant.LightPrimary,
100102
EFirecampThemeVariant.LightSecondary,
101-
].includes(color)
103+
].includes(theme)
102104
? 'light'
103105
: 'dark'
104106
);
105107

108+
// save in local storage
109+
localStorage.setItem("theme", theme);
110+
111+
// update theme
112+
setTheme(theme);
113+
114+
updateMonacoEditorTheme(theme)
115+
};
116+
117+
const updateMonacoEditorTheme = (theme: EFirecampThemeVariant) => {
106118
// update the monaco editor theme
107119
const editorTheme = [
108120
EFirecampThemeVariant.LightPrimary,
109121
EFirecampThemeVariant.LightSecondary,
110-
].includes(color)
122+
].includes(theme)
111123
? EEditorTheme.Lite
112124
: EEditorTheme.Dark;
113125

114126
localStorage.setItem('editorTheme', editorTheme);
115127
EditorApi.setEditorTheme(editorTheme);
116-
};
128+
}
117129

118130
return (
119131
//@ts-ignore
120-
<ColorSchemeProvider colorScheme={themeColor}
121-
toggleColorScheme={(c: ColorScheme | EFirecampThemeVariant) =>
122-
updateTheme(c as EFirecampThemeVariant)
123-
}
132+
<ColorSchemeProvider colorScheme={theme}
133+
toggleColorScheme={(c: ColorScheme | EFirecampThemeVariant) => {
134+
updateTheme(c as EFirecampThemeVariant);
135+
}}
124136
>
125137
<MantineProvider
126138
theme={{
@@ -438,7 +450,7 @@ const FirecampThemeProvider: FC<IFirecampThemeProvider> = ({
438450
'primary-color': [
439451
EFirecampThemeVariant.LightPrimary,
440452
EFirecampThemeVariant.DarkPrimary,
441-
].includes(themeColor)
453+
].includes(theme)
442454
? primaryColor
443455
: secondaryColor,
444456
dark: defaultDarkColor,

platform/firecamp-ui/src/components/theme/FirecampThemeSelector.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ const FirecampThemeSelector = () => {
3535
const _setTheme = (t: any) => {
3636
try {
3737
// Set app body theme - for matching tailwind theme
38-
document.body.className = `theme-${t.split('-')[0] || 'light'} primary-${
39-
t.split('-')[1] || 'orange'
40-
}`;
38+
const themeMode = t.split('-')[0] || 'light';
39+
const themeColor = t.split('-')[1] === 'primary' ? 'orange' : 'green';
40+
document.body.className = `theme-${themeMode} primary-${themeColor}`;
4141
} catch (error) {
4242
console.log({ error });
4343
}
@@ -47,6 +47,7 @@ const FirecampThemeSelector = () => {
4747
const activeTheme = ThemeOptions.find(
4848
(t) => t.value === (colorScheme as EFirecampThemeVariant)
4949
);
50+
if (!activeTheme) return <></>;
5051
return (
5152
<DropdownMenu
5253
onOpenChange={(v) => toggleOpen(v)}

0 commit comments

Comments
 (0)