forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectWalletButton.tsx
More file actions
126 lines (117 loc) · 3.31 KB
/
Copy pathConnectWalletButton.tsx
File metadata and controls
126 lines (117 loc) · 3.31 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
"use client";
import { useErrorHandler } from "@/context/ErrorContext";
import { useStellarWallet } from "@/context/StellarWalletContext";
import { Button, Flex, Text, Tooltip, type ButtonProps } from "@chakra-ui/react";
import { useState } from "react";
const ACCENT = "#4AE292";
type ConnectWalletButtonProps = ButtonProps & {
label?: string;
};
function truncateKey(key: string): string {
return `${key.slice(0, 4)}...${key.slice(-4)}`;
}
export default function ConnectWalletButton({
label = "Connect Freighter",
...buttonProps
}: ConnectWalletButtonProps) {
const { connect, disconnect, publicKey, isConnected } = useStellarWallet();
const toast = useErrorHandler();
const [isConnecting, setIsConnecting] = useState(false);
const [copied, setCopied] = useState(false);
const handleConnect = async () => {
setIsConnecting(true);
try {
await connect();
toast.success("Wallet connected", "Freighter is now linked to SmartDrop");
} catch (error) {
toast.handleError(error, "Wallet Connection");
} finally {
setIsConnecting(false);
}
};
const handleCopy = async () => {
if (!publicKey) return;
try {
await navigator.clipboard.writeText(publicKey);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch {
// Clipboard may be unavailable in hardened browser contexts.
}
};
if (isConnected && publicKey) {
return (
<Flex
align="center"
gap={2}
position="fixed"
bottom={{ base: "16px", md: "20px" }}
right={{ base: "16px", md: "20px" }}
left={{ base: "16px", md: "auto" }}
w={{ base: "calc(100% - 32px)", md: "auto" }}
justify={{ base: "center", md: "flex-start" }}
bg="#1a1a1a"
border="1px solid #333"
borderRadius="3xl"
px={4}
py={2}
>
<Tooltip
label={copied ? "Copied!" : publicKey}
hasArrow
bg="#222"
color="#fff"
fontSize="xs"
>
<Text
fontSize="sm"
fontFamily="mono"
color={ACCENT}
cursor="pointer"
onClick={() => void handleCopy()}
_hover={{ opacity: 0.8 }}
userSelect="none"
>
{truncateKey(publicKey)}
</Text>
</Tooltip>
<Text color="#555" fontSize="xs">
|
</Text>
<Text
fontSize="xs"
color="#A2A2A2"
cursor="pointer"
_hover={{ color: "#fff" }}
onClick={disconnect}
userSelect="none"
>
Disconnect
</Text>
</Flex>
);
}
return (
<Button
bgColor={ACCENT}
color="#000"
borderRadius="3xl"
position="fixed"
bottom={{ base: "16px", md: "20px" }}
right={{ base: "16px", md: "20px" }}
left={{ base: "16px", md: "auto" }}
w={{ base: "calc(100% - 32px)", md: "auto" }}
px={6}
py={4}
fontWeight="bold"
transition="box-shadow 0.2s ease, opacity 0.2s ease"
_hover={{ opacity: 0.9, boxShadow: "glow" }}
{...buttonProps}
onClick={() => void handleConnect()}
isLoading={isConnecting || Boolean(buttonProps.isLoading)}
loadingText={buttonProps.loadingText ?? "Connecting..."}
>
{label}
</Button>
);
}