-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
1292 lines (1146 loc) · 43.5 KB
/
Copy pathindex.html
File metadata and controls
1292 lines (1146 loc) · 43.5 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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NexusBridge</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<style>
/* ── Reset & Variables ─────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg: #0f1117;
--surface: #1a1d27;
--surface2: #222639;
--border: #2e3148;
--accent: #6c63ff;
--accent2: #a78bfa;
--green: #22c55e;
--red: #ef4444;
--text: #e2e8f0;
--text-muted: #94a3b8;
--bubble-out: #6c63ff;
--bubble-in: #2e3148;
--radius: 12px;
--sidebar-w: 340px;
}
html, body {
height: 100%;
background: var(--bg);
color: var(--text);
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
font-size: 14px;
overflow: hidden;
}
/* ── App Shell ─────────────────────────────────────────── */
#app { display: flex; flex-direction: column; height: 100vh; }
/* ── Header ────────────────────────────────────────────── */
#header {
display: flex;
align-items: center;
gap: 10px;
padding: 14px 20px;
background: var(--surface);
border-bottom: 1px solid var(--border);
flex-shrink: 0;
z-index: 10;
}
#header svg { flex-shrink: 0; }
#header h1 { font-size: 20px; font-weight: 700; letter-spacing: -0.3px; }
#header h1 span { color: var(--accent2); }
#conn-status {
margin-left: auto;
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
color: var(--text-muted);
}
#status-dot {
width: 9px; height: 9px;
border-radius: 50%;
background: var(--red);
transition: background 0.3s;
flex-shrink: 0;
}
#status-dot.connected { background: var(--green); }
/* ── Pairing Screen ────────────────────────────────────── */
#pairing-screen {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
gap: 32px;
padding: 24px;
overflow-y: auto;
}
#pairing-card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 20px;
padding: 40px 48px;
display: flex;
flex-direction: column;
align-items: center;
gap: 24px;
max-width: 440px;
width: 100%;
}
#pairing-card h2 {
font-size: 22px;
font-weight: 700;
color: var(--text);
}
#pairing-card p {
color: var(--text-muted);
text-align: center;
line-height: 1.6;
}
#qr-container {
background: #fff;
border-radius: 12px;
padding: 16px;
display: flex;
align-items: center;
justify-content: center;
min-width: 180px;
min-height: 180px;
position: relative;
}
#qr-spinner {
position: absolute;
width: 40px; height: 40px;
border: 3px solid var(--border);
border-top-color: var(--accent);
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#pin-display {
font-size: 42px;
font-weight: 800;
letter-spacing: 10px;
color: var(--accent2);
font-variant-numeric: tabular-nums;
}
#pairing-status {
display: flex;
align-items: center;
gap: 10px;
color: var(--text-muted);
font-size: 13px;
}
#pairing-spinner {
width: 16px; height: 16px;
border: 2px solid var(--border);
border-top-color: var(--accent);
border-radius: 50%;
animation: spin 0.8s linear infinite;
flex-shrink: 0;
}
.btn {
padding: 10px 24px;
border-radius: 8px;
border: none;
cursor: pointer;
font-size: 14px;
font-weight: 600;
transition: opacity 0.15s, transform 0.1s;
}
.btn:hover { opacity: 0.85; }
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-ghost {
background: transparent;
border: 1px solid var(--border);
color: var(--text-muted);
}
.btn-ghost:hover { color: var(--text); border-color: var(--text-muted); }
.btn-danger {
background: transparent;
border: 1px solid var(--red);
color: var(--red);
}
.btn-danger:hover { background: #ef444422; }
/* ── Main App ──────────────────────────────────────────── */
#main-screen {
flex: 1;
display: flex;
overflow: hidden;
}
/* ── Reconnect Banner ──────────────────────────────────── */
#reconnect-banner {
display: none;
background: #7c3aed22;
border-bottom: 1px solid #7c3aed55;
padding: 10px 20px;
text-align: center;
color: var(--accent2);
font-size: 13px;
flex-shrink: 0;
}
#reconnect-banner.visible { display: block; }
/* ── Sidebar ───────────────────────────────────────────── */
#sidebar {
width: var(--sidebar-w);
flex-shrink: 0;
display: flex;
flex-direction: column;
border-right: 1px solid var(--border);
background: var(--surface);
overflow: hidden;
}
#sidebar-header {
padding: 16px 16px 10px;
display: flex;
flex-direction: column;
gap: 10px;
border-bottom: 1px solid var(--border);
flex-shrink: 0;
}
#sidebar-header h3 { font-size: 16px; font-weight: 700; }
#search-input {
background: var(--surface2);
border: 1px solid var(--border);
border-radius: 8px;
padding: 8px 12px;
color: var(--text);
font-size: 14px;
outline: none;
width: 100%;
transition: border-color 0.2s;
}
#search-input:focus { border-color: var(--accent); }
#search-input::placeholder { color: var(--text-muted); }
#conversations-list {
flex: 1;
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: var(--border) transparent;
}
#conversations-list::-webkit-scrollbar { width: 5px; }
#conversations-list::-webkit-scrollbar-track { background: transparent; }
#conversations-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.conv-item {
display: flex;
align-items: center;
gap: 12px;
padding: 12px 16px;
cursor: pointer;
border-bottom: 1px solid var(--border);
transition: background 0.15s;
}
.conv-item:hover { background: var(--surface2); }
.conv-item.active { background: var(--surface2); border-left: 3px solid var(--accent); }
.conv-avatar {
width: 44px; height: 44px;
border-radius: 50%;
background: var(--accent);
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
font-size: 16px;
color: #fff;
}
.conv-info { flex: 1; min-width: 0; }
.conv-name {
font-weight: 600;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.conv-preview {
color: var(--text-muted);
font-size: 12px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin-top: 2px;
}
.conv-meta {
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 4px;
flex-shrink: 0;
}
.conv-time { font-size: 11px; color: var(--text-muted); }
.unread-badge {
background: var(--accent);
color: #fff;
border-radius: 10px;
padding: 1px 7px;
font-size: 11px;
font-weight: 700;
display: none;
}
.unread-badge.visible { display: block; }
#sidebar-empty {
padding: 40px 20px;
text-align: center;
color: var(--text-muted);
}
#sidebar-loading {
padding: 24px;
text-align: center;
color: var(--text-muted);
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
}
/* ── Thread View ───────────────────────────────────────── */
#thread-panel {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
background: var(--bg);
}
#thread-header {
display: flex;
align-items: center;
gap: 12px;
padding: 14px 20px;
background: var(--surface);
border-bottom: 1px solid var(--border);
flex-shrink: 0;
}
#thread-header h2 { font-size: 16px; font-weight: 700; }
#thread-phone { font-size: 13px; color: var(--text-muted); margin-top: 1px; }
#btn-back {
display: none;
background: none;
border: none;
color: var(--text-muted);
cursor: pointer;
padding: 4px;
margin-right: 4px;
}
#btn-back:hover { color: var(--text); }
#messages-area {
flex: 1;
overflow-y: auto;
padding: 20px 16px;
display: flex;
flex-direction: column;
gap: 6px;
scrollbar-width: thin;
scrollbar-color: var(--border) transparent;
}
#messages-area::-webkit-scrollbar { width: 5px; }
#messages-area::-webkit-scrollbar-track { background: transparent; }
#messages-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
#thread-placeholder {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
gap: 16px;
color: var(--text-muted);
padding: 40px;
text-align: center;
}
#thread-placeholder svg { opacity: 0.3; }
.msg-bubble {
max-width: 72%;
padding: 10px 14px;
border-radius: 16px;
line-height: 1.5;
word-break: break-word;
position: relative;
}
.msg-outgoing {
background: var(--bubble-out);
color: #fff;
align-self: flex-end;
border-bottom-right-radius: 4px;
}
.msg-outgoing.pending { opacity: 0.55; }
.msg-outgoing.sent-fail { background: #c0392b; }
.msg-outgoing.sent-ok::after {
content: " ✓";
font-size: 11px;
opacity: 0.8;
}
.msg-incoming {
background: var(--bubble-in);
color: var(--text);
align-self: flex-start;
border-bottom-left-radius: 4px;
}
.msg-time {
display: block;
font-size: 10px;
margin-top: 4px;
opacity: 0.65;
text-align: right;
}
.msg-date-divider {
align-self: center;
font-size: 11px;
color: var(--text-muted);
background: var(--surface2);
border-radius: 20px;
padding: 3px 14px;
margin: 8px 0;
}
#input-bar {
padding: 12px 16px;
background: var(--surface);
border-top: 1px solid var(--border);
display: flex;
gap: 10px;
align-items: flex-end;
flex-shrink: 0;
}
#msg-input {
flex: 1;
background: var(--surface2);
border: 1px solid var(--border);
border-radius: 22px;
padding: 10px 16px;
color: var(--text);
font-size: 14px;
resize: none;
outline: none;
max-height: 120px;
min-height: 44px;
line-height: 1.5;
font-family: inherit;
transition: border-color 0.2s;
}
#msg-input:focus { border-color: var(--accent); }
#msg-input::placeholder { color: var(--text-muted); }
.icon-btn {
width: 44px; height: 44px;
border-radius: 50%;
border: none;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
transition: background 0.15s;
}
#btn-send {
background: var(--accent);
color: #fff;
}
#btn-send:hover { background: #5a52d5; }
#btn-attach {
background: var(--surface2);
color: var(--text-muted);
border: 1px solid var(--border);
}
#btn-attach:hover { color: var(--text); }
/* ── Toast ─────────────────────────────────────────────── */
#toast-container {
position: fixed;
bottom: 24px;
right: 24px;
display: flex;
flex-direction: column;
gap: 10px;
z-index: 1000;
}
.toast {
background: var(--surface2);
border: 1px solid var(--border);
border-left: 3px solid var(--accent);
border-radius: var(--radius);
padding: 12px 16px;
min-width: 260px;
max-width: 360px;
box-shadow: 0 8px 24px #0006;
animation: slideIn 0.25s ease;
cursor: pointer;
}
.toast-sender { font-weight: 700; font-size: 13px; }
.toast-body { color: var(--text-muted); font-size: 12px; margin-top: 2px;
white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }
/* ── Util ──────────────────────────────────────────────── */
.hidden { display: none !important; }
/* ── Responsive ────────────────────────────────────────── */
@media (max-width: 660px) {
:root { --sidebar-w: 100%; }
#sidebar { width: 100%; border-right: none; }
#thread-panel { position: absolute; inset: 0; top: 55px; }
#thread-panel.hidden-mobile { display: none; }
#btn-back { display: flex !important; }
}
</style>
</head>
<body>
<div id="app">
<!-- HEADER -->
<header id="header">
<!-- Signal/Bridge icon -->
<svg width="28" height="28" viewBox="0 0 28 28" fill="none">
<circle cx="14" cy="14" r="3" fill="#6c63ff"/>
<path d="M7 14 C7 10.13 10.13 7 14 7" stroke="#6c63ff" stroke-width="2" stroke-linecap="round" fill="none"/>
<path d="M21 14 C21 10.13 17.87 7 14 7" stroke="#6c63ff" stroke-width="2" stroke-linecap="round" fill="none"/>
<path d="M3 14 C3 7.37 7.37 3 14 3" stroke="#a78bfa" stroke-width="1.5" stroke-linecap="round" fill="none" opacity="0.6"/>
<path d="M25 14 C25 7.37 20.63 3 14 3" stroke="#a78bfa" stroke-width="1.5" stroke-linecap="round" fill="none" opacity="0.6"/>
<line x1="14" y1="17" x2="8" y2="25" stroke="#6c63ff" stroke-width="2" stroke-linecap="round"/>
<line x1="14" y1="17" x2="20" y2="25" stroke="#6c63ff" stroke-width="2" stroke-linecap="round"/>
</svg>
<h1>Nexus<span>Bridge</span></h1>
<div id="conn-status">
<div id="status-dot"></div>
<span id="status-text">Disconnected</span>
<button class="btn btn-danger" id="btn-disconnect" style="margin-left:12px;padding:6px 14px;font-size:12px;display:none">Disconnect</button>
</div>
</header>
<!-- PAIRING SCREEN -->
<div id="pairing-screen">
<div id="pairing-card">
<h2>Connect your Phone</h2>
<p>Scan the QR code with the NexusBridge Android app, or enter the PIN manually.</p>
<div id="qr-container">
<div id="qr-spinner"></div>
<div id="qr-code"></div>
</div>
<div id="pin-display">------</div>
<div id="pairing-status">
<div id="pairing-spinner"></div>
<span id="pairing-status-text">Waiting for phone...</span>
</div>
<button class="btn btn-ghost" id="btn-refresh">↻ New Session</button>
</div>
</div>
<!-- MAIN SCREEN (hidden until paired) -->
<div id="main-screen" class="hidden">
<div id="reconnect-banner">
Phone disconnected — waiting for reconnection | PIN: <strong id="banner-pin"></strong>
</div>
<!-- SIDEBAR -->
<aside id="sidebar">
<div id="sidebar-header">
<div style="display:flex;align-items:center;justify-content:space-between">
<h3>Messages</h3>
<button id="btn-refresh-msgs" title="Refresh conversations" style="background:none;border:1px solid var(--border);color:var(--text-muted);border-radius:6px;padding:4px 10px;cursor:pointer;font-size:13px" onclick="sendToPhone({type:'sms_list',payload:{}});this.textContent='↻ …';setTimeout(()=>this.textContent='↻',1500)">↻</button>
</div>
<input type="text" id="search-input" placeholder="Search conversations..." autocomplete="off" />
</div>
<div id="conversations-list">
<div id="sidebar-loading">
<div style="width:24px;height:24px;border:2px solid var(--border);border-top-color:var(--accent);border-radius:50%;animation:spin 0.8s linear infinite"></div>
<span>Loading conversations...</span>
</div>
</div>
</aside>
<!-- THREAD PANEL -->
<section id="thread-panel">
<!-- placeholder when no thread selected -->
<div id="thread-placeholder">
<svg width="64" height="64" viewBox="0 0 24 24" fill="none">
<path d="M20 2H4a2 2 0 00-2 2v18l4-4h14a2 2 0 002-2V4a2 2 0 00-2-2z" stroke="#6c63ff" stroke-width="1.5" stroke-linejoin="round"/>
<circle cx="8" cy="11" r="1" fill="#6c63ff"/>
<circle cx="12" cy="11" r="1" fill="#6c63ff"/>
<circle cx="16" cy="11" r="1" fill="#6c63ff"/>
</svg>
<p>Select a conversation to start</p>
</div>
<!-- Active thread (hidden until selected) -->
<div id="thread-view" class="hidden" style="display:flex;flex-direction:column;height:100%;">
<div id="thread-header">
<button id="btn-back" aria-label="Back">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M19 12H5M12 5l-7 7 7 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<div id="thread-avatar" class="conv-avatar" style="width:38px;height:38px;font-size:14px">?</div>
<div>
<h2 id="thread-contact-name">Contact</h2>
<div id="thread-phone">+1 000 000 0000</div>
</div>
</div>
<div id="messages-area"></div>
<div id="input-bar">
<button class="icon-btn" id="btn-attach" title="Attach file">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M21.44 11.05L12.25 20.24a5.5 5.5 0 01-7.78-7.78l9.19-9.19a3.67 3.67 0 015.19 5.19L9.66 17.65a1.83 1.83 0 01-2.59-2.59l8.49-8.48" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<textarea id="msg-input" rows="1" placeholder="Type a message..."></textarea>
<button class="icon-btn" id="btn-send" title="Send">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M22 2L11 13M22 2L15 22l-4-9-9-4 20-7z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
</div>
</div>
</section>
</div>
</div>
<!-- TOAST CONTAINER -->
<div id="toast-container"></div>
<script>
"use strict";
// ── Config ──────────────────────────────────────────────────────────────────
const SERVER_HTTP = "https://your.domain.com";
const SERVER_WS = "wss://your.domain.com";
const POLL_INTERVAL = 2000; // ms — status polling while pairing
// ── State ───────────────────────────────────────────────────────────────────
let sessionToken = null;
let sessionPin = null;
let ws = null;
let phoneConnected = false;
let activeThreadId = null;
let activeAddress = null;
// In-memory caches
const conversationsCache = {}; // threadId → conversation object
const threadsCache = {}; // threadId → messages array
let conversationList = []; // ordered list for sidebar
// Polling
let statusPollTimer = null;
let reconnectTimer = null;
// ── DOM Refs ─────────────────────────────────────────────────────────────────
const $pairingScreen = document.getElementById("pairing-screen");
const $mainScreen = document.getElementById("main-screen");
const $qrCode = document.getElementById("qr-code");
const $qrSpinner = document.getElementById("qr-spinner");
const $pinDisplay = document.getElementById("pin-display");
const $pairingStatus = document.getElementById("pairing-status-text");
const $pairingSpinner = document.getElementById("pairing-spinner");
const $btnRefresh = document.getElementById("btn-refresh");
const $statusDot = document.getElementById("status-dot");
const $statusText = document.getElementById("status-text");
const $btnDisconnect = document.getElementById("btn-disconnect");
const $reconnectBanner = document.getElementById("reconnect-banner");
const $bannerPin = document.getElementById("banner-pin");
const $convList = document.getElementById("conversations-list");
const $searchInput = document.getElementById("search-input");
const $threadPlaceholder = document.getElementById("thread-placeholder");
const $threadView = document.getElementById("thread-view");
const $messagesArea = document.getElementById("messages-area");
const $threadName = document.getElementById("thread-contact-name");
const $threadPhone = document.getElementById("thread-phone");
const $threadAvatar = document.getElementById("thread-avatar");
const $msgInput = document.getElementById("msg-input");
const $btnSend = document.getElementById("btn-send");
const $btnBack = document.getElementById("btn-back");
const $btnAttach = document.getElementById("btn-attach");
const $toastContainer = document.getElementById("toast-container");
// ── QR Code ──────────────────────────────────────────────────────────────────
let qrInstance = null;
function renderQR(data) {
$qrCode.innerHTML = "";
$qrSpinner.style.display = "none";
try {
qrInstance = new QRCode($qrCode, {
text: data,
width: 180,
height: 180,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.M,
});
} catch (e) {
$qrCode.textContent = "QR unavailable";
console.error("QR error:", e);
}
}
// ── Session Management ───────────────────────────────────────────────────────
async function createSession() {
$qrCode.innerHTML = "";
$qrSpinner.style.display = "block";
$pinDisplay.textContent = "------";
$pairingStatus.textContent = "Creating session...";
try {
const res = await fetch(`${SERVER_HTTP}/new-session`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
sessionToken = data.sessionToken;
sessionPin = data.pin;
localStorage.setItem("nb_session_token", sessionToken);
$pinDisplay.textContent = sessionPin;
renderQR(data.qrData);
$pairingStatus.textContent = "Waiting for phone...";
$pairingSpinner.style.display = "";
startStatusPolling();
} catch (err) {
$qrSpinner.style.display = "none";
$pairingStatus.textContent = "Error creating session. Retrying...";
console.error("createSession error:", err);
setTimeout(createSession, 3000);
}
}
function startStatusPolling() {
stopStatusPolling();
statusPollTimer = setInterval(async () => {
if (!sessionToken) return;
try {
const res = await fetch(`${SERVER_HTTP}/session-status/${sessionToken}`);
if (!res.ok) return;
const data = await res.json();
if (data.phoneConnected) {
stopStatusPolling();
onPhonePaired();
}
} catch (_) {}
}, POLL_INTERVAL);
}
function stopStatusPolling() {
if (statusPollTimer) { clearInterval(statusPollTimer); statusPollTimer = null; }
}
// ── Pairing Success ──────────────────────────────────────────────────────────
function onPhonePaired() {
stopStatusPolling();
$pairingScreen.classList.add("hidden");
$mainScreen.classList.remove("hidden");
$bannerPin.textContent = sessionPin;
$btnDisconnect.style.display = "";
connectWebSocket();
}
function disconnect() {
// Close WebSocket cleanly
if (ws) {
ws.onclose = null; // prevent auto-reconnect
ws.close();
ws = null;
}
clearTimeout(reconnectTimer);
// Clear stored session
localStorage.removeItem("nb_session_token");
sessionToken = null;
sessionPin = null;
phoneConnected = false;
activeThreadId = null;
activeAddress = null;
// Reset UI to pairing screen
$mainScreen.classList.add("hidden");
$pairingScreen.classList.remove("hidden");
$btnDisconnect.style.display = "none";
setConnectionStatus(false);
conversationList = [];
// Start fresh session
createSession();
}
// ── WebSocket ────────────────────────────────────────────────────────────────
let wsReconnectDelay = 1000;
function connectWebSocket() {
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) return;
const url = `${SERVER_WS}/ws/${sessionToken}?role=client`;
console.log("[WS] Connecting to", url);
ws = new WebSocket(url);
ws.addEventListener("open", () => {
console.log("[WS] Connected");
wsReconnectDelay = 1000;
setConnectionStatus(true);
// sms_list will be requested once server confirms phone is connected
// via connection_status or phone_connected message
});
ws.addEventListener("message", (evt) => {
try {
const msg = JSON.parse(evt.data);
handleServerMessage(msg);
} catch (e) {
console.warn("[WS] Non-JSON message:", evt.data);
}
});
ws.addEventListener("close", (evt) => {
console.warn("[WS] Disconnected", evt.code, evt.reason);
ws = null;
setConnectionStatus(false);
scheduleReconnect();
});
ws.addEventListener("error", (e) => {
console.error("[WS] Error:", e);
});
}
function scheduleReconnect() {
clearTimeout(reconnectTimer);
console.log(`[WS] Reconnecting in ${wsReconnectDelay}ms...`);
reconnectTimer = setTimeout(() => {
connectWebSocket();
}, wsReconnectDelay);
wsReconnectDelay = Math.min(wsReconnectDelay * 2, 30000);
}
function sendToPhone(msgObj) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(msgObj));
} else {
console.warn("[WS] Cannot send — not connected:", msgObj.type);
}
}
// ── Handle Incoming Messages ─────────────────────────────────────────────────
function handleServerMessage(msg) {
const { type, payload } = msg;
switch (type) {
case "ping":
sendToPhone({ type: "pong", payload: {} });
break;
case "pong":
break;
case "connection_status":
if (payload.phoneConnected !== undefined) {
setPhoneStatus(payload.phoneConnected);
if (payload.phoneConnected && conversationList.length === 0) {
// Phone was already connected when we opened — request conversations
setTimeout(() => sendToPhone({ type: "sms_list", payload: {} }), 400);
}
}
break;
case "phone_connected":
setPhoneStatus(true);
showToast("NexusBridge", "Phone connected ✓");
// Short delay — phone needs a moment to be fully ready after WS open
setTimeout(() => sendToPhone({ type: "sms_list", payload: {} }), 800);
break;
case "phone_disconnected":
setPhoneStatus(false);
break;
case "sms_list":
handleSmsList(payload);
break;
case "sms_thread":
handleSmsThread(payload);
break;
case "sms_incoming":
handleIncomingSms(payload);
break;
case "contacts_list":
handleContactsList(payload);
break;
case "mms_attachment":
handleMmsAttachment(payload);
break;
case "sms_sent_ack":
handleSentAck(payload);
break;
case "read_receipt":
// Acknowledge
break;
default:
console.log("[MSG] Unhandled type:", type, payload);
}
}
// ── SMS List ─────────────────────────────────────────────────────────────────
function handleSmsList(payload) {
const convs = payload.conversations || payload || [];
conversationList = convs;
convs.forEach(c => { conversationsCache[c.threadId] = c; });
renderConversationList(convs);
}
function renderConversationList(convs) {
if (!convs || convs.length === 0) {
$convList.innerHTML = `<div id="sidebar-empty" style="padding:40px 20px;text-align:center;color:var(--text-muted)">No conversations found.</div>`;
return;
}
const query = ($searchInput.value || "").toLowerCase();
const filtered = query
? convs.filter(c =>
(c.displayName || c.address || "").toLowerCase().includes(query) ||
(c.snippet || "").toLowerCase().includes(query))
: convs;
$convList.innerHTML = "";
filtered.forEach(conv => {
const el = createConvItem(conv);
$convList.appendChild(el);
});
}
function createConvItem(conv) {
const div = document.createElement("div");
div.className = "conv-item" + (conv.threadId === activeThreadId ? " active" : "");
div.dataset.threadId = conv.threadId;
div.dataset.address = conv.address || "";
const initial = (conv.displayName || conv.address || "?")[0].toUpperCase();
const color = stringToColor(conv.displayName || conv.address || "");
div.innerHTML = `
<div class="conv-avatar" style="background:${color}">${escapeHtml(initial)}</div>
<div class="conv-info">
<div class="conv-name">${escapeHtml(conv.displayName || conv.address || "Unknown")}</div>
<div class="conv-preview">${escapeHtml(truncate(conv.snippet || "", 55))}</div>
</div>
<div class="conv-meta">
<div class="conv-time">${formatRelativeTime(conv.date)}</div>
<div class="unread-badge ${conv.unreadCount > 0 ? 'visible' : ''}">${conv.unreadCount || ""}</div>
</div>
`;
div.addEventListener("click", () => openThread(conv));
return div;
}
// ── Thread View ──────────────────────────────────────────────────────────────
function openThread(conv) {
activeThreadId = conv.threadId;
activeAddress = conv.address;
// Update active state in sidebar
document.querySelectorAll(".conv-item").forEach(el => {
el.classList.toggle("active", el.dataset.threadId === String(conv.threadId));
});
// Zero unread badge in cache + DOM immediately
if (conversationsCache[conv.threadId]) {
conversationsCache[conv.threadId].unreadCount = 0;
}
const activeItem = document.querySelector(`.conv-item[data-thread-id="${conv.threadId}"]`);
if (activeItem) {
const badge = activeItem.querySelector(".unread-badge");
if (badge) { badge.textContent = ""; badge.classList.remove("visible"); }
}
const color = stringToColor(conv.displayName || conv.address || "");
const initial = (conv.displayName || conv.address || "?")[0].toUpperCase();
$threadAvatar.style.background = color;
$threadAvatar.textContent = initial;
$threadName.textContent = conv.displayName || conv.address || "Unknown";
$threadPhone.textContent = conv.address || "";
$threadPlaceholder.classList.add("hidden");
$threadView.classList.remove("hidden");
// Show cached or request from phone
if (threadsCache[conv.threadId]) {
renderMessages(threadsCache[conv.threadId]);
} else {