1
1
import React , { useState , useEffect } from 'react' ;
2
2
import { ThemeProvider , CssBaseline , Box , Container , Tabs , Tab , Grid2 , Modal , Typography , Button , IconButton } from '@mui/material' ;
3
3
import CloseIcon from '@mui/icons-material/Close' ;
4
- import theme from './theme' ;
4
+ import Brightness4Icon from '@mui/icons-material/Brightness4' ;
5
+ import Brightness7Icon from '@mui/icons-material/Brightness7' ;
6
+ import { getTheme } from './theme' ;
5
7
import QueryInput from './components/QueryInput' ;
6
8
import ResultsDisplay from './components/ResultsDisplay' ;
7
9
import About from './components/About' ;
@@ -19,6 +21,26 @@ function App() {
19
21
const [ provider , setProvider ] = useState ( '' ) ;
20
22
const [ llmType , setLlmType ] = useState ( '' ) ;
21
23
const [ apiKey , setApiKey ] = useState ( '' ) ;
24
+ const [ mode , setMode ] = useState ( ( ) => {
25
+ const savedMode = localStorage . getItem ( 'theme-mode' ) ;
26
+ if ( savedMode ) {
27
+ return savedMode ;
28
+ }
29
+ return window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ? 'dark' : 'light' ;
30
+ } ) ;
31
+
32
+ const theme = React . useMemo ( ( ) => getTheme ( mode ) , [ mode ] ) ;
33
+
34
+ useEffect ( ( ) => {
35
+ const mediaQuery = window . matchMedia ( '(prefers-color-scheme: dark)' ) ;
36
+ const handleChange = ( e ) => {
37
+ if ( ! localStorage . getItem ( 'theme-mode' ) ) {
38
+ setMode ( e . matches ? 'dark' : 'light' ) ;
39
+ }
40
+ } ;
41
+ mediaQuery . addListener ( handleChange ) ;
42
+ return ( ) => mediaQuery . removeListener ( handleChange ) ;
43
+ } , [ ] ) ;
22
44
23
45
useEffect ( ( ) => {
24
46
axios . get ( '/csrf-token/' , { withCredentials : true } )
@@ -60,10 +82,27 @@ function App() {
60
82
setSelectedQuery ( query ) ;
61
83
}
62
84
85
+ const toggleColorMode = ( ) => {
86
+ const newMode = mode === 'light' ? 'dark' : 'light' ;
87
+ setMode ( newMode ) ;
88
+ localStorage . setItem ( 'theme-mode' , newMode ) ;
89
+ } ;
90
+
63
91
return (
64
92
< ThemeProvider theme = { theme } >
65
93
< CssBaseline />
66
- < Box sx = { { minHeight : '100vh' , backgroundColor : 'background.default' , mt : { xs : 4 , sm : 6 , md : 10 } , mb : { xs : 4 , sm : 6 , md : 10 } } } >
94
+ < Box sx = { { minHeight : '100vh' , backgroundColor : 'background.default' , mt : { xs : 4 , sm : 6 , md : 10 } , mb : { xs : 4 , sm : 6 , md : 10 } , position : 'relative' } } >
95
+ < IconButton
96
+ onClick = { toggleColorMode }
97
+ sx = { {
98
+ position : 'absolute' ,
99
+ right : 16 ,
100
+ top : 16 ,
101
+ color : 'text.primary'
102
+ } }
103
+ >
104
+ { mode === 'dark' ? < Brightness7Icon /> : < Brightness4Icon /> }
105
+ </ IconButton >
67
106
< Container maxWidth = "lg" sx = { { px : { xs : 2 , sm : 3 , md : 4 } } } >
68
107
< Typography
69
108
variant = "h2"
0 commit comments