-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
903 lines (784 loc) · 31 KB
/
index.html
File metadata and controls
903 lines (784 loc) · 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
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ask Babushka - Wisdom-Guided Relationship Advice</title>
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<!-- FIXED: Use the correct Clerk script URL -->
<!-- Clerk authentication script -->
<script
async
src="https://clerk.askbabushka.ai/npm/@clerk/clerk-js@5/dist/clerk.browser.js"
data-clerk-publishable-key="pk_live_Y2xlcmsuYXNrYmFidXNoa2EuYWkk"
data-clerk-frontend-api="https://accounts.askbabushka.ai">
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Georgia', serif;
background: linear-gradient(135deg, #03ABAF 0%, #05776F 100%);
min-height: 100vh;
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.auth-container {
display: none;
text-align: center;
background: #F7F6F9;
border-radius: 20px;
padding: 40px;
box-shadow: 0 10px 30px rgba(5, 119, 111, 0.2);
margin-bottom: 30px;
border: 2px solid #F3B807;
}
.auth-container.show {
display: block;
}
.main-app {
display: none;
}
.main-app.show {
display: block;
}
.user-info {
background: #F7F6F9;
border-radius: 15px;
padding: 15px 20px;
margin-bottom: 20px;
display: none;
justify-content: space-between;
align-items: center;
box-shadow: 0 5px 15px rgba(5, 119, 111, 0.15);
border: 1px solid #BD9662;
}
.user-info.show {
display: flex;
}
.user-details {
display: flex;
align-items: center;
gap: 10px;
}
.user-avatar {
width: 40px;
height: 40px;
border-radius: 50%;
border: 2px solid #F3B807;
}
.user-name {
color: #05776F;
font-weight: bold;
}
.sign-out-btn {
background: linear-gradient(135deg, #B93C20 0%, #05776F 100%);
color: #F7F6F9;
border: none;
padding: 8px 15px;
border-radius: 15px;
cursor: pointer;
font-size: 0.9rem;
transition: all 0.3s ease;
}
.sign-out-btn:hover {
transform: translateY(-1px);
box-shadow: 0 3px 10px rgba(185, 60, 32, 0.4);
}
.header {
text-align: center;
margin-bottom: 30px;
background: #F7F6F9;
border-radius: 20px;
padding: 30px;
box-shadow: 0 10px 30px rgba(5, 119, 111, 0.15);
border: 2px solid #F3B807;
}
.babushka-image {
width: 120px;
height: 120px;
margin: 0 auto 20px auto;
display: block;
border-radius: 50%;
border: 3px solid #F3B807;
box-shadow: 0 5px 20px rgba(5, 119, 111, 0.3);
object-fit: cover;
}
h1 {
font-size: 2.5rem;
color: #05776F;
margin-bottom: 10px;
font-weight: bold;
text-shadow: 0 1px 2px rgba(243, 184, 7, 0.3);
}
.subtitle {
font-size: 1.2rem;
color: #03ABAF;
font-style: italic;
}
.chat-container {
background: #F7F6F9;
border-radius: 20px;
padding: 0;
box-shadow: 0 10px 30px rgba(5, 119, 111, 0.15);
overflow: hidden;
margin-bottom: 20px;
border: 2px solid #BD9662;
}
.messages {
height: 400px;
overflow-y: auto;
padding: 20px;
background: linear-gradient(to bottom, #F7F6F9 0%, rgba(247, 246, 249, 0.95) 100%);
}
.message {
margin-bottom: 20px;
animation: fadeIn 0.5s ease-in;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.user-message {
text-align: right;
}
.user-message .message-bubble {
background: linear-gradient(135deg, #03ABAF 0%, #05776F 100%);
color: #F7F6F9;
border-radius: 20px 20px 5px 20px;
padding: 15px 20px;
display: inline-block;
max-width: 70%;
box-shadow: 0 5px 15px rgba(3, 171, 175, 0.3);
border: 1px solid #BD9662;
}
.bot-message .message-bubble {
background: #F7F6F9;
color: #05776F;
border-radius: 20px 20px 20px 5px;
padding: 15px 20px;
display: inline-block;
max-width: 80%;
box-shadow: 0 5px 15px rgba(5, 119, 111, 0.1);
border-left: 5px solid #F3B807;
border: 1px solid #BD9662;
}
.wisdom-tag {
background: linear-gradient(45deg, #F3B807, #BD9662);
color: #05776F;
padding: 5px 12px;
border-radius: 15px;
font-size: 0.8rem;
font-weight: bold;
margin-bottom: 10px;
display: inline-block;
text-shadow: 0 1px 1px rgba(247, 246, 249, 0.8);
}
.input-area {
padding: 20px;
background: #F7F6F9;
border-top: 2px solid #F3B807;
}
.input-container {
display: flex;
gap: 10px;
align-items: stretch;
}
#userInput {
flex: 1;
padding: 15px 20px;
border: 2px solid #BD9662;
border-radius: 25px;
font-size: 1rem;
font-family: inherit;
outline: none;
transition: all 0.3s ease;
background: #F7F6F9;
color: #05776F;
}
#userInput:focus {
border-color: #03ABAF;
box-shadow: 0 0 10px rgba(3, 171, 175, 0.2);
}
#userInput::placeholder {
color: #8bb3b1;
}
#sendButton {
background: linear-gradient(135deg, #03ABAF 0%, #05776F 100%);
color: #F7F6F9;
border: none;
border-radius: 25px;
padding: 15px 25px;
font-size: 1rem;
cursor: pointer;
transition: all 0.3s ease;
font-weight: bold;
border: 1px solid #F3B807;
}
#sendButton:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(3, 171, 175, 0.4);
background: linear-gradient(135deg, #05776F 0%, #03ABAF 100%);
}
#sendButton:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.typing-indicator {
display: none;
padding: 15px 20px;
color: #05776F;
font-style: italic;
}
.typing-dots {
display: inline-block;
animation: typing 1.5s infinite;
}
@keyframes typing {
0%, 60%, 100% { opacity: 0; }
30% { opacity: 1; }
}
.examples {
background: #F7F6F9;
border-radius: 20px;
padding: 25px;
box-shadow: 0 10px 30px rgba(5, 119, 111, 0.15);
border: 2px solid #BD9662;
}
.examples h3 {
color: #05776F;
margin-bottom: 15px;
font-size: 1.3rem;
}
.example-buttons {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.example-button {
background: rgba(247, 246, 249, 0.9);
border: 2px solid #BD9662;
border-radius: 15px;
padding: 10px 15px;
cursor: pointer;
transition: all 0.3s ease;
font-size: 0.9rem;
color: #05776F;
}
.example-button:hover {
background: linear-gradient(135deg, #03ABAF 0%, #05776F 100%);
color: #F7F6F9;
border-color: #F3B807;
transform: translateY(-2px);
box-shadow: 0 3px 10px rgba(3, 171, 175, 0.3);
}
.error-message {
background: rgba(185, 60, 32, 0.1);
color: #B93C20;
padding: 15px;
border-radius: 10px;
margin: 10px 0;
border-left: 5px solid #B93C20;
}
.loading {
display: inline-block;
width: 20px;
height: 20px;
border: 2px solid #F7F6F9;
border-radius: 50%;
border-top-color: transparent;
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.auth-welcome {
font-size: 1.5rem;
color: #05776F;
margin-bottom: 20px;
}
.auth-description {
color: #03ABAF;
margin-bottom: 30px;
font-size: 1.1rem;
}
.sign-in-link {
background: rgba(247, 246, 249, 0.9);
border: 2px solid #F3B807;
color: #05776F;
padding: 8px 15px;
border-radius: 15px;
cursor: pointer;
font-size: 0.9rem;
transition: all 0.3s ease;
text-decoration: none;
display: inline-block;
}
.sign-in-link:hover {
background: linear-gradient(135deg, #03ABAF 0%, #05776F 100%);
color: #F7F6F9;
transform: translateY(-1px);
border-color: #BD9662;
}
/* Elegant decorative elements */
.header::before {
content: "✦";
display: block;
font-size: 1.5rem;
color: #F3B807;
margin-bottom: 10px;
opacity: 0.8;
}
.wisdom-tag::before {
content: "✧ ";
color: #05776F;
}
/* Subtle accent touches */
.chat-container::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
height: 3px;
background: linear-gradient(90deg, #F3B807, #BD9662, #F3B807);
border-radius: 20px 20px 0 0;
}
.examples::after {
content: "";
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 2px;
background: linear-gradient(90deg, transparent, #BD9662, transparent);
border-radius: 0 0 20px 20px;
}
.header, .chat-container, .examples {
position: relative;
}
@media (max-width: 600px) {
.container {
padding: 10px;
}
h1 {
font-size: 2rem;
}
.subtitle {
font-size: 1rem;
}
.user-message .message-bubble,
.bot-message .message-bubble {
max-width: 90%;
}
.example-buttons {
flex-direction: column;
}
.input-container {
flex-direction: column;
}
#sendButton {
border-radius: 15px;
}
.user-info {
flex-direction: column;
gap: 15px;
text-align: center;
}
}
</style>
</head>
<body>
<div class="container">
<!-- Auth Container (shown when not signed in) -->
<div class="auth-container" id="authContainer">
<img src="babushka.png" alt="Babushka" class="babushka-image">
<h1>Ask Babushka</h1>
<p class="auth-welcome">Welcome, dear child!</p>
<p class="auth-description">Sign in to get personalized wisdom and save your conversation history with Babushka</p>
<div id="clerk-signin"></div>
<p style="margin-top: 20px; color: #03ABAF;">
Or <a href="#" class="sign-in-link" onclick="showMainApp(); return false;">continue without signing in</a>
</p>
</div>
<!-- Main App (shown when signed in OR anonymous) -->
<div class="main-app" id="mainApp">
<!-- User Info Bar (only shown when authenticated) -->
<div class="user-info" id="userInfo">
<div class="user-details">
<img id="userAvatar" src="" alt="Profile" class="user-avatar">
<span id="userName" class="user-name"></span>
</div>
<button class="sign-out-btn" onclick="signOut()">
<i class="fas fa-sign-out-alt"></i> Sign Out
</button>
</div>
<div class="header">
<img src="babushka.png" alt="Babushka" class="babushka-image">
<h1>Ask Babushka</h1>
<p class="subtitle">Are you hungry? Want some pirozhkis? Tell me what troubles you? [BETA]</p>
<p id="authStatus" style="margin-top: 10px; color: #03ABAF; font-size: 0.9rem;">
<a href="#" class="sign-in-link" onclick="showAuthContainer(); return false;">Sign in for personalized experience</a>
</p>
</div>
<div class="chat-container">
<div class="messages" id="messages">
<div class="message bot-message">
<div class="message-bubble">
<div class="wisdom-tag">Ancient Wisdom</div>
Welcome, dear child! I am Babushka, your relationship advisor with the wisdom of many generations. Tell me about your relationship situation, and I will guide you with the practical wisdom that has helped families for centuries. What weighs on your heart today?
</div>
</div>
</div>
<div class="typing-indicator" id="typingIndicator">
<span class="wisdom-tag">Thinking</span>
Babushka is considering your situation<span class="typing-dots">...</span>
</div>
<div class="input-area">
<div class="input-container">
<input type="text" id="userInput" placeholder="Tell Babushka about your relationship situation..." maxlength="500">
<button id="sendButton">
<i class="fas fa-heart"></i> Send
</button>
</div>
</div>
</div>
<div class="examples">
<h3><i class="fas fa-lightbulb"></i> Need inspiration? Try asking about:</h3>
<div class="example-buttons">
<button class="example-button" onclick="fillInput('My partner and I keep fighting about money')">Money disagreements</button>
<button class="example-button" onclick="fillInput('I feel like my partner doesn\'t listen to me')">Communication issues</button>
<button class="example-button" onclick="fillInput('We\'re thinking about moving in together')">Taking next steps</button>
<button class="example-button" onclick="fillInput('My partner\'s family doesn\'t like me')">Family dynamics</button>
<button class="example-button" onclick="fillInput('We have different life goals')">Future planning</button>
<button class="example-button" onclick="fillInput('I\'m not sure if this relationship is right for me')">Relationship doubts</button>
</div>
</div>
</div>
</div>
<script>
// Configuration
const CLERK_PUBLISHABLE_KEY = 'pk_live_Y2xlcmsuYXNrYmFidXNoa2EuYWkk';
const API_BASE_URL = 'https://babushka-production.up.railway.app';
let clerk;
let currentUser = null;
let userToken = null;
// Initialize Clerk when the Clerk script is loaded
document.addEventListener('DOMContentLoaded', () => {
try {
// Wait for Clerk to be available
if (!window.ClerkJS) {
console.error('ClerkJS not available');
// Show main app with sign-in link
showMainApp();
return;
}
// Initialize Clerk
clerk = new ClerkJS();
// Set up event listeners
clerk.on('user:signed_in', (user) => {
console.log('User signed in:', user);
handleSignedIn(user);
});
clerk.on('user:signed_out', () => {
console.log('User signed out');
handleSignedOut();
});
// Check if user is already signed in
const user = clerk.user;
if (user) {
console.log('User already signed in:', user);
handleSignedIn(user);
} else {
console.log('No user signed in, showing main app with sign-in link');
showMainApp();
}
// Mount sign-in component
const signInDiv = document.getElementById('clerk-signin');
if (signInDiv) {
try {
clerk.mountSignIn(signInDiv, {
signInUrl: 'https://accounts.askbabushka.ai/sign-in',
signUpUrl: 'https://accounts.askbabushka.ai/sign-up',
afterSignInUrl: 'https://www.askbabushka.ai',
afterSignUpUrl: 'https://www.askbabushka.ai',
redirectUrl: 'https://www.askbabushka.ai'
});
console.log('Sign-in component mounted');
} catch (error) {
console.error('Error mounting sign-in component:', error);
// Add sign-in link to main app
const authStatus = document.getElementById('authStatus');
if (authStatus) {
authStatus.innerHTML = `
<p style="margin-top: 10px; color: #03ABAF; font-size: 0.9rem;">
<a href="#" class="sign-in-link" onclick="showAuthContainer(); return false;">
Sign in for personalized experience
</a>
</p>
`;
}
}
}
} catch (error) {
console.error('Error initializing Clerk:', error);
// Show main app with error message
showMainApp();
const authStatus = document.getElementById('authStatus');
if (authStatus) {
authStatus.innerHTML = `
<p style="margin-top: 10px; color: #03ABAF; font-size: 0.9rem;">
<a href="https://accounts.askbabushka.ai/sign-in" target="_blank">
Sign in for personalized experience
</a>
</p>
`;
}
}
});
// Fallback: Show main app after a short delay
setTimeout(() => {
if (!clerk) {
console.log('Clerk not initialized, showing main app');
showMainApp();
}
}, 2000);
// Initialize Clerk immediately if available
if (typeof window.ClerkJS !== 'undefined') {
console.log('ClerkJS is available, initializing...');
clerk = new ClerkJS();
// Mount sign-in component immediately
const signInDiv = document.getElementById('clerk-signin');
if (signInDiv) {
try {
clerk.mountSignIn(signInDiv, {
signInUrl: 'https://accounts.askbabushka.ai/sign-in',
signUpUrl: 'https://accounts.askbabushka.ai/sign-up',
afterSignInUrl: 'https://www.askbabushka.ai',
afterSignUpUrl: 'https://www.askbabushka.ai',
redirectUrl: 'https://www.askbabushka.ai'
});
console.log('Sign-in component mounted immediately');
} catch (error) {
console.error('Error mounting sign-in component immediately:', error);
}
}
}
// Handle signed in user with your custom JWT template
async function handleSignedIn(user) {
console.log('Handling signed in user:', user);
currentUser = user;
try {
// Use your custom JWT template "AskBabushkaAPI"
userToken = await user.getToken({ template: "AskBabushkaAPI" });
console.log('Got user token with AskBabushkaAPI template');
} catch (error) {
console.error('Error getting token with AskBabushkaAPI template:', error);
try {
// Fallback to default token
userToken = await user.getToken();
console.log('Got fallback token');
} catch (fallbackError) {
console.error('Error getting fallback token:', fallbackError);
userToken = null;
}
}
// Update user info display
document.getElementById('userAvatar').src = user.imageUrl || '/default-avatar.png';
document.getElementById('userName').textContent = user.firstName || user.emailAddresses[0]?.emailAddress || 'User';
// Show authenticated state
document.getElementById('userInfo').classList.add('show');
document.getElementById('authStatus').innerHTML = '<span style="color: #03ABAF;">✓ Signed in - conversations saved</span>';
showMainApp();
}
function handleSignedOut() {
console.log('Handling signed out');
currentUser = null;
userToken = null;
// Hide user info
document.getElementById('userInfo').classList.remove('show');
document.getElementById('authStatus').innerHTML = '<a href="#" class="sign-in-link" onclick="showAuthContainer(); return false;">Sign in for personalized experience</a>';
showAuthContainer();
}
function showAuthContainer() {
console.log('Showing auth container');
document.getElementById('authContainer').classList.add('show');
document.getElementById('mainApp').classList.remove('show');
// Mount sign-in component
if (clerk && document.getElementById('clerk-signin')) {
const signInDiv = document.getElementById('clerk-signin');
signInDiv.innerHTML = ''; // Clear any existing content
setTimeout(() => {
try {
clerk.mountSignIn(signInDiv, {
signUpUrl: 'https://accounts.askbabushka.ai/sign-up',
afterSignInUrl: window.location.origin,
redirectUrl: window.location.origin
});
console.log('Sign-in component mounted');
} catch (error) {
console.error('Error mounting sign-in:', error);
// Create a manual sign-in link as fallback
signInDiv.innerHTML = `
<div style="padding: 20px;">
<p style="color: #05776F; margin-bottom: 15px;">Please use the link below to sign in:</p>
<a href="https://accounts.askbabushka.ai/sign-in"
style="background: linear-gradient(135deg, #03ABAF 0%, #05776F 100%);
color: #F7F6F9; padding: 10px 20px; border-radius: 15px;
text-decoration: none; display: inline-block;">
Sign In with Clerk
</a>
</div>
`;
}
}, 100);
}
}
function showMainApp() {
console.log('Showing main app');
document.getElementById('authContainer').classList.remove('show');
document.getElementById('mainApp').classList.add('show');
}
async function signOut() {
console.log('Signing out...');
try {
if (clerk) {
await clerk.signOut();
console.log('Signed out successfully');
} else {
handleSignedOut();
}
} catch (error) {
console.error('Error signing out:', error);
handleSignedOut();
}
}
// Chat functionality
const messagesContainer = document.getElementById('messages');
const userInput = document.getElementById('userInput');
const sendButton = document.getElementById('sendButton');
const typingIndicator = document.getElementById('typingIndicator');
async function sendMessage() {
const message = userInput.value.trim();
if (!message) return;
addMessage(message, 'user');
userInput.value = '';
showTyping();
try {
const headers = {
'Content-Type': 'application/json',
};
// Add auth token if available
if (userToken) {
headers['Authorization'] = `Bearer ${userToken}`;
console.log('Sending authenticated request');
} else {
console.log('Sending anonymous request');
}
const response = await fetch(`${API_BASE_URL}/advice`, {
method: 'POST',
headers: headers,
body: JSON.stringify({
situation: message,
user_id: currentUser?.id || null
}),
});
if (!response.ok) {
throw new Error(`Server error: ${response.status}`);
}
const data = await response.json();
hideTyping();
addMessage(data.advice, 'bot');
} catch (error) {
console.error('Error:', error);
hideTyping();
addMessage('Oh dear, Babushka is having trouble right now. Please try again in a moment. Sometimes wisdom needs a little time to flow properly!', 'bot', true);
}
}
function addMessage(text, sender, isError = false) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}-message`;
const bubbleDiv = document.createElement('div');
bubbleDiv.className = 'message-bubble';
if (sender === 'bot' && !isError) {
const wisdomTag = document.createElement('div');
wisdomTag.className = 'wisdom-tag';
wisdomTag.innerHTML = 'Babushka\'s Wisdom';
bubbleDiv.appendChild(wisdomTag);
}
if (isError) {
bubbleDiv.className += ' error-message';
}
const textNode = document.createTextNode(text);
bubbleDiv.appendChild(textNode);
messageDiv.appendChild(bubbleDiv);
messagesContainer.appendChild(messageDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
function showTyping() {
typingIndicator.style.display = 'block';
sendButton.disabled = true;
sendButton.innerHTML = '<div class="loading"></div>';
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
function hideTyping() {
typingIndicator.style.display = 'none';
sendButton.disabled = false;
sendButton.innerHTML = '<i class="fas fa-heart"></i> Send';
}
function fillInput(text) {
userInput.value = text;
userInput.focus();
}
// Event listeners
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter' && !sendButton.disabled) {
sendMessage();
}
});
// Initialize when page loads
// Only one initialization attempt
document.addEventListener('DOMContentLoaded', () => {
try {
// Wait for Clerk to be available
if (!window.ClerkJS) {
console.error('ClerkJS not available');
return;
}
// Initialize Clerk
clerk = new ClerkJS();
// Set up event listeners
clerk.on('user:signed_in', (user) => {
console.log('User signed in:', user);
handleSignedIn(user);
});
clerk.on('user:signed_out', () => {
console.log('User signed out');
handleSignedOut();
});
// Check if user is already signed in
const user = clerk.user;
if (user) {
console.log('User already signed in:', user);
handleSignedIn(user);
} else {
console.log('No user signed in, showing auth');
showAuthContainer();
}
} catch (error) {
console.error('Error initializing Clerk:', error);
}
});
</script>
</body>
</html>