import { useAuth } from '@sunnyside-io/privacy-boost-react';
function ConnectWallet() {
const {
isAuthenticated,
isWalletAvailable,
address,
authenticateWithWalletAdapter,
logout,
} = useAuth();
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const handleConnect = async () => {
setLoading(true);
setError(null);
try {
await authenticateWithWalletAdapter();
} catch (err: any) {
if (err.code !== 'SIGNATURE_REJECTED') {
setError(err.message);
}
} finally {
setLoading(false);
}
};
if (!isWalletAvailable) {
return <p>Please install a wallet extension</p>;
}
if (isAuthenticated) {
return (
<div>
<p>Connected: {address?.slice(0, 6)}...{address?.slice(-4)}</p>
<button onClick={logout}>Disconnect</button>
</div>
);
}
return (
<div>
{error && <p style={{ color: 'red' }}>{error}</p>}
<button onClick={handleConnect} disabled={loading}>
{loading ? 'Connecting...' : 'Connect Wallet'}
</button>
</div>
);
}