about summary refs log tree commit diff
path: root/src/state/messages/index.tsx
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2024-05-07 17:54:52 -0500
committerGitHub <noreply@github.com>2024-05-07 17:54:52 -0500
commitf78126e01a43250b0968321d959b8722d0d399ff (patch)
tree72fa45361989134b2767ee115cf8cfb27d32c33f /src/state/messages/index.tsx
parent87cb4c105e786b73b8994313e8d46fa86aba96c4 (diff)
downloadvoidsky-f78126e01a43250b0968321d959b8722d0d399ff.tar.zst
[🐴] State transitions (#3880)
* Handle init/resume/suspend/background and polling

* Add debug and temp guard

* Make state transitions sync

* Make init sync also

* Checkpoint: confusing but working state machine

* Reducer-esque

* Remove poll events

* Guard fetchConvo

(cherry picked from commit 8385579d31500bb4bfb60afeecdc1eb3ddd7e747)

* Clean up polling, make sync

(cherry picked from commit 7f75cd04c3bf81c94662785748698640a84bef51)

* Update history handling

(cherry picked from commit b82b552ba4040adf7ead2377541132a386964ff8)

* Check for screen focus in app state listener

* Get rid of ad-hoc status checks
Diffstat (limited to 'src/state/messages/index.tsx')
-rw-r--r--src/state/messages/index.tsx22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/state/messages/index.tsx b/src/state/messages/index.tsx
index 22c4242e2..95ebf0afd 100644
--- a/src/state/messages/index.tsx
+++ b/src/state/messages/index.tsx
@@ -1,6 +1,7 @@
 import React, {useContext, useState, useSyncExternalStore} from 'react'
+import {AppState} from 'react-native'
 import {BskyAgent} from '@atproto-labs/api'
-import {useFocusEffect} from '@react-navigation/native'
+import {useFocusEffect, useIsFocused} from '@react-navigation/native'
 
 import {Convo, ConvoParams, ConvoState} from '#/state/messages/convo'
 import {useAgent} from '#/state/session'
@@ -20,6 +21,7 @@ export function ChatProvider({
   children,
   convoId,
 }: Pick<ConvoParams, 'convoId'> & {children: React.ReactNode}) {
+  const isScreenFocused = useIsFocused()
   const {serviceUrl} = useDmServiceUrlStorage()
   const {getAgent} = useAgent()
   const [convo] = useState(
@@ -44,5 +46,23 @@ export function ChatProvider({
     }, [convo]),
   )
 
+  React.useEffect(() => {
+    const handleAppStateChange = (nextAppState: string) => {
+      if (isScreenFocused) {
+        if (nextAppState === 'active') {
+          convo.resume()
+        } else {
+          convo.background()
+        }
+      }
+    }
+
+    const sub = AppState.addEventListener('change', handleAppStateChange)
+
+    return () => {
+      sub.remove()
+    }
+  }, [convo, isScreenFocused])
+
   return <ChatContext.Provider value={service}>{children}</ChatContext.Provider>
 }