about summary refs log tree commit diff
path: root/src/state/shell/logged-out.tsx
diff options
context:
space:
mode:
authorAnsh <anshnanda10@gmail.com>2023-12-07 16:53:50 -0800
committerGitHub <noreply@github.com>2023-12-07 16:53:50 -0800
commit9d51886e438b1676706009d91bdcf20e8df5dd58 (patch)
tree97abc18e7ddf3c288c355ba81988c959df86908e /src/state/shell/logged-out.tsx
parentafca4bf70160b6b15e3fe55e2cf8ba326a30597c (diff)
downloadvoidsky-9d51886e438b1676706009d91bdcf20e8df5dd58.tar.zst
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>
Diffstat (limited to 'src/state/shell/logged-out.tsx')
-rw-r--r--src/state/shell/logged-out.tsx70
1 files changed, 62 insertions, 8 deletions
diff --git a/src/state/shell/logged-out.tsx b/src/state/shell/logged-out.tsx
index 19eaee76b..a06a6d969 100644
--- a/src/state/shell/logged-out.tsx
+++ b/src/state/shell/logged-out.tsx
@@ -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>({
+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<{
-  setShowLoggedOut: (show: boolean) => void
-}>({
+
+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}>