forked from ibttf/interview-coder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
639 lines (583 loc) · 20.5 KB
/
App.tsx
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import { supabase } from "./lib/supabase"
import SubscribedApp from "./_pages/SubscribedApp"
import SubscribePage from "./_pages/SubscribePage"
import { UpdateNotification } from "./components/UpdateNotification"
import {
QueryClient,
QueryClientProvider,
useQueryClient
} from "@tanstack/react-query"
import { useEffect, useState, useCallback } from "react"
import { User } from "@supabase/supabase-js"
import {
Toast,
ToastDescription,
ToastProvider,
ToastTitle,
ToastViewport
} from "./components/ui/toast"
import { ToastContext } from "./contexts/toast"
// Create a React Query client
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 0,
gcTime: Infinity,
retry: 1,
refetchOnWindowFocus: false
},
mutations: {
retry: 1
}
}
})
// Root component that provides the QueryClient
function App() {
const [toastState, setToastState] = useState({
open: false,
title: "",
description: "",
variant: "neutral" as const
})
const [credits, setCredits] = useState<number>(0)
const [currentLanguage, setCurrentLanguage] = useState<string>("python")
const [isInitialized, setIsInitialized] = useState(false)
// Helper function to safely update credits
const updateCredits = useCallback((newCredits: number) => {
setCredits(newCredits)
window.__CREDITS__ = newCredits
}, [])
// Helper function to safely update language
const updateLanguage = useCallback((newLanguage: string) => {
setCurrentLanguage(newLanguage)
window.__LANGUAGE__ = newLanguage
}, [])
// Helper function to mark initialization complete
const markInitialized = useCallback(() => {
setIsInitialized(true)
window.__IS_INITIALIZED__ = true
}, [])
// Show toast method
const showToast = useCallback(
(
title: string,
description: string,
variant: "neutral" | "success" | "error"
) => {
setToastState({
open: true,
title,
description,
variant
})
},
[]
)
// Listen for PKCE code callback
useEffect(() => {
if (!import.meta.env.DEV) {
const handleAuthCallbackPKCE = async (data: { code: string }) => {
console.log("Production IPC: received code:", data)
try {
const { code } = data || {}
if (!code) {
console.error("No code in callback data")
return
}
const { error } = await supabase.auth.exchangeCodeForSession(code)
if (error) {
console.error("Error exchanging code for session:", error)
} else {
console.log("Production PKCE: Session exchanged successfully")
}
} catch (err) {
console.error("Production PKCE: Error in auth callback:", err)
}
}
console.log("PROD: Setting up PKCE-based IPC listener")
window.electron?.ipcRenderer?.on("auth-callback", handleAuthCallbackPKCE)
return () => {
window.electron?.ipcRenderer?.removeListener(
"auth-callback",
handleAuthCallbackPKCE
)
}
}
}, [])
// Handle credits initialization and updates
useEffect(() => {
const initializeAndSubscribe = async () => {
const {
data: { user }
} = await supabase.auth.getUser()
if (!user) {
updateCredits(1)
updateLanguage("python")
markInitialized()
return
}
// Initial fetch
const { data: subscription } = await supabase
.from("subscriptions")
.select("credits, preferred_language")
.eq("user_id", user.id)
.single()
// Set defaults if no subscription
updateCredits(subscription?.credits ?? 1)
updateLanguage(subscription?.preferred_language ?? "python")
markInitialized()
// Subscribe to changes
const channel = supabase
.channel("credits")
.on(
"postgres_changes",
{
event: "UPDATE",
schema: "public",
table: "subscriptions",
filter: `user_id=eq.${user.id}`
},
(payload) => {
const newCredits = payload.new.credits
updateCredits(newCredits)
}
)
.subscribe()
// Listen for solution success to decrement credits
const unsubscribeSolutionSuccess = window.electronAPI.onSolutionSuccess(
async () => {
// Wait for initialization before proceeding
if (!isInitialized) {
console.warn("Attempted to decrement credits before initialization")
return
}
// Get current credits before updating
const { data: currentSubscription } = await supabase
.from("subscriptions")
.select("credits")
.eq("user_id", user.id)
.single()
if (!currentSubscription) {
console.error(
"No subscription found when trying to decrement credits"
)
return
}
const { data: updatedSubscription, error } = await supabase
.from("subscriptions")
.update({ credits: currentSubscription.credits - 1 })
.eq("user_id", user.id)
.select("credits")
.single()
if (error) {
console.error("Error updating credits:", error)
return
}
updateCredits(updatedSubscription.credits)
}
)
// Cleanup function
return () => {
channel.unsubscribe()
unsubscribeSolutionSuccess()
// Reset initialization state on cleanup
window.__IS_INITIALIZED__ = false
setIsInitialized(false)
}
}
initializeAndSubscribe()
}, [updateCredits, updateLanguage, markInitialized, showToast, isInitialized])
return (
<QueryClientProvider client={queryClient}>
<ToastProvider>
<ToastContext.Provider value={{ showToast }}>
<AppContent isInitialized={isInitialized} />
<UpdateNotification />
<Toast
open={toastState.open}
onOpenChange={(open) =>
setToastState((prev) => ({ ...prev, open }))
}
variant={toastState.variant}
duration={1500}
>
<ToastTitle>{toastState.title}</ToastTitle>
<ToastDescription>{toastState.description}</ToastDescription>
</Toast>
<ToastViewport />
</ToastContext.Provider>
</ToastProvider>
</QueryClientProvider>
)
}
function AuthForm() {
const [isLoading, setIsLoading] = useState(false)
const [email, setEmail] = useState("")
const [password, setPassword] = useState("")
const [error, setError] = useState("")
const [shake, setShake] = useState(false)
const [passwordError, setPasswordError] = useState("")
const [isSignUp, setIsSignUp] = useState(false)
const validatePassword = (value: string) => {
if (isSignUp && value.length < 6) {
setPasswordError("Password must be at least 6 characters")
return false
}
setPasswordError("")
return true
}
const handlePasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value
setPassword(value)
if (value && isSignUp) validatePassword(value)
else setPasswordError("")
}
async function handleEmailAuth(e: React.FormEvent) {
e.preventDefault()
if (isSignUp && !validatePassword(password)) {
setShake(true)
setTimeout(() => setShake(false), 500)
return
}
setIsLoading(true)
setError("")
try {
if (isSignUp) {
const { data: signUpData, error: signUpError } =
await supabase.auth.signUp({
email,
password,
options: {
emailRedirectTo: `${window.location.origin}/auth/callback`
}
})
if (signUpError) throw signUpError
if (signUpData?.session) {
await supabase.auth.setSession({
access_token: signUpData.session.access_token,
refresh_token: signUpData.session.refresh_token
})
return
}
// If no session (email confirmation required), show message and switch to sign in
setError("Please check your email to confirm your account")
setTimeout(() => {
setIsSignUp(false)
}, 2000)
} else {
const { data, error } = await supabase.auth.signInWithPassword({
email,
password
})
if (error) {
if (error.message.includes("Invalid login credentials")) {
setError("Invalid email or password")
} else if (error.message.includes("Email not confirmed")) {
setError("Please verify your email address")
} else {
setError(error.message)
}
setShake(true)
setTimeout(() => setShake(false), 500)
return
}
if (data?.session) {
await supabase.auth.setSession({
access_token: data.session.access_token,
refresh_token: data.session.refresh_token
})
}
}
} catch (error) {
console.error(`Error ${isSignUp ? "signing up" : "signing in"}:`, error)
setError("Something went wrong, try again later")
setShake(true)
setTimeout(() => setShake(false), 500)
} finally {
setIsLoading(false)
}
}
async function handleGoogleAuth() {
setIsLoading(true)
setError("")
console.log("isdev", import.meta.env.DEV)
try {
const { error } = await supabase.auth.signInWithOAuth({
provider: "google",
options: {
redirectTo: import.meta.env.DEV
? "http://localhost:54321/callback"
: "interview-coder://callback",
skipBrowserRedirect: false
}
})
if (error) throw error
} catch (error) {
console.error(`Error with Google auth:`, error)
setError("Something went wrong with Google authentication")
setShake(true)
setTimeout(() => setShake(false), 500)
setIsLoading(false)
}
}
const toggleMode = () => {
setIsSignUp(!isSignUp)
setError("")
setPasswordError("")
setEmail("")
setPassword("")
}
return (
<div className="min-h-screen bg-black">
<div className="flex flex-col items-center justify-center min-h-screen px-4">
<div className="w-full max-w-md space-y-8 p-4 sm:p-8">
<div className="flex flex-col items-center justify-center space-y-6">
<h2 className="text-2xl font-semibold text-white">
{isSignUp ? "Create your account" : "Log in to Interview Coder"}
</h2>
<div className="w-full max-w-sm space-y-4">
<button
onClick={handleGoogleAuth}
disabled={isLoading}
className="w-full flex items-center justify-center gap-2 px-4 py-3 bg-[#1A1A1A] hover:bg-[#242424] text-white rounded-2xl border border-white/10 transition-colors disabled:opacity-50 disabled:cursor-not-allowed text-sm"
>
<svg className="h-5 w-5" viewBox="0 0 24 24">
<path
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
fill="#4285F4"
/>
<path
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
fill="#34A853"
/>
<path
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
fill="#FBBC05"
/>
<path
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
fill="#EA4335"
/>
</svg>
Google
</button>
<div className="relative">
<div className="absolute inset-0 flex items-center">
<div className="w-full border-t border-white/10"></div>
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-black px-2 text-[#989898]">
Or continue with email
</span>
</div>
</div>
<form onSubmit={handleEmailAuth} className="space-y-3">
<div className="space-y-1">
<input
type="email"
placeholder="Email address"
value={email}
onChange={(e) => setEmail(e.target.value)}
className={`w-full px-4 py-3 text-white rounded-2xl border focus:outline-none text-sm font-medium placeholder:text-[#989898] placeholder:font-medium transition-colors frosted-glass ${
error
? "border-red-500 focus:border-red-500"
: "border-white/10 focus:border-white/20"
} ${shake ? "shake" : ""}`}
required
/>
{error && (
<p className="text-sm text-red-500 px-1">{error}</p>
)}
</div>
<div className="space-y-1">
<input
type="password"
placeholder="Password"
value={password}
onChange={handlePasswordChange}
className={`w-full px-4 py-3 text-white rounded-2xl border focus:outline-none text-sm font-medium placeholder:text-[#989898] placeholder:font-medium transition-colors frosted-glass ${
passwordError
? "border-red-500 focus:border-red-500"
: "border-white/10 focus:border-white/20"
} ${shake ? "shake" : ""}`}
required
/>
{passwordError && (
<p className="text-sm text-red-500 px-1">{passwordError}</p>
)}
</div>
<button
type="submit"
disabled={isLoading || !email || !password || !!passwordError}
className="relative w-full px-4 py-3 rounded-2xl transition-all disabled:opacity-50 disabled:cursor-not-allowed text-sm font-medium auth-button"
>
{isLoading
? isSignUp
? "Creating account..."
: "Signing in..."
: isSignUp
? "Create account"
: "Sign in"}
</button>
</form>
<button
onClick={toggleMode}
className="block w-full border border-white/10 rounded-2xl p-4 hover:bg-[#1A1A1A] transition-colors group"
>
<p className="text-center text-sm text-[#989898]">
{isSignUp
? "Already have an account? Sign in →"
: "Don't have an account? Sign up →"}
</p>
</button>
</div>
</div>
</div>
</div>
</div>
)
}
// Main App component that handles conditional rendering based on auth and subscription state
function AppContent({ isInitialized }: { isInitialized: boolean }) {
const [user, setUser] = useState<User | null>(null)
const [loading, setLoading] = useState(true)
const [subscriptionLoading, setSubscriptionLoading] = useState(false)
const [isSubscribed, setIsSubscribed] = useState(false)
const [credits, setCredits] = useState<number | undefined>(undefined)
const [currentLanguage, setCurrentLanguage] = useState<string>("python")
const queryClient = useQueryClient()
// Check auth state on mount
useEffect(() => {
const {
data: { subscription }
} = supabase.auth.onAuthStateChange((event, session) => {
setUser(session?.user ?? null)
setLoading(false)
})
return () => subscription.unsubscribe()
}, [])
// Check subscription and credits status whenever user changes
useEffect(() => {
const checkSubscriptionAndCredits = async () => {
if (!user?.id) {
setIsSubscribed(false)
setCredits(0)
setCurrentLanguage("python")
return
}
setSubscriptionLoading(true)
try {
const { data: subscription } = await supabase
.from("subscriptions")
.select("*, credits, preferred_language")
.eq("user_id", user.id)
.maybeSingle()
setIsSubscribed(!!subscription)
setCredits(subscription?.credits ?? 0)
if (subscription?.preferred_language) {
setCurrentLanguage(subscription.preferred_language)
window.__LANGUAGE__ = subscription.preferred_language
}
} finally {
setSubscriptionLoading(false)
}
}
checkSubscriptionAndCredits()
// Set up real-time subscription for both subscription status and credits
const channel = supabase
.channel(`sub-and-credits-${user?.id}`)
.on(
"postgres_changes",
{
event: "*",
schema: "public",
table: "subscriptions",
filter: user?.id ? `user_id=eq.${user.id}` : undefined
},
async (payload) => {
console.log("Subscription/credits event received:", {
eventType: payload.eventType,
old: payload.old,
new: payload.new
})
// For any subscription event, check current subscription status
if (
payload.eventType === "DELETE" ||
payload.eventType === "UPDATE"
) {
console.log("Checking current subscription and credits status...")
const { data: subscription } = await supabase
.from("subscriptions")
.select("*, credits, preferred_language")
.eq("user_id", user?.id)
.maybeSingle()
console.log("Current subscription check result:", subscription)
setIsSubscribed(!!subscription)
setCredits(subscription?.credits ?? 0)
if (subscription?.preferred_language) {
setCurrentLanguage(subscription.preferred_language)
window.__LANGUAGE__ = subscription.preferred_language
}
await queryClient.invalidateQueries({ queryKey: ["user"] })
}
// Handle INSERT events
if (
payload.eventType === "INSERT" &&
payload.new?.user_id === user?.id
) {
console.log("New subscription detected")
setIsSubscribed(true)
setCredits(payload.new.credits ?? 0)
if (payload.new.preferred_language) {
setCurrentLanguage(payload.new.preferred_language)
window.__LANGUAGE__ = payload.new.preferred_language
}
await queryClient.invalidateQueries({ queryKey: ["user"] })
}
}
)
.subscribe()
return () => {
channel.unsubscribe()
}
}, [user?.id, queryClient])
// Show loading state while checking auth, subscription, initialization, or credits
if (
loading ||
(user && (subscriptionLoading || !isInitialized || credits === undefined))
) {
return (
<div className="min-h-screen bg-black flex items-center justify-center">
<div className="flex flex-col items-center gap-3">
<div className="w-6 h-6 border-2 border-white/20 border-t-white/80 rounded-full animate-spin"></div>
<p className="text-white/60 text-sm">
{loading
? "Loading..."
: !isInitialized
? "Initializing...If you see this screen for more than 10 seconds, please quit and restart the app."
: credits === undefined
? "Loading credits..."
: "Checking subscription..."}
</p>
</div>
</div>
)
}
// If not logged in, show auth form
if (!user) {
return <AuthForm />
}
// If logged in but not subscribed, show subscribe page
if (!isSubscribed) {
return <SubscribePage user={user} />
}
// If logged in and subscribed with credits loaded, show the app
return (
<SubscribedApp
credits={credits!}
currentLanguage={currentLanguage}
setLanguage={setCurrentLanguage}
/>
)
}
export default App