···33import {useFocusEffect, useIsFocused} from '@react-navigation/native'
4455import {Convo} from '#/state/messages/convo/agent'
66-import {ConvoParams, ConvoState} from '#/state/messages/convo/types'
66+import {
77+ ConvoParams,
88+ ConvoState,
99+ ConvoStateBackgrounded,
1010+ ConvoStateReady,
1111+ ConvoStateSuspended,
1212+} from '#/state/messages/convo/types'
1313+import {isConvoActive} from '#/state/messages/convo/util'
714import {useMessagesEventBus} from '#/state/messages/events'
815import {useMarkAsReadMutation} from '#/state/queries/messages/conversation'
916import {useAgent} from '#/state/session'
1717+1818+export * from '#/state/messages/convo/util'
10191120const ChatContext = React.createContext<ConvoState | null>(null)
1221···1423 const ctx = useContext(ChatContext)
1524 if (!ctx) {
1625 throw new Error('useConvo must be used within a ConvoProvider')
2626+ }
2727+ return ctx
2828+}
2929+3030+/**
3131+ * This hook should only be used when the Convo is "active", meaning the chat
3232+ * is loaded and ready to be used, or its in a suspended or background state,
3333+ * and ready for resumption.
3434+ */
3535+export function useConvoActive() {
3636+ const ctx = useContext(ChatContext) as
3737+ | ConvoStateReady
3838+ | ConvoStateBackgrounded
3939+ | ConvoStateSuspended
4040+ if (!ctx) {
4141+ throw new Error('useConvo must be used within a ConvoProvider')
4242+ }
4343+ if (!isConvoActive(ctx)) {
4444+ throw new Error(
4545+ `useConvoActive must only be rendered when the Convo is ready.`,
4646+ )
1747 }
1848 return ctx
1949}
···11+import {
22+ ConvoState,
33+ ConvoStateBackgrounded,
44+ ConvoStateReady,
55+ ConvoStateSuspended,
66+ ConvoStatus,
77+} from './types'
88+99+/**
1010+ * Checks if a `Convo` has a `status` that is "active", meaning the chat is
1111+ * loaded and ready to be used, or its in a suspended or background state, and
1212+ * ready for resumption.
1313+ */
1414+export function isConvoActive(
1515+ convo: ConvoState,
1616+): convo is ConvoStateReady | ConvoStateBackgrounded | ConvoStateSuspended {
1717+ return (
1818+ convo.status === ConvoStatus.Ready ||
1919+ convo.status === ConvoStatus.Backgrounded ||
2020+ convo.status === ConvoStatus.Suspended
2121+ )
2222+}