-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor for reusable
ExtensionAccountsProvider
context (#1466)
- Loading branch information
Ross Bulat
authored
Oct 7, 2023
1 parent
34985f1
commit 13380bb
Showing
170 changed files
with
1,560 additions
and
1,319 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
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
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,14 @@ | ||
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
import type { ActiveAccountsContextInterface } from './types'; | ||
|
||
export const defaultActiveAccountsContext: ActiveAccountsContextInterface = { | ||
activeAccount: null, | ||
activeProxy: null, | ||
activeProxyType: null, | ||
getActiveAccount: () => null, | ||
setActiveAccount: (address, updateLocal) => {}, | ||
setActiveProxy: (address, updateLocal) => {}, | ||
}; |
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,80 @@ | ||
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import type { ReactNode } from 'react'; | ||
import { createContext, useContext, useEffect, useRef, useState } from 'react'; | ||
import type { MaybeAddress } from 'types'; | ||
import { setStateWithRef } from '@polkadot-cloud/utils'; | ||
import { useNetwork } from 'contexts/Network'; | ||
import type { ActiveAccountsContextInterface, ActiveProxy } from './types'; | ||
import { defaultActiveAccountsContext } from './defaults'; | ||
|
||
export const ActiveAccountsContext = | ||
createContext<ActiveAccountsContextInterface>(defaultActiveAccountsContext); | ||
|
||
export const ActiveAccountsProvider = ({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}) => { | ||
const { network } = useNetwork(); | ||
|
||
// Store the currently active account. | ||
const [activeAccount, setActiveAccountState] = useState<MaybeAddress>(null); | ||
const activeAccountRef = useRef<string | null>(activeAccount); | ||
|
||
// Store the active proxy account. | ||
const [activeProxy, setActiveProxyState] = useState<ActiveProxy>(null); | ||
const activeProxyRef = useRef(activeProxy); | ||
|
||
// Setter for the active proxy account. | ||
const setActiveProxy = (newActiveProxy: ActiveProxy, updateLocal = true) => { | ||
if (updateLocal) | ||
if (newActiveProxy) { | ||
localStorage.setItem( | ||
`${network}_active_proxy`, | ||
JSON.stringify(newActiveProxy) | ||
); | ||
} else { | ||
localStorage.removeItem(`${network}_active_proxy`); | ||
} | ||
setStateWithRef(newActiveProxy, setActiveProxyState, activeProxyRef); | ||
}; | ||
|
||
// Setter for the active account. | ||
const setActiveAccount = ( | ||
newActiveAccount: MaybeAddress, | ||
local: boolean = true | ||
) => { | ||
if (local) | ||
if (newActiveAccount === null) { | ||
localStorage.removeItem(`${network}_active_account`); | ||
} else { | ||
localStorage.setItem(`${network}_active_account`, newActiveAccount); | ||
} | ||
setStateWithRef(newActiveAccount, setActiveAccountState, activeAccountRef); | ||
}; | ||
|
||
// Getter for the active account. | ||
const getActiveAccount = () => activeAccountRef.current; | ||
|
||
// Disconnect from the active account on network change, but don't remove local record. | ||
useEffect(() => setActiveAccount(null, false), [network]); | ||
|
||
return ( | ||
<ActiveAccountsContext.Provider | ||
value={{ | ||
activeAccount: activeAccountRef.current, | ||
activeProxy: activeProxyRef.current?.address ?? null, | ||
activeProxyType: activeProxyRef.current?.proxyType ?? null, | ||
setActiveAccount, | ||
getActiveAccount, | ||
setActiveProxy, | ||
}} | ||
> | ||
{children} | ||
</ActiveAccountsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useActiveAccounts = () => useContext(ActiveAccountsContext); |
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,18 @@ | ||
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import type { MaybeAddress } from 'types'; | ||
|
||
export interface ActiveAccountsContextInterface { | ||
activeAccount: MaybeAddress; | ||
activeProxy: MaybeAddress; | ||
activeProxyType: string | null; | ||
getActiveAccount: () => string | null; | ||
setActiveAccount: (address: MaybeAddress, updateLocal?: boolean) => void; | ||
setActiveProxy: (address: ActiveProxy, updateLocal?: boolean) => void; | ||
} | ||
|
||
export type ActiveProxy = { | ||
address: MaybeAddress; | ||
proxyType: string; | ||
} | null; |
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
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
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
Oops, something went wrong.