[🐴] Do not init event bus if no session (#4193)

* Do not init event bus if no session

* Be explicit

* Simplify, fix log
zio/stable
Eric Bailey 2024-05-23 12:00:56 -05:00 committed by GitHub
parent 17e0cb62a8
commit 3d1ed04a70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 24 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import React from 'react'
import {AppState} from 'react-native'
import {MessagesEventBus} from '#/state/messages/events/agent'
import {useAgent} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
const MessagesEventBusContext = React.createContext<MessagesEventBus | null>(
null,
@ -11,7 +11,9 @@ const MessagesEventBusContext = React.createContext<MessagesEventBus | null>(
export function useMessagesEventBus() {
const ctx = React.useContext(MessagesEventBusContext)
if (!ctx) {
throw new Error('useChat must be used within a ChatProvider')
throw new Error(
'useMessagesEventBus must be used within a MessagesEventBusProvider',
)
}
return ctx
}
@ -20,6 +22,26 @@ export function MessagesEventBusProvider({
children,
}: {
children: React.ReactNode
}) {
const {currentAccount} = useSession()
if (!currentAccount) {
return (
<MessagesEventBusContext.Provider value={null}>
{children}
</MessagesEventBusContext.Provider>
)
}
return (
<MessagesEventBusProviderInner>{children}</MessagesEventBusProviderInner>
)
}
export function MessagesEventBusProviderInner({
children,
}: {
children: React.ReactNode
}) {
const {getAgent} = useAgent()
const [bus] = React.useState(