Fixes issue with (#2119)

* Allow going directly to password input screen when switching accounts and password is required

* Revise state handling

* Handle logged out states, enable clearing requestedAccount

---------

Co-authored-by: Eric Bailey <git@esb.lol>
This commit is contained in:
Ansh 2023-12-07 16:53:50 -08:00 committed by GitHub
parent afca4bf701
commit 9d51886e43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 22 deletions

View file

@ -1,23 +1,77 @@
import React from 'react'
type StateContext = {
type State = {
showLoggedOut: boolean
/**
* Account did used to populate the login form when the logged out view is
* shown.
*/
requestedAccountSwitchTo?: string
}
const StateContext = React.createContext<StateContext>({
showLoggedOut: false,
})
const ControlsContext = React.createContext<{
type Controls = {
/**
* Show or hide the logged out view.
*/
setShowLoggedOut: (show: boolean) => void
}>({
/**
* Shows the logged out view and drops the user into the login form using the
* requested account.
*/
requestSwitchToAccount: (props: {
/**
* The did of the account to populate the login form with.
*/
requestedAccount?: string
}) => void
/**
* Clears the requested account so that next time the logged out view is
* show, no account is pre-populated.
*/
clearRequestedAccount: () => void
}
const StateContext = React.createContext<State>({
showLoggedOut: false,
requestedAccountSwitchTo: undefined,
})
const ControlsContext = React.createContext<Controls>({
setShowLoggedOut: () => {},
requestSwitchToAccount: () => {},
clearRequestedAccount: () => {},
})
export function Provider({children}: React.PropsWithChildren<{}>) {
const [showLoggedOut, setShowLoggedOut] = React.useState(false)
const [state, setState] = React.useState<State>({
showLoggedOut: false,
requestedAccountSwitchTo: undefined,
})
const state = React.useMemo(() => ({showLoggedOut}), [showLoggedOut])
const controls = React.useMemo(() => ({setShowLoggedOut}), [setShowLoggedOut])
const controls = React.useMemo<Controls>(
() => ({
setShowLoggedOut(show) {
setState(s => ({
...s,
showLoggedOut: show,
}))
},
requestSwitchToAccount({requestedAccount}) {
setState(s => ({
...s,
showLoggedOut: true,
requestedAccountSwitchTo: requestedAccount,
}))
},
clearRequestedAccount() {
setState(s => ({
...s,
requestedAccountSwitchTo: undefined,
}))
},
}),
[setState],
)
return (
<StateContext.Provider value={state}>