[🐴] Make status checks easier, fix load state (#4010)

* Make status checks easier, fix load state

* Make naming more clear

* Split up types for easier re-use

* Replace hacky usage
This commit is contained in:
Eric Bailey
2024-05-14 11:59:53 -05:00
committed by GitHub
parent bffb9b5906
commit 1c51a48764
6 changed files with 154 additions and 101 deletions
+31 -1
View File
@@ -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,