forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkStatus.tsx
More file actions
393 lines (352 loc) · 13.8 KB
/
Copy pathNetworkStatus.tsx
File metadata and controls
393 lines (352 loc) · 13.8 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
'use client';
import { useEffect, useRef, useState } from 'react';
import { AlertTriangle, CheckCircle2, Clock3, WifiOff, Settings, X } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { useNetwork } from '@/context/NetworkContext';
import { validateUrl, defaultNetworkConfig } from '@/services/rpc';
const RPC_HEALTH_REQUEST_BODY = '{"jsonrpc":"2.0","id":1,"method":"getHealth"}';
export const HEARTBEAT_INTERVAL_MS = 10_000;
export const DEGRADED_LATENCY_MS = 1_000;
export const RPC_REQUEST_TIMEOUT_MS = 8_000;
export type NetworkHealthStatus = 'healthy' | 'degraded' | 'offline';
export interface RpcHealthSnapshot {
status: NetworkHealthStatus;
latencyMs: number | null;
message: string;
}
interface RpcHealthResponse {
result?: {
status?: string;
};
error?: unknown;
}
function describeRpcError(error: unknown): string {
if (typeof error === 'object' && error !== null && 'message' in error) {
const message = (error as { message?: unknown }).message;
if (typeof message === 'string' && message.trim()) {
return message;
}
}
return 'RPC getHealth returned an error';
}
interface NetworkStatusProps {
endpoint?: string;
fetcher?: typeof fetch;
now?: () => number;
}
const INITIAL_SNAPSHOT: RpcHealthSnapshot = {
status: 'degraded',
latencyMs: null,
message: 'Checking RPC health...',
};
const STATUS_STYLES = {
healthy: {
labelKey: 'network.healthy',
icon: CheckCircle2,
badge: 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-800 dark:bg-emerald-900/30 dark:text-emerald-400',
dot: 'bg-emerald-500',
},
degraded: {
labelKey: 'network.degraded',
icon: Clock3,
badge: 'border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-800 dark:bg-amber-900/30 dark:text-amber-400',
dot: 'bg-amber-500',
},
offline: {
labelKey: 'network.offline',
icon: WifiOff,
badge: 'border-red-200 bg-red-50 text-red-700 dark:border-red-800 dark:bg-red-900/30 dark:text-red-400',
dot: 'bg-red-500',
},
} as const;
function translateNetworkMessage(
message: string,
t: ReturnType<typeof useTranslation>['t'],
): string {
if (message === INITIAL_SNAPSHOT.message) {
return t('network.initialMessage');
}
if (message === 'RPC getHealth is responding normally') {
return t('network.normal');
}
if (message === 'RPC returned an invalid health response. Switch RPC endpoints if this continues.') {
return t('network.invalidJson');
}
if (message === 'RPC responded slowly. Network requests may take longer than expected.') {
return t('network.slow');
}
if (message === 'RPC health status unavailable. Switch RPC endpoints if this continues.') {
return t('network.unavailable');
}
if (message.startsWith('RPC returned HTTP ')) {
const status = message.match(/RPC returned HTTP (\d+)/)?.[1] ?? '';
return t('network.httpError', { status });
}
if (message.startsWith('RPC unreachable at ')) {
const endpoint = message
.replace('RPC unreachable at ', '')
.replace('. Check your network connection or switch RPC endpoints.', '');
return t('network.unreachable', { endpoint });
}
if (message.startsWith('RPC getHealth error: ')) {
return t('network.healthError', { message: message.replace('RPC getHealth error: ', '') });
}
if (message.startsWith('RPC reported ')) {
return t('network.reportedStatus', { status: message.replace('RPC reported ', '') });
}
return message;
}
export async function fetchRpcHealth(
endpoint: string,
fetcher: typeof fetch = fetch,
now: () => number = Date.now
): Promise<RpcHealthSnapshot> {
const startedAt = now();
const abortController = new AbortController();
const timeoutId = setTimeout(() => {
abortController.abort();
}, RPC_REQUEST_TIMEOUT_MS);
try {
const response = await fetcher(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: RPC_HEALTH_REQUEST_BODY,
cache: 'no-store',
signal: abortController.signal,
});
const latencyMs = Math.max(0, Math.round(now() - startedAt));
if (!response.ok) {
return {
status: 'offline',
latencyMs,
message: `RPC returned HTTP ${response.status}. Network requests may fail; try again or switch RPC endpoints.`,
};
}
let payload: RpcHealthResponse;
try {
payload = (await response.json()) as RpcHealthResponse;
} catch (error) {
if (abortController.signal.aborted) {
console.error('Stellar RPC getHealth request failed', { endpoint, error });
return {
status: 'offline',
latencyMs: null,
message: `RPC unreachable at ${endpoint}. Check your network connection or switch RPC endpoints.`,
};
}
console.error('Stellar RPC getHealth returned invalid JSON', { endpoint, error });
return {
status: 'degraded',
latencyMs,
message: 'RPC returned an invalid health response. Switch RPC endpoints if this continues.',
};
}
const reportedStatus = payload.result?.status;
if (payload.error) {
return {
status: 'degraded',
latencyMs,
message: `RPC getHealth error: ${describeRpcError(payload.error)}`,
};
}
if (reportedStatus !== 'healthy') {
return {
status: 'degraded',
latencyMs,
message: reportedStatus ? `RPC reported ${reportedStatus}` : 'RPC health status unavailable. Switch RPC endpoints if this continues.',
};
}
if (latencyMs >= DEGRADED_LATENCY_MS) {
return {
status: 'degraded',
latencyMs,
message: 'RPC responded slowly. Network requests may take longer than expected.',
};
}
return {
status: 'healthy',
latencyMs,
message: 'RPC getHealth is responding normally',
};
} catch (error) {
console.error('Stellar RPC getHealth request failed', { endpoint, error });
return {
status: 'offline',
latencyMs: null,
message: `RPC unreachable at ${endpoint}. Check your network connection or switch RPC endpoints.`,
};
} finally {
clearTimeout(timeoutId);
}
}
export default function NetworkStatus({
endpoint,
fetcher = fetch,
now = Date.now,
}: NetworkStatusProps) {
const { t } = useTranslation();
const { networkConfig, setHorizonUrl, setSorobanRpcUrl, setNetworkPassphrase, resetToDefaults } = useNetwork();
const [showSettings, setShowSettings] = useState(false);
const [localHorizonUrl, setLocalHorizonUrl] = useState(networkConfig.horizonUrl);
const [localSorobanRpcUrl, setLocalSorobanRpcUrl] = useState(networkConfig.sorobanRpcUrl);
const [localNetworkPassphrase, setLocalNetworkPassphrase] = useState(
networkConfig.networkPassphrase
);
const rpcEndpoint = endpoint ?? networkConfig.sorobanRpcUrl;
const [snapshot, setSnapshot] = useState<RpcHealthSnapshot>(INITIAL_SNAPSHOT);
const latestCheckId = useRef(0);
const statusStyle = STATUS_STYLES[snapshot.status];
const StatusIcon = statusStyle.icon;
useEffect(() => {
let isMounted = true;
async function checkHealth() {
const checkId = latestCheckId.current + 1;
latestCheckId.current = checkId;
const nextSnapshot = await fetchRpcHealth(rpcEndpoint, fetcher, now);
if (!isMounted || checkId !== latestCheckId.current) {
return;
}
setSnapshot((currentSnapshot) => {
const sameStatusAndMessage =
currentSnapshot.status === nextSnapshot.status &&
currentSnapshot.message === nextSnapshot.message;
const sameVisibleState =
sameStatusAndMessage &&
(nextSnapshot.status === 'offline' || currentSnapshot.latencyMs === nextSnapshot.latencyMs);
return sameVisibleState ? currentSnapshot : nextSnapshot;
});
}
void checkHealth();
const intervalId = window.setInterval(() => {
void checkHealth();
}, HEARTBEAT_INTERVAL_MS);
return () => {
isMounted = false;
window.clearInterval(intervalId);
};
}, [fetcher, now, rpcEndpoint]);
useEffect(() => {
setLocalHorizonUrl(networkConfig.horizonUrl);
setLocalSorobanRpcUrl(networkConfig.sorobanRpcUrl);
setLocalNetworkPassphrase(networkConfig.networkPassphrase);
}, [networkConfig]);
const handleSaveSettings = () => {
if (validateUrl(localHorizonUrl)) {
setHorizonUrl(localHorizonUrl);
}
if (validateUrl(localSorobanRpcUrl)) {
setSorobanRpcUrl(localSorobanRpcUrl);
}
setNetworkPassphrase(localNetworkPassphrase);
setShowSettings(false);
};
const handleReset = () => {
resetToDefaults();
setShowSettings(false);
};
return (
<section
aria-label={t('network.ariaLabel')}
aria-live="polite"
role={snapshot.status === 'offline' ? 'alert' : 'status'}
className="w-full rounded-xl border border-slate-200 bg-slate-50/80 p-4 text-sm shadow-sm dark:border-slate-800 dark:bg-slate-900/60 md:max-w-2xl"
>
<div className="flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between">
<div className="min-w-0 space-y-2">
<div className="flex items-center gap-2">
<AlertTriangle className="h-4 w-4 text-slate-500 dark:text-slate-400" aria-hidden="true" />
<h2 className="font-semibold text-slate-900 dark:text-white">{t('network.heading')}</h2>
</div>
<p className="font-mono text-xs text-slate-600 break-all dark:text-slate-400">{rpcEndpoint}</p>
<p className="text-xs text-slate-600 dark:text-slate-400">{translateNetworkMessage(snapshot.message, t)}</p>
</div>
<div className="flex flex-wrap items-center gap-3 sm:justify-end">
<span className={`inline-flex items-center gap-2 rounded-full border px-3 py-1 font-medium ${statusStyle.badge}`}>
<span data-testid="rpc-status-dot" className={`h-2 w-2 rounded-full ${statusStyle.dot}`} aria-hidden="true" />
<StatusIcon className="h-4 w-4" aria-hidden="true" />
{t(statusStyle.labelKey)}
</span>
<span className="rounded-full border border-slate-200 bg-white px-3 py-1 font-mono text-xs text-slate-700 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-300">
{snapshot.latencyMs === null ? t('network.latencyEmpty') : t('network.latency', { latency: snapshot.latencyMs })}
</span>
<button
type="button"
onClick={() => setShowSettings(!showSettings)}
className="rounded-full border border-slate-200 bg-white px-3 py-1 text-xs font-medium text-slate-700 hover:bg-slate-100 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-300 dark:hover:bg-slate-800"
>
<Settings className="h-4 w-4 inline-block mr-1" />
Settings
</button>
</div>
</div>
{showSettings && (
<div className="mt-4 border-t border-slate-200 pt-4 dark:border-slate-700">
<div className="flex items-center justify-between mb-3">
<h3 className="font-semibold text-slate-900 dark:text-white">Network Settings</h3>
<button
type="button"
onClick={() => setShowSettings(false)}
className="text-slate-500 hover:text-slate-700 dark:text-slate-400 dark:hover:text-slate-200"
>
<X className="h-4 w-4" />
</button>
</div>
<div className="space-y-3">
<div>
<label className="block text-xs font-medium text-slate-700 dark:text-slate-300 mb-1">
Horizon URL
</label>
<input
type="text"
value={localHorizonUrl}
onChange={(e) => setLocalHorizonUrl(e.target.value)}
className="w-full rounded-lg border border-slate-300 px-3 py-2 text-xs bg-white dark:bg-slate-900 dark:border-slate-700 dark:text-white"
placeholder={defaultNetworkConfig.horizonUrl}
/>
</div>
<div>
<label className="block text-xs font-medium text-slate-700 dark:text-slate-300 mb-1">
Soroban RPC URL
</label>
<input
type="text"
value={localSorobanRpcUrl}
onChange={(e) => setLocalSorobanRpcUrl(e.target.value)}
className="w-full rounded-lg border border-slate-300 px-3 py-2 text-xs bg-white dark:bg-slate-900 dark:border-slate-700 dark:text-white"
placeholder={defaultNetworkConfig.sorobanRpcUrl}
/>
</div>
<div>
<label className="block text-xs font-medium text-slate-700 dark:text-slate-300 mb-1">
Network Passphrase
</label>
<input
type="text"
value={localNetworkPassphrase}
onChange={(e) => setLocalNetworkPassphrase(e.target.value)}
className="w-full rounded-lg border border-slate-300 px-3 py-2 text-xs bg-white dark:bg-slate-900 dark:border-slate-700 dark:text-white"
placeholder={defaultNetworkConfig.networkPassphrase}
/>
</div>
<div className="flex gap-2">
<button
type="button"
onClick={handleSaveSettings}
className="rounded-lg bg-sky-600 px-4 py-2 text-xs font-semibold text-white hover:bg-sky-700"
>
Save
</button>
<button
type="button"
onClick={handleReset}
className="rounded-lg border border-slate-300 px-4 py-2 text-xs font-semibold text-slate-700 hover:bg-slate-100 dark:border-slate-700 dark:text-slate-300 dark:hover:bg-slate-800"
>
Reset to Defaults
</button>
</div>
</div>
</div>
)}
</section>
);
}