1
1
import { useContext , createContext , ReactNode , useEffect } from 'react' ;
2
- import { useMutation , useQuery } from '@apollo/client' ;
3
2
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' ;
17
3
import useLocalStorage from '../../hooks/useLocalStorage' ;
18
4
import { CODIGA_THEME } from '../../lib/config' ;
19
- import { UserPreferenceKey } from '../../lib/constants' ;
20
5
21
6
enum Theme {
22
7
THEME_DARK = 'theme-dark' ,
@@ -29,16 +14,13 @@ type ThemeType = Theme.THEME_DARK | Theme.THEME_LIGHT;
29
14
type CacheStorageThemeType = ( theme : ThemeType ) => void ;
30
15
31
16
type ThemeContextType = {
32
- changeToDarkTheme : ( ) => Promise < void > ;
33
- changeToLightTheme : ( ) => Promise < void > ;
17
+ changeToDarkTheme : ( ) => void ;
18
+ changeToLightTheme : ( ) => void ;
34
19
} ;
35
20
36
21
const ThemeContext = createContext ( { } as ThemeContextType ) ;
37
22
38
23
export const ThemeProvider = ( { children } : { children : ReactNode } ) => {
39
- const toast = useToast ( ) ;
40
- const { id : userId } = useUser ( ) ;
41
-
42
24
// CHAKRA'S THEME
43
25
const { setColorMode } = useColorMode ( ) ;
44
26
// USER'S LOCAL THEME PREFERENCE
@@ -47,25 +29,6 @@ export const ThemeProvider = ({ children }: { children: ReactNode }) => {
47
29
Theme . THEME_LIGHT
48
30
) as [ ThemeType , CacheStorageThemeType , ( ) => string ] ;
49
31
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
-
69
32
/**
70
33
* Because a user can access the app while not authenticated,
71
34
* we need to update the theme based on locally stored settings
@@ -76,57 +39,14 @@ export const ThemeProvider = ({ children }: { children: ReactNode }) => {
76
39
setColorMode ( storedTheme === Theme . THEME_DARK ? 'dark' : 'light' ) ;
77
40
} , [ cacheStorageTheme , hydrateValue , setColorMode ] ) ;
78
41
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 = ( ) => {
92
43
cacheStorageTheme ( Theme . THEME_DARK ) ;
93
44
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
- }
110
45
} ;
111
46
112
- const changeToLightTheme = async ( ) => {
47
+ const changeToLightTheme = ( ) => {
113
48
cacheStorageTheme ( Theme . THEME_LIGHT ) ;
114
49
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
- }
130
50
} ;
131
51
132
52
const themeContext = {
0 commit comments