@@ -28,6 +28,57 @@ export default function App() {
2828 }
2929 } , [ userId , isLoading , error , isAuthenticated ] ) ;
3030
31+ // Load theme preference from backend when user authenticates
32+ useEffect ( ( ) => {
33+ if ( isAuthenticated && userId ) {
34+ console . log ( '🎨 [Theme] Loading theme preference for user:' , userId ) ;
35+
36+ fetch ( `/api/theme-preference?userId=${ encodeURIComponent ( userId ) } ` )
37+ . then ( res => res . json ( ) )
38+ . then ( data => {
39+ if ( data . theme ) {
40+ console . log ( '🎨 [Theme] Loaded theme preference:' , data . theme ) ;
41+ setIsDark ( data . theme === 'dark' ) ;
42+ }
43+ } )
44+ . catch ( error => {
45+ console . error ( '🎨 [Theme] Failed to load theme preference:' , error ) ;
46+ // Keep default theme on error
47+ } ) ;
48+ }
49+ } , [ isAuthenticated , userId ] ) ;
50+
51+ // Handle theme change and save to backend
52+ const handleThemeChange = async ( newIsDark : boolean ) => {
53+ // Update UI immediately for responsive feel
54+ setIsDark ( newIsDark ) ;
55+
56+ // Save to backend if user is authenticated
57+ if ( userId ) {
58+ const theme = newIsDark ? 'dark' : 'light' ;
59+ console . log ( `🎨 [Theme] Saving theme preference for user ${ userId } :` , theme ) ;
60+
61+ try {
62+ const response = await fetch ( '/api/theme-preference' , {
63+ method : 'POST' ,
64+ headers : { 'Content-Type' : 'application/json' } ,
65+ body : JSON . stringify ( { userId, theme } )
66+ } ) ;
67+
68+ const data = await response . json ( ) ;
69+
70+ if ( data . success ) {
71+ console . log ( '🎨 [Theme] Theme preference saved successfully:' , theme ) ;
72+ } else {
73+ console . error ( '🎨 [Theme] Failed to save theme preference:' , data ) ;
74+ }
75+ } catch ( error ) {
76+ console . error ( '🎨 [Theme] Error saving theme preference:' , error ) ;
77+ // Continue using the theme locally even if save fails
78+ }
79+ }
80+ } ;
81+
3182 // Handle loading state
3283 if ( isLoading ) {
3384 return (
@@ -56,16 +107,16 @@ export default function App() {
56107 }
57108
58109 // Handle unauthenticated state
59- if ( ! isAuthenticated || ! userId ) {
60- return (
61- < div className = "min-h-screen flex items-center justify-center bg-slate-900" >
62- < div className = "text-center p-8" >
63- < h2 className = "text-red-500 text-2xl font-semibold mb-4" > Not Authenticated</ h2 >
64- < p className = "text-gray-400" > Please open this page from the MentraOS manager app.</ p >
65- </ div >
66- </ div >
67- ) ;
68- }
110+ // if (!isAuthenticated || !userId) {
111+ // return (
112+ // <div className="min-h-screen flex items-center justify-center bg-slate-900">
113+ // <div className="text-center p-8">
114+ // <h2 className="text-red-500 text-2xl font-semibold mb-4">Not Authenticated</h2>
115+ // <p className="text-gray-400">Please open this page from the MentraOS manager app.</p>
116+ // </div>
117+ // </div>
118+ // );
119+ // }
69120
70121 return (
71122 < div className = { `min-h-screen ${ isDark ? 'dark' : 'light' } ` } style = { {
@@ -138,7 +189,11 @@ export default function App() {
138189 < div className = "relative" >
139190 < button
140191 onClick = { ( ) => setIsDropdownOpen ( ! isDropdownOpen ) }
141- className = "flex items-center gap-2 px-3 py-1.5 text-sm font-medium text-[#00e2a2] bg-slate-800/50 rounded-md"
192+ className = { `flex items-center gap-2 px-3 py-1.5 text-sm font-medium rounded-md transition-colors ${
193+ isDark
194+ ? 'text-[#00e2a2] bg-slate-800/50'
195+ : 'text-emerald-700 bg-emerald-100/80'
196+ } `}
142197 >
143198 { activeTab === 'home' ? 'Home' : 'Template' }
144199 < svg
@@ -188,7 +243,7 @@ export default function App() {
188243
189244 { /* Content */ }
190245 < main >
191- { activeTab === 'home' ? < Home /> : < Template isDark = { isDark } setIsDark = { setIsDark } userId = { userId } /> }
246+ { activeTab === 'home' ? < Home /> : < Template isDark = { isDark } setIsDark = { handleThemeChange } userId = { userId || '' } /> }
192247 </ main >
193248 </ div >
194249 ) ;
0 commit comments