-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
140 additions
and
42 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
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,70 @@ | ||
import React, { | ||
useState, | ||
createContext, | ||
useContext, | ||
useCallback, | ||
useEffect, | ||
} from 'react' | ||
import { InjectedAccount } from 'polkadot-api/pjs-signer' | ||
import { useAccounts as useRedotAccounts } from '@reactive-dot/react' | ||
|
||
const LOCALSTORAGE_SELECTED_ACCOUNT_KEY = 'delegit.selectedAccount' | ||
|
||
type AccountContextProps = { | ||
children: React.ReactNode | React.ReactNode[] | ||
} | ||
|
||
export interface IAccountContext { | ||
selectedAccount?: InjectedAccount | ||
accounts: InjectedAccount[] | ||
selectAccount: (account: InjectedAccount) => void | ||
} | ||
|
||
const AccountContext = createContext<IAccountContext | undefined>(undefined) | ||
|
||
const AccountContextProvider = ({ children }: AccountContextProps) => { | ||
const accounts = useRedotAccounts() | ||
const [selectedAccount, setSelected] = useState<InjectedAccount | undefined>() | ||
|
||
const selectAccount = useCallback((account: InjectedAccount) => { | ||
localStorage.setItem(LOCALSTORAGE_SELECTED_ACCOUNT_KEY, account.address) | ||
setSelected(account) | ||
}, []) | ||
|
||
useEffect(() => { | ||
const previousAccountAddress = localStorage.getItem( | ||
LOCALSTORAGE_SELECTED_ACCOUNT_KEY, | ||
) | ||
|
||
if (previousAccountAddress) { | ||
const account = accounts.find( | ||
(account) => account.address === previousAccountAddress, | ||
) | ||
!!account && selectAccount(account) | ||
} else { | ||
selectAccount(accounts[0]) | ||
} | ||
}, [accounts, selectAccount]) | ||
|
||
return ( | ||
<AccountContext.Provider | ||
value={{ | ||
accounts, | ||
selectedAccount, | ||
selectAccount, | ||
}} | ||
> | ||
{children} | ||
</AccountContext.Provider> | ||
) | ||
} | ||
|
||
const useAccounts = () => { | ||
const context = useContext(AccountContext) | ||
if (context === undefined) { | ||
throw new Error('useAccounts must be used within a AccountContextProvider') | ||
} | ||
return context | ||
} | ||
|
||
export { AccountContextProvider, useAccounts } | ||
Check warning on line 70 in src/contexts/AccountsContext.tsx GitHub Actions / build (18.x)
|
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