-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1185 lines (1115 loc) · 41.5 KB
/
index.html
File metadata and controls
1185 lines (1115 loc) · 41.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" class="dark">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ComfyUI Monitor Pro</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
darkMode: "class",
theme: {
extend: {
colors: {
gray: {
850: "#1f2937",
900: "#111827",
950: "#0b0f19",
},
},
},
},
};
</script>
<!-- Import Map -->
<script type="importmap">
{
"imports": {
"react": "https://esm.sh/react@18.2.0",
"react-dom/client": "https://esm.sh/react-dom@18.2.0/client",
"lucide-react": "https://esm.sh/lucide-react@0.309.0",
"@stable-canvas/comfyui-client": "https://esm.sh/@stable-canvas/comfyui-client@1.5.6"
}
}
</script>
<!-- Babel Standalone -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<style>
body {
background-color: #0b0f19;
color: #e5e7eb;
}
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-track {
background: #111827;
}
::-webkit-scrollbar-thumb {
background: #374151;
border-radius: 3px;
}
.img-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
gap: 8px;
}
.img-grid-lg {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 12px;
}
.fade-in {
animation: fadeIn 0.3s ease-in-out;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(5px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel" data-type="module">
import React, {
useState,
useEffect,
useMemo,
useCallback,
useRef,
} from "react";
import { createRoot } from "react-dom/client";
import { Client } from "@stable-canvas/comfyui-client";
import {
Activity,
Server,
Clock,
CheckCircle2,
XCircle,
PlayCircle,
PauseCircle,
Trash2,
ChevronLeft,
ChevronRight,
Zap,
Monitor,
Maximize2,
X,
FileJson,
ChevronDown,
Image as ImageIcon,
LayoutGrid,
List,
FileText,
AlertTriangle,
Github,
ExternalLink,
AlertOctagon
} from "lucide-react";
// --- Utils ---
const formatBytes = (bytes) => {
if (!bytes) return "0 B";
const sizes = ["B", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return (
parseFloat((bytes / Math.pow(1024, i)).toFixed(1)) + " " + sizes[i]
);
};
const getBaseUrl = (host) =>
host.startsWith("http") ? host : `http://${host}`;
const extractImages = (item, host) => {
const results = [];
if (!item.outputs) return results;
Object.values(item.outputs).forEach((out) => {
if (out.images) {
out.images.forEach((img) => {
if (!img.filename) return;
const params = new URLSearchParams({
filename: img.filename,
subfolder: img.subfolder || "",
type: img.type || "output",
});
results.push({
src: `${getBaseUrl(host)}/view?${params}`,
type: img.type || "output",
filename: img.filename,
});
});
}
const appendBase64 = (txt) => {
const src = txt.startsWith("data:")
? txt
: `data:image/png;base64,${txt}`;
if (!results.some((r) => r.src === src))
results.push({ src, type: "b64" });
};
if (out.text && Array.isArray(out.text)) {
out.text.forEach((txt) => {
if (typeof txt === "string" && txt.length > 500) {
if (txt.startsWith("[") && txt.endsWith("]")) {
try {
const json = JSON.parse(txt);
if (Array.isArray(json)) json.forEach(appendBase64);
} catch (e) {
console.error(e);
}
return;
}
appendBase64(txt);
}
});
}
if (out.base64Images && Array.isArray(out.base64Images)) {
out.base64Images.forEach(appendBase64);
}
});
return results;
};
// --- IndexedDB ---
class HistoryDB {
constructor() {
this.dbName = "ComfyMonitorDB";
this.storeName = "history";
this.db = null;
}
async connect() {
if (this.db) return this.db;
return new Promise((resolve, reject) => {
const req = indexedDB.open(this.dbName, 1);
req.onupgradeneeded = (e) => {
const db = e.target.result;
const tx = e.target.transaction;
let store;
if (!db.objectStoreNames.contains(this.storeName)) {
store = db.createObjectStore(this.storeName, { keyPath: "id" });
} else {
store = tx.objectStore(this.storeName);
}
if (!store.indexNames.contains("endpoint_ts")) {
store.createIndex("endpoint_ts", ["endpoint", "timestamp"]);
}
};
req.onsuccess = (e) => {
this.db = e.target.result;
resolve(this.db);
};
req.onerror = (e) => reject(e.target.error);
});
}
async saveItems(items, endpoint) {
const db = await this.connect();
return new Promise((resolve, reject) => {
const tx = db.transaction([this.storeName], "readwrite");
const store = tx.objectStore(this.storeName);
items.forEach((item) => {
const promptId = item.prompt[1];
const startMsg = item.status?.messages?.find(
(m) => m[0] === "execution_start"
);
const ts = startMsg ? startMsg[1].timestamp : Date.now();
store.put({
id: `${endpoint}_${promptId}`,
endpoint,
timestamp: ts,
promptId,
data: item,
});
});
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
});
}
async getPaginated(endpoint, page = 1, pageSize = 20) {
const db = await this.connect();
const skip = (page - 1) * pageSize;
const items = [];
return new Promise((resolve, reject) => {
const tx = db.transaction([this.storeName], "readonly");
const store = tx.objectStore(this.storeName);
const index = store.index("endpoint_ts");
const range = IDBKeyRange.bound(
[endpoint, 0],
[endpoint, Infinity]
);
const request = index.openCursor(range, "prev");
let advanced = false;
request.onsuccess = (e) => {
const cursor = e.target.result;
if (!cursor) {
resolve({ items, hasMore: false });
return;
}
if (skip > 0 && !advanced) {
advanced = true;
cursor.advance(skip);
return;
}
if (cursor.value.endpoint === endpoint) {
items.push(cursor.value.data);
}
if (items.length < pageSize) {
cursor.continue();
} else {
resolve({ items, hasMore: true });
}
};
request.onerror = () => reject(request.error);
});
}
async clear(endpoint) {
const db = await this.connect();
return new Promise((resolve, reject) => {
const tx = db.transaction([this.storeName], "readwrite");
const store = tx.objectStore(this.storeName);
const index = store.index("endpoint_ts");
const range = IDBKeyRange.bound(
[endpoint, 0],
[endpoint, Infinity]
);
const req = index.openCursor(range);
req.onsuccess = (e) => {
const cursor = e.target.result;
if (cursor) {
cursor.delete();
cursor.continue();
} else {
resolve();
}
};
req.onerror = () => reject(req.error);
});
}
}
const historyDB = new HistoryDB();
// --- Components ---
const ProgressBar = ({ current, total, color }) => {
const percent = Math.min(
100,
Math.max(0, ((total - current) / total) * 100)
);
const colors = { blue: "bg-blue-600", purple: "bg-purple-600" };
return (
<div className="h-1.5 w-full bg-gray-800 rounded-full overflow-hidden">
<div
className={`h-full ${colors[color]} transition-all duration-500`}
style={{ width: `${percent}%` }}
></div>
</div>
);
};
const ImageThumbnail = ({ src, type, onClick, className }) => (
<div
className={`relative aspect-square bg-gray-950 rounded border border-gray-800 overflow-hidden cursor-zoom-in group ${className}`}
onClick={(e) => {
e.stopPropagation();
onClick(src);
}}
>
<img
src={src}
className="w-full h-full object-cover"
loading="lazy"
/>
<div className="absolute inset-0 bg-white/10 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center">
<Maximize2 size={16} className="text-white drop-shadow-md" />
</div>
</div>
);
const Lightbox = ({ src, onClose }) => {
if (!src) return null;
return (
<div
className="fixed inset-0 z-[100] bg-black/95 backdrop-blur-md flex items-center justify-center p-4 animate-in fade-in duration-200"
onClick={onClose}
>
<button
className="absolute top-4 right-4 text-white hover:text-red-400 p-2"
onClick={onClose}
>
<X size={32} />
</button>
<img
src={src}
className="max-w-full max-h-full rounded shadow-2xl"
onClick={(e) => e.stopPropagation()}
/>
</div>
);
};
// --- CORS Help Modal ---
const CorsHelpModal = ({ isOpen, onClose }) => {
if (!isOpen) return null;
return (
<div className="fixed inset-0 z-[200] bg-black/80 backdrop-blur-sm flex items-center justify-center p-4 animate-in fade-in duration-200">
<div className="bg-[#1a202e] border border-red-500/50 p-6 rounded-xl max-w-lg w-full shadow-2xl relative">
<div className="flex items-center gap-3 mb-4 text-red-400">
<AlertOctagon size={32} />
<h2 className="text-xl font-bold">Connection Failed / CORS Error</h2>
</div>
<p className="text-gray-300 mb-4 text-sm leading-relaxed">
Unable to connect to ComfyUI. This is usually because ComfyUI is not running,
or <b>CORS (Cross-Origin Resource Sharing)</b> is blocking the connection.
</p>
<div className="bg-black/40 border border-gray-700 rounded-lg p-3 mb-4">
<p className="text-xs text-gray-500 mb-1 uppercase font-semibold">Solution:</p>
<p className="text-sm text-gray-300 mb-2">Please restart ComfyUI with the following argument:</p>
<code className="block bg-gray-950 text-green-400 p-2 rounded border border-gray-800 font-mono text-sm select-all">
--enable-cors-header=*
</code>
</div>
<p className="text-xs text-gray-500 mb-6">
This allows the monitor (running in the browser) to communicate with your local API server.
</p>
<div className="flex justify-end">
<button
onClick={onClose}
className="bg-gray-700 hover:bg-gray-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors"
>
I Understand, Close
</button>
</div>
</div>
</div>
);
};
const JsonView = ({ data, level = 0, name = null }) => {
const [collapsed, setCollapsed] = useState(level > 1);
const isObject = data !== null && typeof data === "object";
const isArray = Array.isArray(data);
if (!isObject) {
let color = "text-gray-300";
if (typeof data === "string") color = "text-green-400";
if (typeof data === "number") color = "text-blue-400";
if (typeof data === "boolean") color = "text-purple-400";
return (
<div className="font-mono text-xs pl-4 py-0.5">
{name && <span className="text-gray-500 mr-1">{name}:</span>}
<span className={`${color} break-all`}>
{JSON.stringify(data)}
</span>
</div>
);
}
const keys = Object.keys(data);
const isEmpty = keys.length === 0;
return (
<div className="font-mono text-xs pl-2">
<div
className="flex items-center gap-1 hover:bg-gray-800/50 rounded px-1 cursor-pointer select-none py-0.5"
onClick={() => !isEmpty && setCollapsed(!collapsed)}
>
<span className="text-gray-500 w-4">
{!isEmpty &&
(collapsed ? (
<ChevronRight size={12} />
) : (
<ChevronDown size={12} />
))}
</span>
{name && <span className="text-purple-300 mr-1">{name}:</span>}
<span className="text-gray-500">{isArray ? "[" : "{"}</span>
{collapsed && !isEmpty && (
<span className="text-gray-600 mx-1">...</span>
)}
{collapsed && (
<span className="text-gray-500">
{isArray ? "]" : "}"}{" "}
<span className="text-gray-600 text-[10px] ml-2">
// {keys.length} items
</span>
</span>
)}
</div>
{!collapsed && (
<div className="border-l border-gray-800 ml-2.5">
{keys.map((key) => (
<JsonView
key={key}
name={isArray ? null : key}
data={data[key]}
level={level + 1}
/>
))}
<div className="pl-4 text-gray-500">{isArray ? "]" : "}"}</div>
</div>
)}
</div>
);
};
const DetailModal = ({ item, host, onClose, onImageClick }) => {
if (!item) return null;
const images = extractImages(item, host);
return (
<div
className="fixed inset-0 z-[50] bg-black/80 backdrop-blur-sm flex items-center justify-center p-4 sm:p-6"
onClick={onClose}
>
<div
className="bg-[#131825] w-full max-w-5xl h-[90vh] rounded-xl border border-gray-800 shadow-2xl flex flex-col overflow-hidden animate-in zoom-in-95 duration-200"
onClick={(e) => e.stopPropagation()}
>
<div className="flex items-center justify-between p-4 border-b border-gray-800 bg-gray-900/50">
<div className="flex items-center gap-3">
<div
className={`p-2 rounded-lg ${item.status?.status_str === "success"
? "bg-green-900/20 text-green-400"
: "bg-red-900/20 text-red-400"
}`}
>
{item.status?.status_str === "success" ? (
<CheckCircle2 size={20} />
) : (
<XCircle size={20} />
)}
</div>
<div>
<h2 className="text-lg font-bold text-gray-100">
Task Details
</h2>
<div className="text-xs text-gray-500 font-mono">
{item.prompt[1]}
</div>
</div>
</div>
<button
onClick={onClose}
className="p-2 hover:bg-gray-800 rounded-lg text-gray-400 hover:text-white"
>
<X size={20} />
</button>
</div>
<div className="flex-1 overflow-y-auto p-6 space-y-8 custom-scrollbar">
{images.length > 0 && (
<section>
<h3 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-3 flex items-center gap-2">
<ImageIcon size={16} /> Generated Images ({images.length})
</h3>
<div className="img-grid-lg">
{images.map((img, i) => (
<div key={i} className="space-y-1">
<ImageThumbnail
src={img.src}
type={img.type}
onClick={onImageClick}
className="rounded-lg shadow-lg"
/>
<div className="text-[10px] text-gray-500 truncate px-1">
{img.filename}
</div>
</div>
))}
</div>
</section>
)}
{item.status?.messages && (
<section>
<h3 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-3 flex items-center gap-2">
<Activity size={16} /> Execution Logs
</h3>
<div className="bg-black/30 rounded-lg border border-gray-800 p-4 font-mono text-xs text-gray-400 max-h-60 overflow-auto">
{item.status.messages.map((msg, i) => (
<div
key={i}
className="border-b border-gray-800/50 last:border-0 py-1 flex gap-4"
>
<span className="text-blue-400 min-w-[120px]">
{msg[0]}
</span>
<span className="text-gray-500">
{new Date(msg[1].timestamp).toLocaleTimeString()}
</span>
{msg[1].node_id && (
<span className="text-gray-600">
Node: {msg[1].node_id}
</span>
)}
</div>
))}
</div>
</section>
)}
<section>
<h3 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-3 flex items-center gap-2">
<FileJson size={16} /> Payload Inspector
</h3>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
<div className="bg-black/20 rounded-lg border border-gray-800 p-3 overflow-auto max-h-[500px]">
<div className="text-xs text-gray-500 mb-2 font-bold">
WORKFLOW / PROMPT
</div>
<JsonView data={item.prompt[2]} />
</div>
<div className="bg-black/20 rounded-lg border border-gray-800 p-3 overflow-auto max-h-[500px]">
<div className="text-xs text-gray-500 mb-2 font-bold">
OUTPUTS
</div>
<JsonView data={item.outputs} />
</div>
</div>
</section>
</div>
</div>
</div>
);
};
const QueueItem = ({ item, status }) => (
<div className="bg-black/20 border border-gray-800 p-2 rounded relative overflow-hidden">
{status === "Running" && (
<div className="absolute left-0 top-0 bottom-0 w-0.5 bg-blue-500 animate-pulse"></div>
)}
<div className="flex justify-between items-center mb-1">
<span
className={`text-[9px] uppercase font-bold px-1 rounded ${status === "Running" ? "text-blue-400" : "text-yellow-400"
}`}
>
{status}
</span>
<span className="text-[9px] text-gray-600 font-mono">
{item[1].slice(0, 6)}
</span>
</div>
<div className="text-[10px] text-gray-400 truncate">
{Object.values(item[2]).length} nodes
</div>
</div>
);
const LeftPanel = ({ systemStats, queue, connectionStatus, latency }) => {
const sys = systemStats?.system || {};
const device = systemStats?.devices?.[0] || {};
return (
<div className="flex flex-col gap-4 h-full">
<div className="bg-gray-900 border border-gray-800 rounded-xl p-4 shadow-lg">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center gap-2">
<div
className={`w-2.5 h-2.5 rounded-full ${connectionStatus === "connected"
? "bg-green-500 shadow-[0_0_8px_rgba(34,197,94,0.6)]"
: "bg-red-500"
}`}
></div>
<span className="text-sm font-medium text-white">
{connectionStatus === "connected" ? "Online" : "Offline"}
</span>
</div>
{latency && (
<span className="text-[10px] bg-gray-800 px-1.5 py-0.5 rounded text-gray-400">
{latency}ms
</span>
)}
</div>
{systemStats && (
<div className="space-y-3">
<div>
<div className="flex justify-between text-[10px] text-gray-400 mb-1">
<span>GPU VRAM</span>
<span>
{formatBytes(device.vram_free)} /{" "}
{formatBytes(device.vram_total)}
</span>
</div>
<ProgressBar
current={device.vram_free}
total={device.vram_total}
color="purple"
/>
</div>
<div>
<div className="flex justify-between text-[10px] text-gray-400 mb-1">
<span>RAM</span>
<span>
{formatBytes(sys.ram_free)} /{" "}
{formatBytes(sys.ram_total)}
</span>
</div>
<ProgressBar
current={sys.ram_free}
total={sys.ram_total}
color="blue"
/>
</div>
</div>
)}
</div>
<div className="bg-gray-900 border border-gray-800 rounded-xl p-4 flex-1 shadow-lg overflow-hidden flex flex-col">
<div className="flex items-center justify-between mb-3">
<h2 className="text-xs font-semibold text-gray-400 uppercase tracking-wider flex items-center gap-2">
<Activity size={14} /> Queue
</h2>
<span className="bg-gray-800 text-white px-1.5 py-0.5 rounded text-[10px]">
{queue.Running.length + queue.Pending.length}
</span>
</div>
<div className="flex-1 overflow-y-auto space-y-2 pr-1 custom-scrollbar">
{queue.Running.map((item) => (
<QueueItem key={item[1]} item={item} status="Running" />
))}
{queue.Pending.map((item) => (
<QueueItem key={item[1]} item={item} status="Pending" />
))}
{queue.Running.length === 0 && queue.Pending.length === 0 && (
<div className="text-center text-gray-700 text-xs py-10">
Queue Empty
</div>
)}
</div>
</div>
</div>
);
};
const HistoryItem = ({ item, host, onImageClick, onClick }) => {
const id = item.prompt[1];
const status = item.status?.status_str || "unknown";
const isSuccess = status === "success";
const images = useMemo(() => extractImages(item, host), [item, host]);
return (
<div
onClick={onClick}
className="bg-gray-900 border border-gray-800 rounded p-3 group hover:border-gray-600 cursor-pointer transition-all hover:bg-gray-800/30"
>
<div className="flex items-start justify-between mb-2">
<div className="flex items-center gap-2">
{isSuccess ? (
<CheckCircle2 size={16} className="text-emerald-500" />
) : (
<XCircle size={16} className="text-red-500" />
)}
<div>
<div
className={`text-xs font-bold ${isSuccess ? "text-gray-200" : "text-red-400"
}`}
>
{status.toUpperCase()}
</div>
<div className="text-[10px] text-gray-500 font-mono">
{id}
</div>
</div>
</div>
<div className="text-[9px] text-gray-600 group-hover:text-gray-400 transition-colors">
Click for details
</div>
</div>
{images.length > 0 && (
<div className="img-grid pt-2 border-t border-gray-800/50">
{images.slice(0, 5).map((img, i) => (
<ImageThumbnail
key={i}
src={img.src}
type={img.type}
onClick={onImageClick}
/>
))}
{images.length > 5 && (
<div className="bg-gray-800 rounded flex items-center justify-center text-[10px] text-gray-400">
+{images.length - 5}
</div>
)}
</div>
)}
</div>
);
};
const HistoryGridItem = ({ item, host, onClick }) => {
const id = item.prompt[1];
const status = item.status?.status_str || "unknown";
const isSuccess = status === "success";
const images = useMemo(() => extractImages(item, host), [item, host]);
const cover = images.length > 0 ? images[0].src : null;
return (
<div
onClick={onClick}
className="relative aspect-square group rounded-lg overflow-hidden border border-gray-800 bg-gray-900 hover:border-gray-500 cursor-pointer transition-all shadow-md"
>
{cover ? (
<img
src={cover}
className="w-full h-full object-cover transition-transform duration-500 group-hover:scale-110"
loading="lazy"
/>
) : (
<div className="w-full h-full flex flex-col items-center justify-center text-gray-700 bg-gray-900/50">
{isSuccess ? (
<FileText size={32} />
) : (
<AlertTriangle size={32} className="text-red-900" />
)}
<span className="text-[10px] mt-2">
{isSuccess ? "No Image" : "Failed"}
</span>
</div>
)}
<div className="absolute top-2 right-2 z-10">
{isSuccess ? (
<div className="bg-green-500/20 text-green-400 p-1 rounded-full backdrop-blur-md">
<CheckCircle2 size={14} />
</div>
) : (
<div className="bg-red-500/20 text-red-400 p-1 rounded-full backdrop-blur-md">
<XCircle size={14} />
</div>
)}
</div>
<div className="absolute inset-x-0 bottom-0 p-3 bg-gradient-to-t from-black/90 via-black/60 to-transparent translate-y-full group-hover:translate-y-0 transition-transform duration-300 flex justify-between items-end">
<div className="overflow-hidden">
<div className="text-[10px] text-gray-300 font-mono truncate">
{id}
</div>
{images.length > 1 && (
<div className="text-[9px] text-blue-400">
{images.length} images
</div>
)}
</div>
<Maximize2 size={14} className="text-white opacity-80" />
</div>
</div>
);
};
const RightPanel = ({
endpoint,
refreshTrigger,
onImageClick,
onSelect,
}) => {
const [history, setHistory] = useState([]);
const [page, setPage] = useState(1);
const [hasMore, setHasMore] = useState(false);
const [loading, setLoading] = useState(false);
const [viewMode, setViewMode] = useState("list");
const loadData = useCallback(
async (reset = false) => {
setLoading(true);
if (reset) {
setPage(1);
setHistory([]);
}
const p = reset ? 1 : page;
const limit = viewMode === "grid" ? 30 : 20;
try {
const res = await historyDB.getPaginated(endpoint, p, limit);
setHistory(res.items);
setHasMore(res.hasMore);
} finally {
setLoading(false);
}
},
[endpoint, page, viewMode]
);
useEffect(() => {
loadData(true);
}, [endpoint, viewMode]);
useEffect(() => {
loadData();
}, [refreshTrigger]);
useEffect(() => {
loadData();
}, [page]);
return (
<div className="flex flex-col h-full bg-gray-900 border border-gray-800 rounded-xl shadow-lg overflow-hidden">
<div className="p-3 border-b border-gray-800 bg-gray-900 flex justify-between items-center shrink-0">
<h2 className="text-sm font-semibold text-white flex items-center gap-2">
<Clock size={16} className="text-gray-400" /> History
<span className="text-[10px] text-gray-600 bg-gray-800 px-1 rounded border border-gray-700 hidden sm:inline-block truncate max-w-[150px]">
{endpoint}
</span>
</h2>
<div className="flex items-center gap-3">
<div className="bg-gray-800 rounded p-1 flex gap-1">
<button
onClick={() => setViewMode("list")}
className={`p-1 rounded transition-colors ${viewMode === "list"
? "bg-gray-700 text-white shadow"
: "text-gray-500 hover:text-gray-300"
}`}
title="List View"
>
<List size={16} />
</button>
<button
onClick={() => setViewMode("grid")}
className={`p-1 rounded transition-colors ${viewMode === "grid"
? "bg-gray-700 text-white shadow"
: "text-gray-500 hover:text-gray-300"
}`}
title="Gallery View"
>
<LayoutGrid size={16} />
</button>
</div>
<div className="h-4 w-[1px] bg-gray-700 mx-1"></div>
<button
onClick={async () => {
if (confirm(`Clear history for ${endpoint}?`)) {
await historyDB.clear(endpoint);
loadData(true);
}
}}
className="text-gray-600 hover:text-red-400"
>
<Trash2 size={16} />
</button>
</div>
</div>
<div className="flex-1 overflow-y-auto p-4 bg-[#0d121f] custom-scrollbar">
{history.length === 0 && !loading && (
<div className="text-center py-10 text-gray-600 text-sm">
No history data for this server
</div>
)}
{viewMode === "list" ? (
<div className="space-y-3 fade-in">
{history.map((item) => (
<HistoryItem
key={item.prompt[1]}
item={item}
host={endpoint}
onImageClick={onImageClick}
onClick={() => onSelect(item)}
/>
))}
</div>
) : (
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 2xl:grid-cols-6 gap-3 fade-in">
{history.map((item) => (
<HistoryGridItem
key={item.prompt[1]}
item={item}
host={endpoint}
onClick={() => onSelect(item)}
/>
))}
</div>
)}
</div>
<div className="p-2 border-t border-gray-800 bg-gray-900 flex justify-between items-center text-xs shrink-0">
<span className="text-gray-500">Page {page}</span>
<div className="flex gap-2">
<button
onClick={() => setPage((p) => Math.max(1, p - 1))}
disabled={page === 1 || loading}
className="px-2 py-1 bg-gray-800 rounded hover:bg-gray-700 disabled:opacity-50"
>
<ChevronLeft size={12} />
</button>
<button
onClick={() => setPage((p) => p + 1)}
disabled={!hasMore || loading}
className="px-2 py-1 bg-gray-800 rounded hover:bg-gray-700 disabled:opacity-50"
>
<ChevronRight size={12} />
</button>
</div>
</div>
</div>
);
};
class WorkerTimer {
constructor() {
this.worker = null;
this.blobUrl = null;
}
_createWorker(type, time) {
this.stop();
const fnCode = `
const time = ${time};
const type = '${type}';
if (type === 'timeout') {
setTimeout(function() {
self.postMessage('tick');
}, time);
} else if (type === 'interval') {
setInterval(function() {
self.postMessage('tick');
}, time);
}
`;
const blob = new Blob([fnCode], { type: 'application/javascript' });
this.blobUrl = URL.createObjectURL(blob);
this.worker = new Worker(this.blobUrl);
}
setTimeout(callback, delay) {
this._createWorker('timeout', delay);
this.worker.onmessage = () => {
if (typeof callback === 'function') callback();
this.stop();
};
}
setInterval(callback, delay) {
this._createWorker('interval', delay);
this.worker.onmessage = () => {