-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1582 lines (1551 loc) · 86.4 KB
/
index.html
File metadata and controls
1582 lines (1551 loc) · 86.4 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" />
<link rel="icon" href="data:,">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NetPulse - Real-time Latency Monitor</title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
darkMode: 'class', // Enable class-based dark mode
theme: {
extend: {
colors: {
'gray-950': '#0d1117',
'gray-900': '#161b22',
'gray-800': '#21262d',
'gray-700': '#30363d',
'gray-600': '#484f58',
},
animation: {
'bg-scroll': 'bg-scroll 2s linear infinite',
'bg-scroll-reverse': 'bg-scroll-reverse 2s linear infinite',
},
keyframes: {
'bg-scroll': {
'0%': { 'background-position': '100% 0' },
'100%': { 'background-position': '-100% 0' },
},
'bg-scroll-reverse': {
'0%': { 'background-position': '-100% 0' },
'100%': { 'background-position': '100% 0' },
}
}
}
}
}
</script>
<script>
// This script runs before React to prevent a flash of the wrong theme.
if (localStorage.getItem('netPulseTheme') === 'dark' || (!('netPulseTheme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js" integrity="sha512-BNaRQnYJYiPSqHHDb58B0yaPfCu+Wgds8Gp/gU33kqBtgNS4tSPHuGibyoeqMV/TJlSKda6FXzoEyYGjTe+vXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js" integrity="sha512-qZvrmS2ekKPF2mSznTQsxqPgnpkI4DNTlrdUmTzrDgektczlKNRRhy5X5AAOnx5S09ydFYWWNSfcEqDTTHgtNA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"></script>
<!-- React UMD scripts -->
<script src="https://unpkg.com/react@18/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js" crossorigin></script>
<!-- Babel Standalone for in-browser transpilation -->
<!-- <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> - Network version can be used if not using Firefox -->
<script src="./babel-standalone.js"></script> <!-- Local copy used due to Mozilla Bug:1437937 https://bugzilla.mozilla.org/show_bug.cgi?id=1437937 -->
<link rel="stylesheet" href="/index.css">
</head>
<body class="bg-slate-100 text-slate-800 dark:bg-gray-950 dark:text-gray-300">
<div id="root"></div>
<script type="text/babel" data-presets="react,typescript" data-plugins="transform-modules-umd">
// All application code is consolidated here.
// The individual .ts/.tsx files are no longer used due to improve single file/portability.
// --- From types.ts ---
// --- From utils/rttUtils.ts ---
const calculateStats = (results) => {
const validResults = results.filter((r) => r !== null);
const successfulPings = validResults.filter(r => r.status === 'success');
const rttValues = successfulPings.map(r => r.rtt);
const sent = validResults.length;
const received = successfulPings.length;
const loss = sent > 0 ? ((sent - received) / sent) * 100 : 0;
if (received === 0) {
return { min: 0, avg: 0, max: 0, loss, sent, received };
}
const min = Math.min(...rttValues);
const max = Math.max(...rttValues);
const avg = Math.round(rttValues.reduce((sum, val) => sum + val, 0) / rttValues.length);
return { min, avg, max, loss, sent, received };
};
const getTimelineColor = (result, criticalThreshold) => {
if (!result || result.status === 'failed') {
return 'bg-blue-600';
}
const { rtt } = result;
const excellent = criticalThreshold * 0.25;
const good = criticalThreshold * 0.50;
const poor = criticalThreshold * 1.0;
if (rtt <= excellent) return 'bg-green-500';
if (rtt <= good) return 'bg-yellow-500';
if (rtt <= poor) return 'bg-orange-500';
return 'bg-red-600';
};
const getLatencyDescription = (result, criticalThreshold) => {
if (!result || result.status === 'failed') {
return 'Request Failed';
}
const { rtt } = result;
const excellent = criticalThreshold * 0.25;
const good = criticalThreshold * 0.50;
const poor = criticalThreshold * 1.0;
if (rtt <= excellent) return 'Excellent';
if (rtt <= good) return 'Good';
if (rtt <= poor) return 'Poor';
return 'Very Poor';
};
// --- From utils/netUtils.ts ---
const IPV4_REGEX = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const IPV6_REGEX = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/i;
const DOMAIN_REGEX = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i;
const validateHostFormat = (host) => {
if (IPV4_REGEX.test(host)) return { isValid: true, type: 'ipv4' };
if (IPV6_REGEX.test(host)) return { isValid: true, type: 'ipv6' };
if (DOMAIN_REGEX.test(host)) return { isValid: true, type: 'domain' };
return { isValid: false, type: 'invalid' };
};
const resolveHost = async (host) => {
const validation = validateHostFormat(host);
if (!validation.isValid) {
return { result: null, error: 'Invalid host format.' };
}
let queryName = host;
let queryType = 'A';
if (validation.type === 'ipv4') {
queryName = host.split('.').reverse().join('.') + '.in-addr.arpa';
queryType = 'PTR';
} else if (validation.type === 'ipv6') {
return { result: `Valid IPv6 address`, error: null };
}
try {
const response = await fetch(`https://cloudflare-dns.com/dns-query?name=${encodeURIComponent(queryName)}&type=${queryType}`, {
headers: { 'accept': 'application/dns-json' },
});
if (!response.ok) {
throw new Error(`DNS query failed with status ${response.status}`);
}
const data = await response.json();
if (data.Status !== 0 || !data.Answer || data.Answer.length === 0) {
if (validation.type === 'domain') return { result: null, error: 'Host not found.' };
if (validation.type === 'ipv4') return { result: `No PTR record found.`, error: null };
return { result: null, error: 'DNS record not found.' };
}
const answer = data.Answer[0].data.replace(/"/g, '').replace(/\.$/, '');
if (validation.type === 'domain') {
return { result: `Resolves to: ${answer}`, error: null };
}
if (validation.type === 'ipv4') {
return { result: `PTR: ${answer}`, error: null };
}
return { result: null, error: null };
} catch (err) {
console.error('DNS resolution error:', err);
return { result: null, error: 'DNS resolution failed. Check network connection.' };
}
};
const getLocalIpAddress = () => {
return new Promise((resolve) => {
try {
const rtc = new RTCPeerConnection({ iceServers: [] });
rtc.createDataChannel('');
rtc.onicecandidate = (e) => {
if (!e.candidate || !e.candidate.candidate) return;
const candidate = e.candidate.candidate;
const ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/;
const match = ipRegex.exec(candidate);
if (match && match[1] && match[1] !== '127.0.0.1') {
rtc.onicecandidate = null;
try { rtc.close(); } catch (err) {}
resolve(match[1]);
}
};
rtc.createOffer()
.then(offer => rtc.setLocalDescription(offer))
.catch(err => {
console.error("WebRTC offer creation failed:", err);
resolve(null);
});
setTimeout(() => {
try { rtc.close(); } catch(err) {}
resolve(null);
}, 1000);
} catch (err) {
console.error("WebRTC initialization failed:", err);
resolve(null);
}
});
};
const getExternalIpAddress = async () => {
try {
const response = await fetch('https://api.ipify.org?format=json');
if (!response.ok) throw new Error('Failed to fetch external IP');
const data = await response.json();
return data.ip || null;
} catch (error) {
console.error("Could not fetch external IP:", error);
return null;
}
};
// --- From utils/favicon.ts ---
const createFavicon = (color) => {
const canvas = document.createElement('canvas');
canvas.width = 32;
canvas.height = 32;
const ctx = canvas.getContext('2d');
if (ctx) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(16, 16, 14, 0, 2 * Math.PI);
ctx.fill();
}
return canvas.toDataURL('image/png');
};
const faviconColors = {
idle: '#06b6d4', excellent: '#22c55e', good: '#eab308',
poor: '#f97316', critical: '#dc2626', failed: '#2563eb',
};
const updateFavicon = (status, latestResult, criticalThreshold) => {
let color = faviconColors.idle;
if (status === 'running' && latestResult) {
if (latestResult.status === 'failed') {
color = faviconColors.failed;
} else {
const { rtt } = latestResult;
const excellent = criticalThreshold * 0.25;
const good = criticalThreshold * 0.50;
const poor = criticalThreshold * 1.0;
if (rtt <= excellent) color = faviconColors.excellent;
else if (rtt <= good) color = faviconColors.good;
else if (rtt <= poor) color = faviconColors.poor;
else color = faviconColors.critical;
}
}
const link = document.querySelector("link[rel*='icon']") || document.createElement('link');
link.type = 'image/png';
link.rel = 'shortcut icon';
link.href = createFavicon(color);
document.getElementsByTagName('head')[0].appendChild(link);
};
// --- From utils/exportUtils.ts ---
const getFileName = (settings, extension) =>
`netpulse_${settings.host}_${new Date().toISOString().replace(/:/g, '-')}.${extension}`;
const getAllResults = (results) => {
return Object.values(results).flat().filter((r) => r !== null);
};
const exportToCsv = (results, settings) => {
const allResults = getAllResults(results);
if (allResults.length === 0) return alert("No data to export.");
let csvContent = "data:text/csv;charset=utf-8,";
csvContent += "Timestamp,RTT (ms),Status\r\n";
allResults.forEach(res => {
const timestamp = new Date(res.timestamp).toISOString();
const row = `${timestamp},${res.rtt},${res.status}`;
csvContent += row + "\r\n";
});
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", getFileName(settings, 'csv'));
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
const exportToPng = async (element, settings) => {
if (!element) return alert("Could not find the element to capture.");
const animatedElements = element.querySelectorAll('.animate-pulse');
animatedElements.forEach(el => el.classList.remove('animate-pulse'));
const isDarkMode = document.documentElement.classList.contains('dark');
const backgroundColor = isDarkMode ? '#161b22' : '#ffffff';
try {
const canvas = await html2canvas(element, { backgroundColor, useCORS: true });
const image = canvas.toDataURL("image/png", 1.0);
const link = document.createElement('a');
link.download = getFileName(settings, 'png');
link.href = image;
link.click();
} catch (error) {
console.error("Failed to export as PNG:", error);
alert("An error occurred while exporting the image.");
} finally {
animatedElements.forEach(el => el.classList.add('animate-pulse'));
}
};
const exportToXlsx = (results, settings) => {
const allResults = getAllResults(results);
if (allResults.length === 0) return alert("No data to export.");
const dataForSheet = allResults.map(res => ({
Timestamp: new Date(res.timestamp).toISOString(),
'RTT (ms)': res.rtt,
Status: res.status
}));
const worksheet = XLSX.utils.json_to_sheet(dataForSheet);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Ping Results");
XLSX.writeFile(workbook, getFileName(settings, 'xlsx'));
};
const exportToPdf = async (element, settings) => {
if (!element) return alert("Could not find the element to capture.");
const animatedElements = element.querySelectorAll('.animate-pulse');
animatedElements.forEach(el => el.classList.remove('animate-pulse'));
const isDarkMode = document.documentElement.classList.contains('dark');
const backgroundColor = isDarkMode ? '#161b22' : '#ffffff';
try {
const canvas = await html2canvas(element, { backgroundColor, useCORS: true });
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = jspdf;
const pdf = new jsPDF({
orientation: 'landscape',
unit: 'px',
format: [canvas.width, canvas.height]
});
pdf.addImage(imgData, 'PNG', 0, 0, canvas.width, canvas.height);
pdf.save(getFileName(settings, 'pdf'));
} catch (error) {
console.error("Failed to export as PDF:", error);
alert("An error occurred while exporting the PDF.");
} finally {
animatedElements.forEach(el => el.classList.add('animate-pulse'));
}
};
// --- From contexts/TooltipContext.tsx ---
const TooltipContext = React.createContext(undefined);
const TooltipProvider = ({ children }) => {
const [activeTooltip, setActiveTooltip] = React.useState(null);
return (
<TooltipContext.Provider value={{ activeTooltip, setActiveTooltip }}>
{children}
</TooltipContext.Provider>
);
};
const useTooltip = () => {
const context = React.useContext(TooltipContext);
if (context === undefined) {
throw new Error('useTooltip must be used within a TooltipProvider');
}
return context;
};
// --- From components/Clock.tsx ---
const Clock = ({ status }) => {
const [now, setNow] = React.useState(new Date());
const [color, setColor] = React.useState('text-cyan-600 dark:text-cyan-400');
React.useEffect(() => {
const timerId = setInterval(() => setNow(new Date()), 1000);
return () => clearInterval(timerId);
}, []);
React.useEffect(() => {
switch (status) {
case 'Running':
setColor('text-green-500');
break;
case 'Stopped':
case 'Finished':
setColor('text-red-500 animate-pulse');
const timeoutId = setTimeout(() => {
setColor('text-cyan-600 dark:text-cyan-400');
}, 3000);
return () => clearTimeout(timeoutId);
case 'Idle':
default:
setColor('text-cyan-600 dark:text-cyan-400');
break;
}
}, [status]);
const dateString = now.toLocaleDateString(undefined, {
weekday: 'short', year: 'numeric', month: 'short', day: 'numeric'
});
return (
<div className="text-right">
<div className="text-xs text-cyan-600 dark:text-cyan-400 mb-1">{dateString}</div>
<div className={`text-lg font-bold transition-colors duration-300 ${color}`}>
{now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false })}
</div>
</div>
);
};
// --- From components/TimerDisplay.tsx ---
const padZero = (num) => num.toString().padStart(2, '0');
const formatSeconds = (totalSeconds) => {
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return `${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`;
};
const TimerDisplay = ({ status, startTime, durationMinutes }) => {
const [remaining, setRemaining] = React.useState(durationMinutes * 60);
const [eta, setEta] = React.useState(null);
React.useEffect(() => {
if (status === 'Running' && startTime) {
const durationMs = durationMinutes * 60 * 1000;
const endTime = startTime + durationMs;
setEta(new Date(endTime));
const intervalId = setInterval(() => {
const now = Date.now();
const newRemaining = Math.max(0, Math.round((endTime - now) / 1000));
setRemaining(newRemaining);
}, 1000);
return () => clearInterval(intervalId);
} else {
setRemaining(durationMinutes * 60);
setEta(null);
}
}, [status, startTime, durationMinutes]);
if (status !== 'Running') {
return null;
}
return (
<div className="text-xs text-gray-600 dark:text-gray-400 mt-1 space-y-1">
<div>
<span className="font-semibold text-gray-500">Remaining:</span> <span className="text-yellow-500 font-semibold">{formatSeconds(remaining)}</span>
</div>
{eta && (
<div>
<span className="font-semibold text-gray-500">End Time:</span> {eta.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false })}
</div>
)}
</div>
);
};
// --- From components/Legend.tsx --- (Sparkline Component)
const DetailedTooltipContent = ({ data, settings }) => {
const { result, timestamp } = data;
const date = new Date(timestamp);
const timeStr = date.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short' });
const dateStr = date.toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
const targetDisplay = () => {
const { host, resolvedHost } = settings;
if (!resolvedHost || resolvedHost.startsWith('No PTR')) return host;
const hostType = validateHostFormat(host).type;
if (hostType === 'domain' && resolvedHost.startsWith('Resolves to:')) return `${host} - ${resolvedHost.substring(12).trim()}`;
if ((hostType === 'ipv4' || hostType === 'ipv6') && resolvedHost.startsWith('PTR:')) return `${host} - ${resolvedHost.substring(5).trim()}`;
return host;
};
if (!result) {
return (
<div className="grid grid-cols-[auto_1fr] gap-x-2">
<span className="font-bold text-gray-500 dark:text-gray-400">Time:</span><span>{timeStr}</span>
<p className="col-span-2 text-yellow-500 mt-1">No data for this time.</p>
</div>
);
}
return (
<div className="grid grid-cols-[max-content_1fr] gap-x-3 gap-y-1">
<span className="font-bold text-gray-500 dark:text-gray-400 text-right">Date</span><span>: {dateStr}</span>
<span className="font-bold text-gray-500 dark:text-gray-400 text-right">Time</span><span>: {timeStr}</span>
<span className="font-bold text-gray-500 dark:text-gray-400 text-right">Target</span><span>: {targetDisplay()}</span>
<span className="font-bold text-gray-500 dark:text-gray-400 text-right">Latency</span>
<span>: {result.rtt} ms - <span className="font-semibold">{getLatencyDescription(result, settings.criticalThreshold)}</span></span>
<span className="font-bold text-gray-500 dark:text-gray-400 text-right">Threshold</span><span>: {settings.criticalThreshold} ms (Critical)</span>
</div>
);
};
const Tooltip = ({ data, settings, active, dataLength }) => {
const { result, timestamp, index } = data;
const positionStyle = { top: 0, transform: 'translateY(calc(-100% - 8px))', pointerEvents: 'none', position: 'absolute', zIndex: 10 };
const maxIndex = dataLength > 1 ? dataLength - 1 : 1;
if(index < maxIndex / 2) positionStyle.left = `${(index / maxIndex) * 100}%`;
else positionStyle.right = `${(1 - (index / maxIndex)) * 100}%`;
const timeFormatOptions = { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
if (settings.pingsPerSecond > 1) {
const advancedOptions = { ...timeFormatOptions, fractionalSecondDigits: 2 };
return <div style={positionStyle} className="p-3 bg-white dark:bg-gray-950 text-slate-800 dark:text-white text-xs rounded-md shadow-lg border border-slate-200 dark:border-gray-600 whitespace-nowrap">
{active ? <DetailedTooltipContent data={data} settings={settings} /> : <div className="text-center"><div className="font-bold">{new Date(timestamp).toLocaleTimeString([], advancedOptions)}</div>{result && <div>Latency: {result.status === 'success' ? `${result.rtt} ms` : 'Failed'}</div>}</div>}
</div>;
}
return <div style={positionStyle} className="p-3 bg-white dark:bg-gray-950 text-slate-800 dark:text-white text-xs rounded-md shadow-lg border border-slate-200 dark:border-gray-600 whitespace-nowrap">
{active ? <DetailedTooltipContent data={data} settings={settings} /> : <div className="text-center"><div className="font-bold">{new Date(timestamp).toLocaleTimeString([], timeFormatOptions)}</div>{result && <div>Latency: {result.status === 'success' ? `${result.rtt} ms` : 'Failed'}</div>}</div>}
</div>;
};
const Sparkline = ({ data, settings, minuteTimestamp }) => {
const [hoverData, setHoverData] = React.useState(null);
const svgRef = React.useRef(null);
const { activeTooltip, setActiveTooltip } = useTooltip();
const { criticalThreshold, pingsPerSecond } = settings;
const isActive = activeTooltip?.minuteTimestamp === minuteTimestamp;
const activeDataForThisRow = isActive ? activeTooltip : null;
const dataLength = 60 * pingsPerSecond;
if (data.filter(d => d !== null).length < 1 && !activeDataForThisRow) {
return <div className="w-full h-full bg-slate-200/50 dark:bg-gray-800/50 rounded-sm" />;
}
const successfulPings = data.map(d => d?.rtt).filter((rtt) => rtt !== null && rtt !== undefined);
const VIEWBOX_WIDTH = 120;
const VIEWBOX_HEIGHT = 20;
const maxRtt = Math.max(...successfulPings, criticalThreshold);
const colorMap = {
'bg-green-500': '#22c55e', 'bg-yellow-500': '#eab308', 'bg-orange-500': '#f97316',
'bg-red-600': '#dc2626', 'bg-blue-600': '#2563eb',
};
const getPoint = (result, index) => {
const maxIndex = dataLength > 1 ? dataLength - 1 : 1;
const x = (index / maxIndex) * VIEWBOX_WIDTH;
const rtt = result?.rtt ?? null;
const y = (rtt === null || result?.status === 'failed')
? VIEWBOX_HEIGHT
: VIEWBOX_HEIGHT - ((rtt / maxRtt) * (VIEWBOX_HEIGHT - 2) + 1);
return { x, y };
}
const handleMouseMove = (event) => {
if (!svgRef.current) return;
const rect = svgRef.current.getBoundingClientRect();
const x = event.clientX - rect.left;
const maxIndex = dataLength > 1 ? dataLength - 1 : 1;
const index = Math.min(maxIndex, Math.max(0, Math.round((x / rect.width) * maxIndex)));
const result = data[index];
const point = getPoint(result, index);
const timestamp = result?.timestamp || (new Date(minuteTimestamp).getTime() + index * (60000 / dataLength));
setHoverData({ index, result, x: point.x, y: point.y, timestamp });
};
const handleMouseLeave = () => setHoverData(null);
const handleClick = () => {
if (isActive && activeTooltip?.index === hoverData?.index) setActiveTooltip(null);
else if (hoverData) setActiveTooltip({ ...hoverData, minuteTimestamp });
};
const displayData = activeDataForThisRow || hoverData;
const indicatorColorName = getTimelineColor(displayData?.result ?? null, criticalThreshold);
const indicatorFill = colorMap[indicatorColorName] || '#6b7280';
return (
<div className="w-full h-full relative">
<svg ref={svgRef} width="100%" height="100%" viewBox={`0 0 ${VIEWBOX_WIDTH} ${VIEWBOX_HEIGHT}`} className="bg-slate-200/50 dark:bg-gray-800/50 rounded-sm cursor-crosshair" preserveAspectRatio="none" onMouseMove={handleMouseMove} onMouseLeave={handleMouseLeave} onClick={handleClick}>
{data.map((result, i) => {
const maxIndex = dataLength > 1 ? dataLength - 1 : 1;
if (i >= maxIndex) return null;
const nextResult = data[i+1];
if (result === null || nextResult === null) return null;
const p1 = getPoint(result, i);
const p2 = getPoint(nextResult, i + 1);
const colorName = getTimelineColor(result, criticalThreshold);
const stroke = colorMap[colorName] || '#6b7280';
return <line key={i} x1={p1.x.toFixed(2)} y1={p1.y.toFixed(2)} x2={p2.x.toFixed(2)} y2={p2.y.toFixed(2)} stroke={stroke} strokeWidth="1.5" />;
})}
{displayData && (displayData.result || activeDataForThisRow) && (
<g className="pointer-events-none">
<line x1={displayData.x} y1="0" x2={displayData.x} y2={VIEWBOX_HEIGHT} stroke="#a7a7a7" strokeWidth="0.5" strokeDasharray="2,2" />
{displayData.result && <circle cx={displayData.x} cy={displayData.y} r="1.5" fill={indicatorFill} stroke="white" strokeWidth="0.5" />}
</g>
)}
</svg>
{displayData && <Tooltip data={displayData} settings={settings} active={!!activeDataForThisRow} dataLength={dataLength} />}
</div>
);
};
// --- From components/StatsDisplay.tsx ---
const MinuteRow = ({ minuteTimestamp, results, settings }) => {
const timestamp = new Date(minuteTimestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false });
const [isHovered, setIsHovered] = React.useState(false);
const displayStats = calculateStats(results);
return (
<div
className={`flex items-center space-x-2 relative ${isHovered ? 'z-10' : 'z-0'}`}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<div className="w-16 text-gray-500">[{timestamp}]</div>
<div className="flex-1 h-5">
<Sparkline
data={results}
settings={settings}
minuteTimestamp={minuteTimestamp}
/>
</div>
<div className="w-32 hidden sm:flex items-center justify-around text-xs">
<span className="w-1/3 text-center text-green-500">{displayStats.min}ms</span>
<span className="w-1/3 text-center text-yellow-500 font-bold">{displayStats.avg}ms</span>
<span className="w-1/3 text-center text-red-500">{displayStats.max}ms</span>
</div>
</div>
);
};
// --- From components/TimelineDisplay.tsx ---
const Legend = ({ criticalThreshold }) => {
const excellent = Math.floor(criticalThreshold * 0.25);
const good = Math.floor(criticalThreshold * 0.50);
const poor = Math.floor(criticalThreshold * 1.00);
const tiers = [
{ desc: `Excellent ≤ ${excellent}ms`, colorClass: getTimelineColor({ rtt: excellent, status: 'success', timestamp: 0 }, criticalThreshold) },
{ desc: `Good ≤ ${good}ms`, colorClass: getTimelineColor({ rtt: good, status: 'success', timestamp: 0 }, criticalThreshold) },
{ desc: `Poor ≤ ${poor}ms`, colorClass: getTimelineColor({ rtt: poor, status: 'success', timestamp: 0 }, criticalThreshold) },
{ desc: `Very Poor > ${poor}ms`, colorClass: getTimelineColor({ rtt: poor + 1, status: 'success', timestamp: 0 }, criticalThreshold) },
{ desc: 'Failed', colorClass: getTimelineColor(null, criticalThreshold) },
];
return (
<div>
<h3 className="text-lg font-bold text-cyan-600 dark:text-cyan-400 mb-3">Legend</h3>
<div className="flex flex-wrap items-center gap-x-4 gap-y-2 text-sm">
{tiers.map(({ desc, colorClass }) => (
<div key={desc} className="flex items-center space-x-2">
<div className={`w-4 h-4 rounded-sm ${colorClass}`}></div>
<span className="text-gray-600 dark:text-gray-400">{desc}</span>
</div>
))}
</div>
</div>
);
}
const formatDuration = (totalSeconds) => {
if (totalSeconds < 60) return `${totalSeconds} seconds`;
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes} minute${minutes > 1 ? 's' : ''}${seconds > 0 ? ` and ${seconds} second${seconds > 1 ? 's' : ''}` : ''}`;
}
const Stat = ({label, value}) => (
<>
<dt className="text-gray-500 font-semibold">{label}</dt>
<dd className="text-slate-800 dark:text-gray-200">{value}</dd>
</>
);
const ExportButton = ({ resultsRef, results, settings }) => {
const [isOpen, setIsOpen] = React.useState(false);
const menuRef = React.useRef(null);
React.useEffect(() => {
const handleClickOutside = (event) => {
if (menuRef.current && !menuRef.current.contains(event.target)) {
setIsOpen(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, []);
const handleExport = (format) => {
switch(format) {
case 'png': exportToPng(resultsRef.current, settings); break;
case 'csv': exportToCsv(results, settings); break;
case 'xlsx': exportToXlsx(results, settings); break;
case 'pdf': exportToPdf(resultsRef.current, settings); break;
}
setIsOpen(false);
};
return (
<div className="relative inline-block text-left" ref={menuRef}>
<button onClick={() => setIsOpen(!isOpen)} className="text-xs bg-slate-200 dark:bg-gray-700 hover:bg-slate-300 dark:hover:bg-gray-600 text-slate-700 dark:text-gray-300 font-semibold py-1 px-4 rounded-md transition duration-200">
Export
</button>
{isOpen && (
<div className="origin-top-right absolute right-0 mt-2 w-32 rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 z-20">
<div className="py-1" role="menu" aria-orientation="vertical">
<a onClick={() => handleExport('png')} className="block px-4 py-2 text-sm text-slate-700 dark:text-gray-300 hover:bg-slate-100 dark:hover:bg-gray-700 cursor-pointer" role="menuitem">PNG</a>
<a onClick={() => handleExport('csv')} className="block px-4 py-2 text-sm text-slate-700 dark:text-gray-300 hover:bg-slate-100 dark:hover:bg-gray-700 cursor-pointer" role="menuitem">CSV</a>
<a onClick={() => handleExport('xlsx')} className="block px-4 py-2 text-sm text-slate-700 dark:text-gray-300 hover:bg-slate-100 dark:hover:bg-gray-700 cursor-pointer" role="menuitem">XLSX</a>
<a onClick={() => handleExport('pdf')} className="block px-4 py-2 text-sm text-slate-700 dark:text-gray-300 hover:bg-slate-100 dark:hover:bg-gray-700 cursor-pointer" role="menuitem">PDF</a>
</div>
</div>
)}
</div>
);
};
const NetworkIcon = ({ label, ip, hostname, status, tooltipText, children }) => {
const colorClasses = {
off: 'text-gray-400 dark:text-gray-600 border-gray-300 dark:border-gray-700',
on: 'text-cyan-600 dark:text-cyan-400 border-cyan-400 dark:border-cyan-700',
success: 'text-green-600 dark:text-green-400 border-green-400 dark:border-green-700',
};
const currentClasses = colorClasses[status] || colorClasses.off;
const renderTooltipText = (text) => {
return text.split('\n').map((line, i) => (
<p key={i} className={i > 0 ? 'mt-1.5' : ''}>{line}</p>
));
};
return (
<div className="text-center w-[7.5rem]">
<div className={`w-10 h-10 mx-auto bg-slate-100 dark:bg-gray-800 border rounded-full flex items-center justify-center transition-colors duration-500 ${currentClasses}`}>
{children}
</div>
<div
className={`mt-1 flex flex-col justify-start items-center transition-colors duration-500 ${status === 'off' ? 'text-gray-400 dark:text-gray-600' : 'text-gray-600 dark:text-gray-400'}`}
style={{ height: '5rem' }}
>
<div className="flex items-center justify-center gap-1 font-semibold">
<span>{label}</span>
{tooltipText && (
<div className="relative group flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" className="h-3.5 w-3.5 cursor-help text-gray-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M8.228 9c.549-1.165 2.03-2 3.772-2 2.21 0 4 1.343 4 3 0 1.4-1.278 2.575-3.006 2.907-.542.104-.994.54-.994 1.093m0 3h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<div className="absolute bottom-full mb-2 w-64 p-3 bg-gray-800 dark:bg-gray-950 text-white text-xs text-left rounded-md shadow-lg border border-gray-600 opacity-0 group-hover:opacity-100 transition-opacity duration-300 pointer-events-none z-10 -translate-x-1/2 left-1/2">
{renderTooltipText(tooltipText)}
</div>
</div>
)}
</div>
<div className="text-xs text-slate-700 dark:text-gray-300 break-words w-full px-1">{ip || <> </>}</div>
<div className="text-xs text-gray-500 break-words w-full px-1">{hostname || <> </>}</div>
</div>
</div>
);
};
const NetworkLine = ({ outbound, inbound }) => {
const colorMap = {
cyan: "from-transparent via-cyan-500 to-transparent",
green: "from-transparent via-green-500 to-transparent",
yellow: "from-transparent via-yellow-500 to-transparent",
orange: "from-transparent via-orange-500 to-transparent",
red: "from-transparent via-red-500 to-transparent",
blue: "from-transparent via-blue-600 to-transparent",
};
const getLineClasses = (isAnimating, animationClass, color) => {
const base = "w-full h-[4px] transition-all duration-500";
if (!isAnimating) {
return `${base} bg-slate-300 dark:bg-gray-700`;
}
const gradient = colorMap[color] || colorMap.cyan;
return `${base} bg-[length:200%_100%] bg-gradient-to-r ${gradient} ${animationClass}`;
};
const outboundClasses = getLineClasses(outbound.animating, 'animate-bg-scroll', outbound.color);
const inboundClasses = getLineClasses(inbound.animating, 'animate-bg-scroll-reverse', inbound.color);
return (
<div className="h-10 flex flex-col justify-center items-center space-y-2 w-full">
<div className={outboundClasses} />
<div className={inboundClasses} />
</div>
);
};
const getTargetDisplayInfo = (settings) => {
const { host, resolvedHost } = settings;
if (!host) {
return { ip: null, hostname: null };
}
const { type: hostType } = validateHostFormat(host);
if (!resolvedHost || resolvedHost === '' || resolvedHost.includes('failed') || resolvedHost.includes('not found')) {
if(hostType === 'domain') return { ip: null, hostname: host };
return { ip: host, hostname: null };
}
if (hostType === 'domain') {
if (resolvedHost.startsWith('Resolves to: ')) {
const ip = resolvedHost.substring('Resolves to: '.length).trim();
return { ip: ip, hostname: host };
}
return { ip: null, hostname: host };
}
if (hostType === 'ipv4' || hostType === 'ipv6') {
const ip = host;
if (resolvedHost.startsWith('PTR: ')) {
const ptr = resolvedHost.substring('PTR: '.length).trim();
return { ip: ip, hostname: ptr };
}
return { ip: ip, hostname: null };
}
return { ip: host, hostname: null }; // Fallback
};
const getSimpleColorFromRtt = (result, threshold) => {
if (!result || result.status === 'failed') return 'blue';
const { rtt } = result;
const excellent = threshold * 0.25;
const good = threshold * 0.50;
const poor = threshold * 1.0;
if (rtt <= excellent) return 'green';
if (rtt <= good) return 'yellow';
if (rtt <= poor) return 'orange';
return 'red';
}
const NetworkPath = (props) => {
const { settings, status, pingResults, localIp, externalIp, externalIpHostname, internetReachable, targetReachable } = props;
const youStatus = internetReachable ? 'on' : 'off';
const internetStatus = internetReachable ? 'on' : 'off';
const targetStatus = targetReachable === true ? 'success' : 'off';
const isPinging = status === 'Running';
const isPingingSuccessfully = isPinging && targetReachable === true;
const allResults = Object.values(pingResults).flat().filter((r) => r !== null);
const latestResult = allResults.length > 0 ? allResults[allResults.length - 1] : null;
const inboundPingColor = getSimpleColorFromRtt(latestResult, settings.criticalThreshold);
const idleLineState = {
outbound: { animating: internetReachable, color: 'cyan' },
inbound: { animating: internetReachable, color: 'cyan' },
};
const pingingLineState = {
outbound: { animating: isPinging, color: 'cyan' },
inbound: { animating: isPingingSuccessfully, color: inboundPingColor },
};
const youTooltip = `Your local IP is found via WebRTC, which can be blocked by browser privacy settings, extensions, or VPNs.\nThe application will function correctly even if the IP isn't displayed.`;
const parsePtrRecord = (ptrResult) => {
if (!ptrResult || !ptrResult.startsWith('PTR: ')) {
return null;
}
return ptrResult.substring('PTR: '.length).trim();
};
const targetDisplay = getTargetDisplayInfo(settings);
const internetHostname = parsePtrRecord(externalIpHostname);
return (
<div className="my-6 flex items-start justify-between text-xs w-full">
<NetworkIcon label="You" ip={localIp} status={youStatus} tooltipText={youTooltip}>
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5.121 17.804A13.937 13.937 0 0112 16c2.5 0 4.847.655 6.879 1.804M15 10a3 3 0 11-6 0 3 3 0 016 0zm6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
</NetworkIcon>
<NetworkLine {...(isPinging ? pingingLineState : idleLineState)} />
<NetworkIcon label="Gateway" status={internetStatus}>
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8.111 16.404a5.5 5.5 0 017.778 0M12 20h.01m-7.08-7.071a7.5 7.5 0 0110.607 0M12 6h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
</NetworkIcon>
<NetworkLine {...(isPinging ? pingingLineState : idleLineState)} />
<NetworkIcon label="Internet" ip={externalIp} hostname={internetHostname} status={internetStatus}>
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 15a4 4 0 004 4h9a5 5 0 10-.1-9.999 5.002 5.002 0 10-9.78 2.096A4.001 4.001 0 003 15z" />
</svg>
</NetworkIcon>
<NetworkLine {...pingingLineState} />
<NetworkIcon label="Target" ip={targetDisplay.ip} hostname={targetDisplay.hostname} status={targetStatus}>
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 12h14M5 12a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v4a2 2 0 01-2-2M5 12a2 2 0 00-2 2v4a2 2 0 002 2h14a2 2 0 002-2v-4a2 2 0 00-2-2m-2-4h.01M17 16h.01" />
</svg>
</NetworkIcon>
</div>
);
};
const ResultsDisplay = (props) => {
const { settings, status, pingResults, summaryStats, startTime } = props;
const resultsPanelRef = React.useRef(null);
const minuteKeys = Object.keys(pingResults).sort();
const hasResults = minuteKeys.length > 0;
const showWelcome = status === 'Idle' && !hasResults && !startTime;
const isFinished = status === 'Stopped' || status === 'Finished';
const formatTimestamp = (isoString, otherIsoString) => {
const date = new Date(isoString);
const otherDate = new Date(otherIsoString);
const options = {
hour: 'numeric', minute: '2-digit', second: '2-digit'
};
if (date.toDateString() !== otherDate.toDateString()) {
options.year = 'numeric';
options.month = 'short';
options.day = 'numeric';
}
return date.toLocaleString(undefined, options);
};
const getStatusMessage = () => {
if (status === 'Running') {
const baseMsg = `Monitoring from ${props.localIp || 'You'} to ${settings.host}...`;
if (props.targetReachable === 'unknown') {
return <p className="text-yellow-500 animate-pulse">{baseMsg}</p>
}
return <p className="text-green-500 animate-pulse">{baseMsg}</p>
}
if (showWelcome) {
return <p className="text-gray-500">Configure settings and press "Start Monitoring" to begin.</p>
}
return <p className="text-cyan-600 dark:text-cyan-400">Monitoring session to {settings.host} {isFinished ? 'concluded' : status.toLowerCase()}.</p>
};
return (
<div ref={resultsPanelRef} className="bg-white dark:bg-gray-900 p-4 sm:p-6 rounded-lg shadow-lg border border-slate-200 dark:border-gray-700 text-sm">
<div className="pb-4 border-b border-slate-200 dark:border-gray-700 mb-4 flex justify-between items-start">
<div className="flex-1 pr-4">{getStatusMessage()}</div>
<div className="text-right">
<Clock status={status} />
<TimerDisplay status={status} startTime={startTime} durationMinutes={settings.duration} />
</div>
</div>
<NetworkPath {...props} />
{(status === 'Running' || hasResults) && (
<div className="mt-6 pt-4 border-t border-slate-200 dark:border-gray-700">
<div className="hidden sm:flex items-center text-xs text-gray-500 mb-2">
<div className="w-16"></div>
<div className="flex-1 text-center">Latency Graph</div>
<div className="w-32 text-center">Min | Avg | Max</div>
</div>
<div className="relative space-y-1">
{minuteKeys.map((minuteTimestamp) => (
<MinuteRow
key={minuteTimestamp}
minuteTimestamp={minuteTimestamp}
results={pingResults[minuteTimestamp]}
settings={settings}
/>
))}
</div>
</div>
)}
{(hasResults || isFinished) && (
<div className="mt-6 pt-4 border-t border-slate-200 dark:border-gray-700">
{hasResults && <Legend key={settings.criticalThreshold} criticalThreshold={settings.criticalThreshold} />}
{isFinished && (
<div className={hasResults ? "mt-6" : ""}>
<div className="flex justify-between items-center mb-3">
<h3 className="text-lg font-bold text-cyan-600 dark:text-cyan-400">Monitoring Statistics for {settings.host}</h3>
{hasResults && <ExportButton resultsRef={resultsPanelRef} results={pingResults} settings={settings} />}
</div>
{summaryStats && summaryStats.sent > 0 ? (
<dl className="grid grid-cols-[max-content_1fr] gap-x-4 gap-y-1.5 text-sm">
<Stat label="Start Time" value={formatTimestamp(summaryStats.startTime, summaryStats.stopTime)} />
<Stat label="Stop Time" value={formatTimestamp(summaryStats.stopTime, summaryStats.startTime)} />
<Stat label="Duration" value={formatDuration(summaryStats.totalDuration)} />
<Stat label="Packets" value={
<div className="flex flex-wrap items-center gap-x-4">
<span><span className="font-bold text-cyan-600 dark:text-cyan-400">↑Sent:</span> {summaryStats.sent}</span>
<span><span className="font-bold text-green-500">↓Rcvd:</span> {summaryStats.received}</span>
<span><span className="font-bold text-red-500">øLost:</span> {summaryStats.sent - summaryStats.received} ({summaryStats.loss.toFixed(2)}%)</span>
</div>
} />
<Stat label="Latency" value={
<div className="flex flex-wrap items-center gap-x-4">
<span><span className="font-bold text-green-500">Min:</span> {summaryStats.min}ms</span>
<span><span className="font-bold text-yellow-500">Avg:</span> {summaryStats.avg}ms</span>
<span><span className="font-bold text-red-500">Max:</span> {summaryStats.max}ms</span>
</div>
} />
</dl>
) : (
<p className="text-gray-500">Session ended. No data was recorded.</p>
)}
</div>
)}
</div>
)}
</div>
);
};
// --- From components/SettingsPanel.tsx ---
const SettingsPanel = ({ settings, setSettings, onStart, onStop, status }) => {
const isRunning = status === 'Running';
const [hostInput, setHostInput] = React.useState(settings.host);
const [hostError, setHostError] = React.useState(null);
const [resolutionInfo, setResolutionInfo] = React.useState(settings.resolvedHost || null);
const [isResolving, setIsResolving] = React.useState(false);
const [durationH, setDurationH] = React.useState(Math.floor(settings.duration / 60));
const [durationM, setDurationM] = React.useState(settings.duration % 60);
React.useEffect(() => {
if (settings.saveSettings) {
localStorage.setItem('netPulseSettings', JSON.stringify(settings));
} else {
localStorage.removeItem('netPulseSettings');
}
}, [settings]);
React.useEffect(() => {
setHostInput(settings.host);
setResolutionInfo(settings.resolvedHost || null);
setDurationH(Math.floor(settings.duration / 60));
setDurationM(settings.duration % 60);
}, [settings]);
const handleSettingsChange = (e) => {
const { name, value, type } = e.target;
if (type === 'checkbox') {
setSettings(prev => ({ ...prev, [name]: e.target.checked }));
} else {
setSettings(prev => ({
...prev,
[name]: parseInt(value, 10),
}));
}
};
const handleDurationChange = (e) => {
const { name, value } = e.target;
const numValue = parseInt(value, 10) || 0;
let newHours = durationH;
let newMinutes = durationM;
if (name === 'durationH') {
newHours = numValue;
setDurationH(newHours);
} else if (name === 'durationM') {
newMinutes = numValue;
setDurationM(newMinutes);
}
const totalMinutes = (newHours * 60) + newMinutes;
setSettings(prev => ({ ...prev, duration: totalMinutes }));
}
const handleHostResolution = async () => {
if (!hostInput || hostInput === settings.host) {
setIsResolving(false);
return;
}
const { isValid } = validateHostFormat(hostInput);
if (!isValid) {
setHostError('Invalid format. Use a domain, IPv4, or IPv6 address.');
return;
}
setIsResolving(true);
setHostError(null);
setResolutionInfo(null);
const { result, error } = await resolveHost(hostInput);
if (error) {
setHostError(error);
setSettings(prev => ({ ...prev, host: hostInput, resolvedHost: '' }));
} else {
setResolutionInfo(result);
setSettings(prev => ({ ...prev, host: hostInput, resolvedHost: result || '' }));
}
setIsResolving(false);
};
const canStart = !isRunning && !hostError && !isResolving && hostInput.length > 0 && settings.duration > 0;
return (
<div className="bg-white dark:bg-gray-900 p-6 rounded-lg shadow-lg border border-slate-200 dark:border-gray-700">
<h2 className="text-xl font-bold text-cyan-600 dark:text-cyan-400 mb-4">Configuration</h2>
<div className="space-y-4">
<div>
<label htmlFor="host" className="block text-sm font-medium text-gray-600 dark:text-gray-400">Target Host</label>
<input
type="text"
id="host"
name="host"