From 1c51a48764e4145679198f68368713410e28c8da Mon Sep 17 00:00:00 2001
From: Eric Bailey <git@esb.lol>
Date: Tue, 14 May 2024 11:59:53 -0500
Subject: [🐴] Make status checks easier, fix load state (#4010)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Make status checks easier, fix load state

* Make naming more clear

* Split up types for easier re-use

* Replace hacky usage
---
 src/state/messages/convo/index.tsx | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

(limited to 'src/state/messages/convo/index.tsx')

diff --git a/src/state/messages/convo/index.tsx b/src/state/messages/convo/index.tsx
index 9c5295832..e955d4118 100644
--- a/src/state/messages/convo/index.tsx
+++ b/src/state/messages/convo/index.tsx
@@ -3,11 +3,20 @@ import {AppState} from 'react-native'
 import {useFocusEffect, useIsFocused} from '@react-navigation/native'
 
 import {Convo} from '#/state/messages/convo/agent'
-import {ConvoParams, ConvoState} from '#/state/messages/convo/types'
+import {
+  ConvoParams,
+  ConvoState,
+  ConvoStateBackgrounded,
+  ConvoStateReady,
+  ConvoStateSuspended,
+} from '#/state/messages/convo/types'
+import {isConvoActive} from '#/state/messages/convo/util'
 import {useMessagesEventBus} from '#/state/messages/events'
 import {useMarkAsReadMutation} from '#/state/queries/messages/conversation'
 import {useAgent} from '#/state/session'
 
+export * from '#/state/messages/convo/util'
+
 const ChatContext = React.createContext<ConvoState | null>(null)
 
 export function useConvo() {
@@ -18,6 +27,27 @@ export function useConvo() {
   return ctx
 }
 
+/**
+ * This hook should only be used when the Convo is "active", meaning the chat
+ * is loaded and ready to be used, or its in a suspended or background state,
+ * and ready for resumption.
+ */
+export function useConvoActive() {
+  const ctx = useContext(ChatContext) as
+    | ConvoStateReady
+    | ConvoStateBackgrounded
+    | ConvoStateSuspended
+  if (!ctx) {
+    throw new Error('useConvo must be used within a ConvoProvider')
+  }
+  if (!isConvoActive(ctx)) {
+    throw new Error(
+      `useConvoActive must only be rendered when the Convo is ready.`,
+    )
+  }
+  return ctx
+}
+
 export function ConvoProvider({
   children,
   convoId,
-- 
cgit 1.4.1

