Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store active extension + wallet in localstorage and initialize lazy #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import {
import { getSubstrateChain } from './chains'
import { getNightlyConnectAdapter } from './helpers/getNightlyAdapter'

export const LS_ACTIVE_ACCOUNT_ADDRESS = 'activeAccountAddress'
export const LS_ACTIVE_EXTENSION_ID = 'activeExtensionId'

const UseInkathonProviderContext = createContext<UseInkathonProviderContextType | null>(null)

/**
Expand Down Expand Up @@ -87,7 +90,7 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
const [api, setApi] = useState<ApiPromise>()
const [provider, setProvider] = useState<WsProvider | HttpProvider>()
const [accounts, setAccounts] = useState<InjectedAccount[]>([])
const [activeAccount, setActiveAccount] = useState<InjectedAccount>()
const [activeAccount, _setActiveAccount] = useState<InjectedAccount>()
const [lastActiveAccount, setLastActiveAccount] = useState<InjectedAccount>()
const activeExtension = useRef<InjectedExtension>()
const activeSigner = useRef<Signer>()
Expand Down Expand Up @@ -138,6 +141,30 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
return _api
}

// Set active account with local storage persistence
const setActiveAccount: React.Dispatch<React.SetStateAction<InjectedAccount | undefined>> = (
account,
) => {
if (typeof account === 'function') {
_setActiveAccount((prevAccount) => {
const newAccount = account(prevAccount)
if (newAccount) {
localStorage.setItem(LS_ACTIVE_ACCOUNT_ADDRESS, newAccount.address)
} else {
localStorage.removeItem(LS_ACTIVE_ACCOUNT_ADDRESS)
}
return newAccount
})
} else {
_setActiveAccount(account)
if (account) {
localStorage.setItem(LS_ACTIVE_ACCOUNT_ADDRESS, account.address)
} else {
localStorage.removeItem(LS_ACTIVE_ACCOUNT_ADDRESS)
}
}
}

// Updates account list and active account
const updateAccounts = (
injectedAccounts: InjectedAccount[],
Expand All @@ -148,6 +175,7 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
const _lastAccount = lastActiveAccountAddress
? { address: lastActiveAccountAddress }
: lastActiveAccount

const newAccount =
newAccounts.find((a) => accountsAreEqual(a, _lastAccount)) || newAccounts?.[0]

Expand Down Expand Up @@ -207,6 +235,7 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
// Enable wallet
const extension = await enableWallet(_wallet, appName)
activeExtension.current = extension
localStorage.setItem(LS_ACTIVE_EXTENSION_ID, _wallet.id)
activeSigner.current = extension?.signer as Signer

// Query & keep listening to injected accounts
Expand Down Expand Up @@ -235,6 +264,8 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
if (disconnectApi) {
await provider?.disconnect()
await api?.disconnect()
localStorage.removeItem(LS_ACTIVE_EXTENSION_ID)
localStorage.removeItem(LS_ACTIVE_ACCOUNT_ADDRESS)
return
}
if (activeExtension.current?.name === nightlyConnect.id) {
Expand Down Expand Up @@ -265,7 +296,20 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
// Initialize
useEffect(() => {
if (isInitialized.current || isInitializing.current) return
connectOnInit ? connect(undefined, undefined, undefined, true) : initialize()

const activeExtensionId = localStorage.getItem(LS_ACTIVE_EXTENSION_ID) || undefined
const activeAccountAddress = localStorage.getItem(LS_ACTIVE_ACCOUNT_ADDRESS) || undefined
const userWantsConnection = activeExtensionId && activeAccountAddress

let activeExtension: SubstrateWallet | undefined

if (activeExtensionId) {
activeExtension = allSubstrateWallets.find((w) => w.id === activeExtensionId)
}

connectOnInit || userWantsConnection
? connect(undefined, activeExtension, activeAccountAddress, true)
: initialize()
return () => {
unsubscribeAccounts.current?.()
}
Expand Down