generated from google-gemini/aistudio-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
279 lines (245 loc) · 9.31 KB
/
App.tsx
File metadata and controls
279 lines (245 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* Overview: App.tsx
* Purpose: Implements part of the Eduvane application behavior for this module.
* Notes: Keep exports focused and update comments when behavior changes.
*/
import React, { useState, useEffect } from 'react';
import { LandingPage } from './components/standalone/LandingPage.tsx';
import { AuthScreen } from './components/standalone/AuthScreen.tsx';
import { Dashboard } from './components/standalone/Dashboard.tsx';
import { VaneIcon } from './constants.tsx';
import { Submission, PracticeSet, UserProfile } from './types.ts';
import { SupabaseService, supabase } from './services/SupabaseService.ts';
import { LogOut, Loader2, Info, Moon, Sun, Monitor } from 'lucide-react';
type Theme = 'light' | 'dark' | 'system';
const App: React.FC = () => {
const [session, setSession] = useState<any>(null);
const [profile, setProfile] = useState<UserProfile | null>(null);
const [showAuth, setShowAuth] = useState(false);
const [authMode, setAuthMode] = useState<'signin' | 'signup'>('signin');
const [loading, setLoading] = useState(true);
const [isGuest, setIsGuest] = useState(false);
const [submissions, setSubmissions] = useState<Submission[]>([]);
const [practiceSets, setPracticeSets] = useState<PracticeSet[]>([]);
const [guestSubmissions, setGuestSubmissions] = useState<Submission[]>([]);
const [guestPracticeSets, setGuestPracticeSets] = useState<PracticeSet[]>([]);
const [theme, setTheme] = useState<Theme>(() => {
return (localStorage.getItem('eduvane-theme') as Theme) || 'system';
});
useEffect(() => {
const root = window.document.documentElement;
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const applyTheme = () => {
const isDark = theme === 'dark' || (theme === 'system' && mediaQuery.matches);
if (isDark) root.classList.add('dark');
else root.classList.remove('dark');
};
applyTheme();
localStorage.setItem('eduvane-theme', theme);
const listener = () => {
if (theme === 'system') applyTheme();
};
mediaQuery.addEventListener('change', listener);
return () => mediaQuery.removeEventListener('change', listener);
}, [theme]);
const fetchUserData = async (userId: string) => {
setLoading(true);
try {
const [subs, sets, prof] = await Promise.all([
SupabaseService.submissions.list(userId),
SupabaseService.practice.list(userId),
SupabaseService.profile.get(userId)
]);
setSubmissions(subs);
setPracticeSets(sets);
setProfile(prof || { id: userId, email: 'user@eduvane.ai', xp_total: 0 });
} catch (e) {
console.error("Error fetching user data:", e);
} finally {
setLoading(false);
}
};
useEffect(() => {
const init = async () => {
// Handle Demo Login event
const onDemoLogin = () => {
const demoUser = JSON.parse(localStorage.getItem('eduvane_demo_session') || '{}');
if (demoUser.id) {
SupabaseService.profile.upsert({
id: demoUser.id,
email: demoUser.email || 'demo@eduvane.ai',
xp_total: 120,
first_name: demoUser.given_name,
last_name: demoUser.family_name,
google_user_id: demoUser.google_user_id
});
setSession({ user: demoUser });
setIsGuest(false);
fetchUserData(demoUser.id);
}
};
window.addEventListener('eduvane_demo_login', onDemoLogin);
// Check existing sessions (Supabase or Demo)
const demoSession = localStorage.getItem('eduvane_demo_session');
if (SupabaseService.isConfigured()) {
const { data: { session: currentSession } } = await supabase!.auth.getSession();
if (currentSession) {
await SupabaseService.profile.syncFromAuthUser(currentSession.user);
setSession(currentSession);
await fetchUserData(currentSession.user.id);
} else if (demoSession) {
onDemoLogin();
} else {
setLoading(false);
}
const { data: { subscription } } = supabase!.auth.onAuthStateChange(async (_event, newSession) => {
if (newSession) {
await SupabaseService.profile.syncFromAuthUser(newSession.user);
setSession(newSession);
await fetchUserData(newSession.user.id);
setIsGuest(false);
} else if (!localStorage.getItem('eduvane_demo_session')) {
setSession(null);
setProfile(null);
setSubmissions([]);
setPracticeSets([]);
setLoading(false);
}
});
return () => {
subscription.unsubscribe();
window.removeEventListener('eduvane_demo_login', onDemoLogin);
};
} else {
if (demoSession) {
onDemoLogin();
} else {
setLoading(false);
}
return () => window.removeEventListener('eduvane_demo_login', onDemoLogin);
}
};
init();
}, []);
const handleGuestStart = () => {
setIsGuest(true);
};
const handleSignUpClick = () => {
setAuthMode('signup');
setShowAuth(true);
};
const handleSignInClick = () => {
setAuthMode('signin');
setShowAuth(true);
};
const handleSignOut = async () => {
setLoading(true);
try {
await SupabaseService.auth.signOut();
setIsGuest(false);
setSession(null);
setProfile(null);
setShowAuth(false);
setSubmissions([]);
setPracticeSets([]);
setGuestSubmissions([]);
setGuestPracticeSets([]);
} catch (e) {
console.error("Sign out error:", e);
} finally {
setLoading(false);
}
};
const currentUserId = session?.user?.id;
const saveSubmission = async (sub: Submission) => {
if (session && currentUserId) {
await SupabaseService.submissions.save(currentUserId, sub);
setSubmissions((current) => [sub, ...current]);
} else {
setGuestSubmissions((current) => [sub, ...current]);
}
};
const savePracticeSet = async (set: PracticeSet) => {
if (session && currentUserId) {
await SupabaseService.practice.save(currentUserId, set);
setPracticeSets((current) => [set, ...current]);
} else {
setGuestPracticeSets((current) => [set, ...current]);
}
};
const toggleTheme = () => {
if (theme === 'light') setTheme('dark');
else if (theme === 'dark') setTheme('system');
else setTheme('light');
};
const ThemeIcon = () => {
const Icon = (theme === 'light' ? Sun : theme === 'dark' ? Moon : Monitor) as React.ComponentType<{ size?: number }>;
return <Icon size={18} />;
};
if (!session && !isGuest) {
if (showAuth) {
return (
<AuthScreen
initialMode={authMode}
onBack={() => setShowAuth(false)}
/>
);
}
return (
<LandingPage
onSignUp={handleSignUpClick}
onSignIn={handleSignInClick}
onGuest={handleGuestStart}
/>
);
}
if (loading) return (
<div className="min-h-screen flex flex-col items-center justify-center bg-[#F7F9FC] dark:bg-slate-950 p-6 text-center">
<Loader2 className="animate-spin text-[#1FA2A6] mb-4" size={32} />
<p className="text-sm font-semibold text-slate-500 dark:text-slate-400">Reviewing your profile...</p>
</div>
);
const activeSubmissions = session ? submissions : guestSubmissions;
const activePracticeSets = session ? practiceSets : guestPracticeSets;
return (
<div className="h-[100dvh] flex flex-col overflow-hidden bg-[#F7F9FC] dark:bg-slate-950 transition-colors duration-200">
{isGuest && (
<div className="shrink-0 bg-[#1E3A5F] dark:bg-slate-900 text-white/80 px-4 py-2 text-[11px] font-medium flex items-center justify-center gap-2 z-[60]">
<Info size={14} className="text-[#1FA2A6]" />
Guest Mode: Your activity is temporary and will be cleared on refresh.
</div>
)}
<header className="bg-white dark:bg-slate-900 border-b border-slate-200 dark:border-slate-800 p-4 md:px-8 sticky top-0 z-50 h-16 flex items-center transition-colors">
<div className="max-w-6xl mx-auto w-full flex justify-between items-center">
<div className="flex items-center gap-2">
<VaneIcon color="#1FA2A6" size={24} />
<h1 className="text-lg font-bold tracking-tight text-[#1E3A5F] dark:text-slate-100">Eduvane</h1>
</div>
<div className="flex items-center gap-2">
<button
onClick={toggleTheme}
className="p-2 text-slate-400 hover:text-[#1FA2A6] dark:hover:text-slate-100 transition-colors"
title={`Switch Theme`}
>
<ThemeIcon />
</button>
<button onClick={handleSignOut} className="text-slate-400 hover:text-red-500 transition-colors">
<LogOut size={18} />
</button>
</div>
</div>
</header>
<main className="flex-1 overflow-y-auto overflow-x-hidden overscroll-y-contain max-w-6xl mx-auto w-full py-4 px-4 md:py-6">
<Dashboard
userId={currentUserId || 'GUEST'}
profile={profile}
submissions={activeSubmissions}
practiceSets={activePracticeSets}
onSaveSubmission={saveSubmission}
onSavePracticeSet={savePracticeSet}
/>
</main>
</div>
);
};
export default App;