-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract wallet and connection hooks
- Loading branch information
1 parent
dfed313
commit d8a0f66
Showing
15 changed files
with
226 additions
and
11,614 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { PageContent, Input, Button, Stack, Card, Flex } from 'bumbag'; | ||
import React, { useCallback } from 'react'; | ||
import { useWallet } from './hooks'; | ||
import useSocket from './hooks/socket'; | ||
|
||
const App = (): JSX.Element => { | ||
const { library, account, onConnectWallet, active } = useWallet(); | ||
const { connection } = useSocket({ | ||
onMessageReceived: useCallback(() => undefined, []), | ||
}); | ||
|
||
const onRequestPay = useCallback(() => { | ||
if (!library || !account || !connection) return; | ||
(async () => { | ||
try { | ||
const signer = library.getSigner(account); | ||
const signature = await signer.signMessage('👋'); | ||
|
||
connection.send( | ||
JSON.stringify({ | ||
signature, | ||
qrCode: 'Here it comes!', | ||
}) | ||
); | ||
console.info('Message sent'); | ||
} catch (error) { | ||
console.error( | ||
`Failure!${error && error.message ? `\n\n${error.message}` : ''}` | ||
); | ||
} | ||
})(); | ||
}, [library, account, connection]); | ||
|
||
return ( | ||
<PageContent> | ||
<Card> | ||
<Stack> | ||
<Input placeholder="Enter your the QR code here." /> | ||
<Flex alignX="right"> | ||
{active ? ( | ||
library && | ||
account && ( | ||
<Button palette="primary" onClick={onRequestPay}> | ||
Pay | ||
</Button> | ||
) | ||
) : ( | ||
<Button palette="primary" onClick={onConnectWallet}> | ||
Connect | ||
</Button> | ||
)} | ||
</Flex> | ||
</Stack> | ||
</Card> | ||
</PageContent> | ||
); | ||
}; | ||
|
||
export default App; |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { useWeb3React } from '@web3-react/core'; | ||
import { injected } from '../connectors'; | ||
|
||
export default function useEagerConnect(): boolean { | ||
const { activate, active } = useWeb3React(); | ||
|
||
const [tried, setTried] = useState(false); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
if (tried) return; | ||
try { | ||
if (await injected.isAuthorized()) activate(injected, undefined, true); | ||
} catch { | ||
setTried(true); | ||
} | ||
})(); | ||
}, [activate, tried]); | ||
|
||
// If the connection worked, wait until we get confirmation of that to flip the flag. | ||
useEffect(() => { | ||
if (!tried && active) setTried(true); | ||
}, [tried, active]); | ||
|
||
return tried; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as useEagerConnect } from './eager-connect'; | ||
export { default as useInactiveListener } from './inactive-listener'; | ||
export { default as useWallet } from './wallet'; |
Oops, something went wrong.