Created
December 11, 2021 21:09
-
-
Save vit0rr/6d25688f1cc966a91a2f0cc5b0167c0c to your computer and use it in GitHub Desktop.
Button to connect your Phantom wallet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* We are going to be using the useEffect hook! | |
Code based on buildspace | |
*/ | |
import React, { useEffect, useState } from 'react'; | |
import './App.css'; | |
const App = () => { | |
/* | |
* This function holds the logic for deciding if a Phantom Wallet is | |
* connected or not | |
*/ | |
const [walletAddress, setWalletAddress] = useState(null); | |
const checkIfWalletIsConnected = async () => { | |
try { | |
const { solana } = window; | |
if (solana) { | |
if (solana.isPhantom) { | |
console.log('Phantom wallet found!'); | |
const response = await solana.connect({onlyIfTrusted: true}) | |
console.log( | |
'Connected with Public Key', | |
response.publicKey.toString() | |
) | |
setWalletAddress(response.publicKey.toString()) | |
} | |
} else { | |
alert('Solana object not found! Get a Phantom Wallet 👻'); | |
} | |
} catch (error) { | |
console.error(error); | |
} | |
}; | |
const connectWallet = async() => { | |
const {solana} = window | |
if (solana){ | |
const response = await solana.connect() | |
console.log('Connected with Public Key:', response.publicKey.toString()) | |
setWalletAddress(response.publicKey.toString()) | |
} | |
}; | |
const renderNotConnectedContainer = () => ( | |
<button | |
className="cta-button connect-wallet-button" | |
onClick={connectWallet} | |
> | |
Connect to Wallet | |
</button> | |
); | |
/* | |
* When our component first mounts, let's check to see if we have a connected | |
* Phantom Wallet | |
*/ | |
useEffect(() => { | |
const onLoad = async () => { | |
await checkIfWalletIsConnected(); | |
}; | |
window.addEventListener('load', onLoad); | |
return () => window.removeEventListener('load', onLoad); | |
}, []); | |
return ( | |
<div className="App"> | |
{/* This was solely added for some styling fanciness */} | |
<div className={walletAddress ? 'authed-container' : 'container'}> | |
<div className="container"> | |
<div className="header-container"> | |
<p className="header">🖼 GIF Portal</p> | |
<p className="sub-text"> | |
View your GIF collection in the metaverse ✨ | |
</p> | |
{!walletAddress && renderNotConnectedContainer()} | |
</div> | |
</div> | |
</div> | |
</div> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment