Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 57a8216

Browse files
feat: updated theme logic to follow the system setting
1 parent 871504f commit 57a8216

File tree

2 files changed

+6
-87
lines changed

2 files changed

+6
-87
lines changed

src/renderer/App.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ApolloProvider } from '@apollo/client';
2-
import { ChakraProvider } from '@chakra-ui/react';
2+
import { ChakraProvider, ColorModeScript } from '@chakra-ui/react';
33
import { theme } from '@codiga/components';
44
import { MemoryRouter as Router, Routes, Route } from 'react-router-dom';
55

@@ -26,12 +26,11 @@ import Filters from './components/Filters/Filters';
2626
import { UserProvider } from './components/UserContext';
2727
import { ThemeProvider } from './components/ThemeContext';
2828
import { FiltersProvider } from './components/FiltersContext';
29-
import ViewSnippet from './pages/ViewSnippet';
30-
import ViewCookbookSnippets from './pages/ViewCookbookSnippets';
3129

3230
export default function App() {
3331
return (
3432
<ApolloProvider client={client}>
33+
<ColorModeScript initialColorMode="system" />
3534
<ChakraProvider theme={theme}>
3635
<UserProvider>
3736
<ThemeProvider>

src/renderer/components/ThemeContext/ThemeContext.tsx

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
import { useContext, createContext, ReactNode, useEffect } from 'react';
2-
import { useMutation, useQuery } from '@apollo/client';
32
import { useColorMode } from '@chakra-ui/react';
4-
import { useToast } from '@codiga/components';
5-
6-
import { useUser } from '../UserContext';
7-
import { User } from '../../types/userTypes';
8-
import { GET_USER_PREFERENCES } from '../../graphql/queries';
9-
import {
10-
RemoveUserPreferenceData,
11-
RemoveUserPreferenceVariables,
12-
REMOVE_USER_PREFERENCE,
13-
UpdateUserPreferenceData,
14-
UpdateUserPreferenceVariables,
15-
UPDATE_USER_PREFERENCE,
16-
} from '../../graphql/mutations';
173
import useLocalStorage from '../../hooks/useLocalStorage';
184
import { CODIGA_THEME } from '../../lib/config';
19-
import { UserPreferenceKey } from '../../lib/constants';
205

216
enum Theme {
227
THEME_DARK = 'theme-dark',
@@ -29,16 +14,13 @@ type ThemeType = Theme.THEME_DARK | Theme.THEME_LIGHT;
2914
type CacheStorageThemeType = (theme: ThemeType) => void;
3015

3116
type ThemeContextType = {
32-
changeToDarkTheme: () => Promise<void>;
33-
changeToLightTheme: () => Promise<void>;
17+
changeToDarkTheme: () => void;
18+
changeToLightTheme: () => void;
3419
};
3520

3621
const ThemeContext = createContext({} as ThemeContextType);
3722

3823
export const ThemeProvider = ({ children }: { children: ReactNode }) => {
39-
const toast = useToast();
40-
const { id: userId } = useUser();
41-
4224
// CHAKRA'S THEME
4325
const { setColorMode } = useColorMode();
4426
// USER'S LOCAL THEME PREFERENCE
@@ -47,25 +29,6 @@ export const ThemeProvider = ({ children }: { children: ReactNode }) => {
4729
Theme.THEME_LIGHT
4830
) as [ThemeType, CacheStorageThemeType, () => string];
4931

50-
/**
51-
* If the user is logged in, we check if they have a saved theme preference.
52-
* If they do, we'll update Chakra and the local version
53-
*/
54-
useQuery<{ user: Partial<User> }>(GET_USER_PREFERENCES, {
55-
skip: !!userId,
56-
onCompleted: (respData) => {
57-
if (respData?.user?.preferences) {
58-
const savedTheme = respData.user.preferences
59-
.map((preference) => preference.key)
60-
.includes(UserPreferenceKey.Theme)
61-
? Theme.THEME_DARK
62-
: Theme.THEME_LIGHT;
63-
cacheStorageTheme(savedTheme);
64-
setColorMode(savedTheme === Theme.THEME_DARK ? 'dark' : 'light');
65-
}
66-
},
67-
});
68-
6932
/**
7033
* Because a user can access the app while not authenticated,
7134
* we need to update the theme based on locally stored settings
@@ -76,57 +39,14 @@ export const ThemeProvider = ({ children }: { children: ReactNode }) => {
7639
setColorMode(storedTheme === Theme.THEME_DARK ? 'dark' : 'light');
7740
}, [cacheStorageTheme, hydrateValue, setColorMode]);
7841

79-
// used to set a DARK theme
80-
const [removeUserPreference] = useMutation<
81-
RemoveUserPreferenceData,
82-
RemoveUserPreferenceVariables
83-
>(REMOVE_USER_PREFERENCE);
84-
85-
// used to set a LIGHT theme
86-
const [updateUserPreference] = useMutation<
87-
UpdateUserPreferenceData,
88-
UpdateUserPreferenceVariables
89-
>(UPDATE_USER_PREFERENCE);
90-
91-
const changeToDarkTheme = async () => {
42+
const changeToDarkTheme = () => {
9243
cacheStorageTheme(Theme.THEME_DARK);
9344
setColorMode('dark');
94-
if (!userId) return;
95-
try {
96-
await updateUserPreference({
97-
variables: {
98-
key: UserPreferenceKey.Theme,
99-
value: Theme.THEME_DARK,
100-
},
101-
refetchQueries: [{ query: GET_USER_PREFERENCES }],
102-
});
103-
} catch (err) {
104-
toast({
105-
status: 'error',
106-
description:
107-
'An error occurred while updating your theme. Please try again.',
108-
});
109-
}
11045
};
11146

112-
const changeToLightTheme = async () => {
47+
const changeToLightTheme = () => {
11348
cacheStorageTheme(Theme.THEME_LIGHT);
11449
setColorMode('light');
115-
if (!userId) return;
116-
try {
117-
await removeUserPreference({
118-
variables: {
119-
key: UserPreferenceKey.Theme,
120-
},
121-
refetchQueries: [{ query: GET_USER_PREFERENCES }],
122-
});
123-
} catch (err) {
124-
toast({
125-
status: 'error',
126-
description:
127-
'An error occurred while updating your theme. Please try again.',
128-
});
129-
}
13050
};
13151

13252
const themeContext = {

0 commit comments

Comments
 (0)