about summary refs log tree commit diff
path: root/src/state/session/index.tsx
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2023-11-10 09:59:04 -0600
committerEric Bailey <git@esb.lol>2023-11-10 09:59:04 -0600
commit436a14eabb4fe2238ff6048f41042433c0e07268 (patch)
tree047882fe839963796eaa31e908702e3ccd9e86e0 /src/state/session/index.tsx
parentd0d93168d4a33c9fc06f84fd3aa98bef6f5f90f0 (diff)
downloadvoidsky-436a14eabb4fe2238ff6048f41042433c0e07268.tar.zst
Pare down session as much as possible
Diffstat (limited to 'src/state/session/index.tsx')
-rw-r--r--src/state/session/index.tsx36
1 files changed, 21 insertions, 15 deletions
diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx
index 90948b01e..d0ca10137 100644
--- a/src/state/session/index.tsx
+++ b/src/state/session/index.tsx
@@ -1,20 +1,25 @@
 import React from 'react'
+import {DeviceEventEmitter} from 'react-native'
 import {BskyAgent, AtpPersistSessionHandler} from '@atproto/api'
 
 import {networkRetry} from '#/lib/async/retry'
 import {logger} from '#/logger'
 import * as persisted from '#/state/persisted'
 import {PUBLIC_BSKY_AGENT} from '#/state/queries'
+import {IS_PROD} from '#/lib/constants'
 
 export type SessionAccount = persisted.PersistedAccount
 
-export type StateContext = {
+export type SessionState = {
   agent: BskyAgent
   isInitialLoad: boolean
   isSwitchingAccounts: boolean
   accounts: persisted.PersistedAccount[]
   currentAccount: persisted.PersistedAccount | undefined
+}
+export type StateContext = SessionState & {
   hasSession: boolean
+  isSandbox: boolean
 }
 export type ApiContext = {
   createAccount: (props: {
@@ -30,15 +35,13 @@ export type ApiContext = {
     password: string
   }) => Promise<void>
   logout: () => Promise<void>
-  initSession: (account: persisted.PersistedAccount) => Promise<void>
-  resumeSession: (account?: persisted.PersistedAccount) => Promise<void>
-  removeAccount: (
-    account: Partial<Pick<persisted.PersistedAccount, 'handle' | 'did'>>,
-  ) => void
-  selectAccount: (account: persisted.PersistedAccount) => Promise<void>
+  initSession: (account: SessionAccount) => Promise<void>
+  resumeSession: (account?: SessionAccount) => Promise<void>
+  removeAccount: (account: SessionAccount) => void
+  selectAccount: (account: SessionAccount) => Promise<void>
   updateCurrentAccount: (
     account: Partial<
-      Pick<persisted.PersistedAccount, 'handle' | 'email' | 'emailConfirmed'>
+      Pick<SessionAccount, 'handle' | 'email' | 'emailConfirmed'>
     >,
   ) => void
   clearCurrentAccount: () => void
@@ -46,11 +49,12 @@ export type ApiContext = {
 
 const StateContext = React.createContext<StateContext>({
   agent: PUBLIC_BSKY_AGENT,
-  hasSession: false,
   isInitialLoad: true,
   isSwitchingAccounts: false,
   accounts: [],
   currentAccount: undefined,
+  hasSession: false,
+  isSandbox: false,
 })
 
 const ApiContext = React.createContext<ApiContext>({
@@ -94,6 +98,8 @@ function createPersistSessionHandler(
       logger.DebugContext.session,
     )
 
+    if (expired) DeviceEventEmitter.emit('session-dropped')
+
     persistSessionCallback({
       expired,
       refreshedAccount,
@@ -103,9 +109,8 @@ function createPersistSessionHandler(
 
 export function Provider({children}: React.PropsWithChildren<{}>) {
   const isDirty = React.useRef(false)
-  const [state, setState] = React.useState<StateContext>({
+  const [state, setState] = React.useState<SessionState>({
     agent: PUBLIC_BSKY_AGENT,
-    hasSession: false,
     isInitialLoad: true, // try to resume the session first
     isSwitchingAccounts: false,
     accounts: persisted.get('session').accounts,
@@ -113,7 +118,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
   })
 
   const setStateAndPersist = React.useCallback(
-    (fn: (prev: StateContext) => StateContext) => {
+    (fn: (prev: SessionState) => SessionState) => {
       isDirty.current = true
       setState(fn)
     },
@@ -312,9 +317,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
       setStateAndPersist(s => {
         return {
           ...s,
-          accounts: s.accounts.filter(
-            a => !(a.did === account.did || a.handle === account.handle),
-          ),
+          accounts: s.accounts.filter(a => a.did !== account.did),
         }
       })
     },
@@ -431,6 +434,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
     () => ({
       ...state,
       hasSession: !!state.currentAccount,
+      isSandbox: state.currentAccount
+        ? !IS_PROD(state.currentAccount?.service)
+        : false,
     }),
     [state],
   )