🐛 Partially fixed bugs with connecting to wallet button. Removed one button state for simplicity.

This commit is contained in:
sid
2024-10-26 16:02:40 +01:00
parent d77c490e01
commit d33b175b64
3 changed files with 24 additions and 134 deletions

View File

@@ -37,20 +37,16 @@ function formatAddress(address: string | undefined): string {
function MetaMaskConnect() {
const { sdk, connected, connecting, account } = useSDK();
const [metaMaskInstalled, setMetaMaskInstalled] = useState(false);
const isMetaMaskInstalled = () =>
typeof window !== 'undefined' && typeof window.ethereum !== 'undefined';
const [isConnected, setIsConnected] = useState(false);
useEffect(() => {
if (isMetaMaskInstalled()) {
setMetaMaskInstalled(true);
}
}, []);
setIsConnected(connected);
}, [connected]);
const connect = async () => {
try {
await sdk?.connect();
setIsConnected(true);
} catch (err) {
console.warn(`No accounts found`, err);
}
@@ -59,36 +55,25 @@ function MetaMaskConnect() {
const disconnect = () => {
if (sdk) {
sdk.terminate();
setIsConnected(false);
}
};
if (!metaMaskInstalled) {
return (
<Button
onClick={() =>
window.open('https://metamask.io/download.html', '_blank')
}
className="bg-gradient-to-r from-blue-500 to-indigo-700"
>
Install MetaMask
</Button>
);
}
return (
<div className="relative">
{connected ? (
{isConnected ? (
<Popover>
<PopoverTrigger asChild>
<Button>{formatAddress(account)}</Button>
</PopoverTrigger>
<PopoverContent className="w-44">
<button
<Button
variant="destructive"
onClick={disconnect}
className="w-full px-4 py-2 text-left text-destructive hover:bg-muted"
className="w-full px-4 py-2 text-left hover:bg-muted hover:text-destructive"
>
Disconnect
</button>
</Button>
</PopoverContent>
</Popover>
) : (