-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathWalletConnector.tsx
More file actions
114 lines (101 loc) · 3.74 KB
/
Copy pathWalletConnector.tsx
File metadata and controls
114 lines (101 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
import React, { useState } from 'react';
import { useWalletStore } from '@/store/walletStore';
import { useWalletConnector } from '@/hooks/useWalletConnector';
import { Button } from '@/components/ui/button';
import { WalletModal } from '@/components/WalletModal';
import { NetworkBadge } from './NetworkBadge';
import { CopyButton } from '@/components/ui/CopyButton';
import {
Loader2,
LogOut,
Wallet,
ChevronDown,
ShieldCheck
} from 'lucide-react';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
DropdownMenuSeparator
} from '@/components/ui/dropdown-menu';
export const WalletConnector: React.FC = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
const { isConnected, address, chainId, disconnect, isConnecting } = useWalletStore();
const { isLoadingConnector } = useWalletConnector();
// Show connecting state if either the chunk is loading or the wallet is authenticating
const isPending = isConnecting || isLoadingConnector;
const handleDisconnect = () => {
disconnect();
};
const formatAddress = (addr: string) => {
return `${addr.substring(0, 6)}...${addr.substring(addr.length - 4)}`;
};
if (!isConnected) {
return (
<>
<Button
onClick={() => setIsModalOpen(true)}
disabled={isPending}
className="flex items-center gap-2"
>
{isPending ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Wallet className="h-4 w-4" />
)}
{isPending ? 'Connecting...' : 'Connect Wallet'}
</Button>
<WalletModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
/>
</>
);
}
return (
<div className="flex items-center gap-2">
{/* Network detection happens inside the Badge */}
<NetworkBadge chainId={chainId} />
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="flex items-center gap-2 px-3">
<div className="flex flex-col items-end hidden sm:flex">
<span className="text-xs font-medium text-muted-foreground uppercase">Connected</span>
<span className="text-sm font-mono">{formatAddress(address!)}</span>
</div>
<div className="sm:hidden">
<Wallet className="h-4 w-4" />
</div>
<ChevronDown className="h-3 w-3 opacity-50" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-56">
<div className="flex items-center justify-between p-2">
<div className="flex flex-col">
<span className="text-xs text-muted-foreground">Wallet Address</span>
<span className="text-sm font-mono truncate max-w-[140px]">{address}</span>
</div>
<CopyButton text={address!} variant="icon" />
</div>
<DropdownMenuSeparator />
<DropdownMenuItem className="flex items-center gap-2 cursor-default">
<ShieldCheck className="h-4 w-4 text-green-500" />
<div className="flex flex-col">
<span className="text-sm">KYC Status</span>
<span className="text-xs text-green-500 font-medium">Verified</span>
</div>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={handleDisconnect}
className="text-destructive focus:text-destructive flex items-center gap-2 cursor-pointer"
>
<LogOut className="h-4 w-4" />
<span>Disconnect</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
);
};