-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathWalletButton.tsx
More file actions
155 lines (147 loc) · 3.74 KB
/
WalletButton.tsx
File metadata and controls
155 lines (147 loc) · 3.74 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
import {
Button,
Text,
Modal,
Profile,
Icon,
Tooltip,
} from "@stellar/design-system"
import { useState } from "react"
import { useWallet } from "../hooks/useWallet"
import { connectWallet, disconnectWallet } from "../util/wallet"
export const WalletButton = () => {
const [showDisconnectModal, setShowDisconnectModal] = useState(false)
const [isWarningTooltipVisible, setIsWarningTooltipVisible] = useState(false)
const { address, isPending, balances, walletWarnings } = useWallet()
const buttonLabel = isPending ? "Loading..." : "Connect"
// Build warning message based on wallet issues
const getWarningMessage = () => {
const warnings: string[] = []
if (walletWarnings.popupAlways) {
warnings.push("This wallet triggers a popup on every interaction")
}
if (walletWarnings.noGetNetworkSupport) {
warnings.push("This wallet doesn't support network detection")
}
return warnings.join(". ")
}
// Handle click on warning icon - open help URL if available
const handleWarningClick = () => {
if (walletWarnings.helpUrl) {
window.open(walletWarnings.helpUrl, "_blank", "noopener,noreferrer")
}
}
if (!address) {
return (
<Button variant="primary" size="md" onClick={() => void connectWallet()}>
{buttonLabel}
</Button>
)
}
return (
<div
style={{
display: "flex",
flexDirection: "row",
alignItems: "center",
gap: "5px",
opacity: isPending ? 0.6 : 1,
}}
>
<Text as="div" size="sm">
Wallet Balance: {balances?.xlm?.balance ?? "-"} XLM
</Text>
<div id="modalContainer">
<Modal
visible={showDisconnectModal}
onClose={() => setShowDisconnectModal(false)}
parentId="modalContainer"
>
<Modal.Heading>Disconnect wallet?</Modal.Heading>
<Modal.Body>
<Text as="p" size="sm">
Connected as{" "}
<code>{`${address.slice(0, 6)}...${address.slice(-6)}`}</code>
</Text>
</Modal.Body>
<Modal.Footer itemAlignment="stack">
<Button
size="md"
variant="primary"
onClick={() => {
void disconnectWallet().then(() =>
setShowDisconnectModal(false),
)
}}
>
Disconnect
</Button>
<Button
size="md"
variant="tertiary"
onClick={() => {
setShowDisconnectModal(false)
}}
>
Cancel
</Button>
</Modal.Footer>
</Modal>
</div>
<div style={{ position: "relative" }}>
<Profile
publicAddress={address}
size="md"
isShort
onClick={() => setShowDisconnectModal(true)}
/>
{walletWarnings.hasWarnings && (
<div
onMouseEnter={() => setIsWarningTooltipVisible(true)}
onMouseLeave={() => setIsWarningTooltipVisible(false)}
style={{
position: "absolute",
top: "-6px",
right: "-6px",
zIndex: 1,
}}
>
<Tooltip
isVisible={isWarningTooltipVisible}
isContrast
placement="bottom"
triggerEl={
<div
onClick={handleWarningClick}
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
cursor: "pointer",
backgroundColor: "var(--color-gray-00, #fff)",
borderRadius: "50%",
padding: "3px",
border: "2px solid var(--color-yellow-60, #f0ad4e)",
boxShadow: "0 1px 3px rgba(0, 0, 0, 0.2)",
}}
>
<Icon.AlertTriangle
style={{
color: "var(--color-yellow-60, #f0ad4e)",
width: "12px",
height: "12px",
}}
/>
</div>
}
>
<div style={{ maxWidth: "15em" }}>
{getWarningMessage()}. Click to learn more about this issue.
</div>
</Tooltip>
</div>
)}
</div>
</div>
)
}