-
Notifications
You must be signed in to change notification settings - Fork 3
/
useGetAccounts.ts
44 lines (38 loc) · 1.39 KB
/
useGetAccounts.ts
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
'use client';
import { useEffect, useContext } from 'react';
import { quais } from 'quais';
import { DispatchContext } from '@/store';
import { dispatchAccount } from '@/components/lib/utils';
// ---- get accounts ---- //
// called in background on page load, gets user accounts and provider if pelagus is connected
// sets up accountsChanged listener to handle account changes
const useGetAccounts = () => {
const dispatch = useContext(DispatchContext);
useEffect(() => {
const getAccounts = async (provider: any, accounts?: Array<string> | undefined) => {
let account;
await provider
.send('quai_accounts')
.then((accounts: Array<string>) => {
account = dispatchAccount(accounts, dispatch);
})
.catch((err: Error) => {
console.log('Error getting accounts.', err);
});
return account;
};
if (!window.pelagus) {
dispatch({ type: 'SET_WEB3_PROVIDER', payload: undefined });
return;
} else {
const web3provider = new quais.BrowserProvider(window.pelagus);
dispatch({ type: 'SET_WEB3_PROVIDER', payload: web3provider });
getAccounts(web3provider);
window.pelagus.on('accountsChanged', (accounts: Array<string> | undefined) =>
dispatchAccount(accounts, dispatch)
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
};
export default useGetAccounts;