forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkMismatchBanner.tsx
More file actions
104 lines (92 loc) · 2.86 KB
/
Copy pathNetworkMismatchBanner.tsx
File metadata and controls
104 lines (92 loc) · 2.86 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
import { useEffect, useMemo, useRef, useState } from 'react';
import './NetworkMismatchBanner.css';
interface NetworkMismatchBannerProps {
currentNetwork: string | null;
expectedNetwork: string;
walletType: 'freighter' | 'albedo' | 'xbull' | 'rabet';
onSwitchNetwork: () => Promise<void> | void;
}
const NETWORK_LABELS: Record<string, string> = {
PUBLIC: 'Public network',
TESTNET: 'Testnet',
};
const WALLET_SWITCH_COPY: Record<string, string> = {
freighter: 'Switch network in Freighter',
albedo: 'Switch network in Albedo',
xbull: 'Switch network in xBull',
rabet: 'Switch network in Rabet',
};
export const NetworkMismatchBanner = ({
currentNetwork,
expectedNetwork,
walletType,
onSwitchNetwork,
}: NetworkMismatchBannerProps) => {
const [isSwitching, setIsSwitching] = useState(false);
const [switchResult, setSwitchResult] = useState<'success' | 'error' | null>(null);
const liveRef = useRef<HTMLSpanElement>(null);
const shouldRender = useMemo(() => {
if (!currentNetwork) return false;
return currentNetwork !== expectedNetwork;
}, [currentNetwork, expectedNetwork]);
useEffect(() => {
if (!shouldRender) {
setIsSwitching(false);
setSwitchResult(null);
}
}, [shouldRender]);
if (!shouldRender) {
return null;
}
const currentLabel = NETWORK_LABELS[currentNetwork!] ?? currentNetwork;
const expectedLabel = NETWORK_LABELS[expectedNetwork] ?? expectedNetwork;
const descriptionId = 'network-mismatch-desc';
const handleClick = async () => {
setIsSwitching(true);
setSwitchResult(null);
try {
await onSwitchNetwork();
setSwitchResult('success');
} catch {
setSwitchResult('error');
} finally {
setIsSwitching(false);
}
};
return (
<div
className="network-mismatch-banner"
role="status"
aria-live="polite"
aria-atomic="true"
>
<div className="network-mismatch-banner__content">
<p className="network-mismatch-banner__title">
Your wallet network doesn't match the app.
</p>
<p id={descriptionId} className="network-mismatch-banner__copy">
Current network: {currentLabel}. Expected: {expectedLabel}.
</p>
</div>
<button
type="button"
className="network-mismatch-banner__button"
onClick={handleClick}
disabled={isSwitching}
aria-describedby={descriptionId}
>
{isSwitching ? 'Switching…' : WALLET_SWITCH_COPY[walletType]}
</button>
<span
ref={liveRef}
role="log"
aria-live="polite"
aria-atomic="true"
className="sr-only"
>
{switchResult === 'success' && `Network switched to ${expectedLabel}.`}
{switchResult === 'error' && `Failed to switch network. Please switch manually in your ${walletType} wallet.`}
</span>
</div>
);
};