MCPcopy Create free account
hub / github.com/TheCSir/TerminalStart / StatsWidget

Function StatsWidget

src/components/widgets/StatsWidget.tsx:28–227  ·  view source on GitHub ↗
({ mode = 'text' })

Source from the content-addressed store, hash-verified

26};
27
28export const StatsWidget: React.FC<StatsWidgetProps> = ({ mode = 'text' }) => {
29 const [ping, setPing] = useState(0);
30 const [isOnline, setIsOnline] = useState(navigator.onLine);
31 const [memory, setMemory] = useState(getMemoryMB);
32 const [uptime, setUptime] = useState('00:00:00');
33 const mountTime = useRef(Date.now());
34 const pingRef = useRef(0);
35
36 // History for graphs (last 30 points)
37 const HISTORY_LENGTH = 30;
38 const [history, setHistory] = useState({
39 ping: new Array(HISTORY_LENGTH).fill(0),
40 memory: new Array(HISTORY_LENGTH).fill(0),
41 });
42
43 // Online/offline listener
44 useEffect(() => {
45 const handleOnline = () => setIsOnline(true);
46 const handleOffline = () => setIsOnline(false);
47 window.addEventListener('online', handleOnline);
48 window.addEventListener('offline', handleOffline);
49 return () => {
50 window.removeEventListener('online', handleOnline);
51 window.removeEventListener('offline', handleOffline);
52 };
53 }, []);
54
55 // Ping measurement (existing logic)
56 useEffect(() => {
57 const measurePing = async () => {
58 const start = performance.now();
59 try {
60 if (!navigator.onLine) {
61 throw new Error('Offline');
62 }
63 await fetch(`https://www.google.com?_=${Date.now()}`, {
64 method: 'HEAD',
65 mode: 'no-cors',
66 cache: 'no-store',
67 });
68 const end = performance.now();
69 const latency = Math.round(end - start);
70 setPing(latency);
71 pingRef.current = latency;
72 } catch {
73 setPing(0);
74 pingRef.current = 0;
75 }
76 };
77
78 measurePing();
79 const interval = setInterval(measurePing, 2000);
80 return () => clearInterval(interval);
81 }, []);
82
83 // Memory + history polling (every 2s)
84 useEffect(() => {
85 const poll = () => {

Callers

nothing calls this directly

Calls 3

measurePingFunction · 0.85
pollFunction · 0.85
formatUptimeFunction · 0.85

Tested by

no test coverage detected