[🐴] Integrate global event bus (#3904)
* Conditionally run global event bus * Add current convo id context, bundle providers
This commit is contained in:
parent
37f22ca224
commit
165fdb7049
6 changed files with 112 additions and 31 deletions
38
src/state/messages/current-convo-id.tsx
Normal file
38
src/state/messages/current-convo-id.tsx
Normal file
|
@ -0,0 +1,38 @@
|
|||
import React from 'react'
|
||||
|
||||
const CurrentConvoIdContext = React.createContext<{
|
||||
currentConvoId: string | undefined
|
||||
setCurrentConvoId: (convoId: string | undefined) => void
|
||||
}>({
|
||||
currentConvoId: undefined,
|
||||
setCurrentConvoId: () => {},
|
||||
})
|
||||
|
||||
export function useCurrentConvoId() {
|
||||
const ctx = React.useContext(CurrentConvoIdContext)
|
||||
if (!ctx) {
|
||||
throw new Error(
|
||||
'useCurrentConvoId must be used within a CurrentConvoIdProvider',
|
||||
)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
export function CurrentConvoIdProvider({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const [currentConvoId, setCurrentConvoId] = React.useState<
|
||||
string | undefined
|
||||
>()
|
||||
const ctx = React.useMemo(
|
||||
() => ({currentConvoId, setCurrentConvoId}),
|
||||
[currentConvoId],
|
||||
)
|
||||
return (
|
||||
<CurrentConvoIdContext.Provider value={ctx}>
|
||||
{children}
|
||||
</CurrentConvoIdContext.Provider>
|
||||
)
|
||||
}
|
|
@ -2,6 +2,7 @@ import React from 'react'
|
|||
import {AppState} from 'react-native'
|
||||
import {BskyAgent} from '@atproto-labs/api'
|
||||
|
||||
import {useGate} from '#/lib/statsig/statsig'
|
||||
import {isWeb} from '#/platform/detection'
|
||||
import {MessagesEventBus} from '#/state/messages/events/agent'
|
||||
import {MessagesEventBusState} from '#/state/messages/events/types'
|
||||
|
@ -20,7 +21,7 @@ export function useMessagesEventBus() {
|
|||
return ctx
|
||||
}
|
||||
|
||||
export function MessagesEventBusProvider({
|
||||
export function Temp_MessagesEventBusProvider({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
|
@ -65,3 +66,18 @@ export function MessagesEventBusProvider({
|
|||
</MessagesEventBusContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function MessagesEventBusProvider({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const gate = useGate()
|
||||
const {serviceUrl} = useDmServiceUrlStorage()
|
||||
if (gate('dms') && serviceUrl) {
|
||||
return (
|
||||
<Temp_MessagesEventBusProvider>{children}</Temp_MessagesEventBusProvider>
|
||||
)
|
||||
}
|
||||
return children
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import {BskyAgent} from '@atproto-labs/api'
|
|||
import {useFocusEffect, useIsFocused} from '@react-navigation/native'
|
||||
|
||||
import {Convo, ConvoParams, ConvoState} from '#/state/messages/convo'
|
||||
import {CurrentConvoIdProvider} from '#/state/messages/current-convo-id'
|
||||
import {MessagesEventBusProvider} from '#/state/messages/events'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useDmServiceUrlStorage} from '#/screens/Messages/Temp/useDmServiceUrlStorage'
|
||||
|
||||
|
@ -66,3 +68,11 @@ export function ChatProvider({
|
|||
|
||||
return <ChatContext.Provider value={service}>{children}</ChatContext.Provider>
|
||||
}
|
||||
|
||||
export function MessagesProvider({children}: {children: React.ReactNode}) {
|
||||
return (
|
||||
<CurrentConvoIdProvider>
|
||||
<MessagesEventBusProvider>{children}</MessagesEventBusProvider>
|
||||
</CurrentConvoIdProvider>
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue