Skip to content

Commit

Permalink
[🐴] Integrate global event bus (#3904)
Browse files Browse the repository at this point in the history
* Conditionally run global event bus

* Add current convo id context, bundle providers
  • Loading branch information
estrattonbailey authored May 8, 2024
1 parent 37f22ca commit 165fdb7
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 31 deletions.
33 changes: 18 additions & 15 deletions src/App.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {useQueryClient} from '@tanstack/react-query'

import {Provider as StatsigProvider} from '#/lib/statsig/statsig'
import {logger} from '#/logger'
import {MessagesProvider} from '#/state/messages'
import {init as initPersistedState} from '#/state/persisted'
import {Provider as LabelDefsProvider} from '#/state/preferences/label-defs'
import {Provider as ModerationOptsProvider} from '#/state/preferences/moderation-opts'
Expand Down Expand Up @@ -97,21 +98,23 @@ function InnerApp() {
<QueryProvider currentDid={currentAccount?.did}>
<PushNotificationsListener>
<StatsigProvider>
{/* LabelDefsProvider MUST come before ModerationOptsProvider */}
<LabelDefsProvider>
<ModerationOptsProvider>
<LoggedOutViewProvider>
<SelectedFeedProvider>
<UnreadNotifsProvider>
<GestureHandlerRootView style={s.h100pct}>
<TestCtrls />
<Shell />
</GestureHandlerRootView>
</UnreadNotifsProvider>
</SelectedFeedProvider>
</LoggedOutViewProvider>
</ModerationOptsProvider>
</LabelDefsProvider>
<MessagesProvider>
{/* LabelDefsProvider MUST come before ModerationOptsProvider */}
<LabelDefsProvider>
<ModerationOptsProvider>
<LoggedOutViewProvider>
<SelectedFeedProvider>
<UnreadNotifsProvider>
<GestureHandlerRootView style={s.h100pct}>
<TestCtrls />
<Shell />
</GestureHandlerRootView>
</UnreadNotifsProvider>
</SelectedFeedProvider>
</LoggedOutViewProvider>
</ModerationOptsProvider>
</LabelDefsProvider>
</MessagesProvider>
</StatsigProvider>
</PushNotificationsListener>
</QueryProvider>
Expand Down
31 changes: 17 additions & 14 deletions src/App.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {useLingui} from '@lingui/react'

import {Provider as StatsigProvider} from '#/lib/statsig/statsig'
import {logger} from '#/logger'
import {MessagesProvider} from '#/state/messages'
import {init as initPersistedState} from '#/state/persisted'
import {Provider as LabelDefsProvider} from '#/state/preferences/label-defs'
import {Provider as ModerationOptsProvider} from '#/state/preferences/moderation-opts'
Expand Down Expand Up @@ -84,20 +85,22 @@ function InnerApp() {
key={currentAccount?.did}>
<QueryProvider currentDid={currentAccount?.did}>
<StatsigProvider>
{/* LabelDefsProvider MUST come before ModerationOptsProvider */}
<LabelDefsProvider>
<ModerationOptsProvider>
<LoggedOutViewProvider>
<SelectedFeedProvider>
<UnreadNotifsProvider>
<SafeAreaProvider>
<Shell />
</SafeAreaProvider>
</UnreadNotifsProvider>
</SelectedFeedProvider>
</LoggedOutViewProvider>
</ModerationOptsProvider>
</LabelDefsProvider>
<MessagesProvider>
{/* LabelDefsProvider MUST come before ModerationOptsProvider */}
<LabelDefsProvider>
<ModerationOptsProvider>
<LoggedOutViewProvider>
<SelectedFeedProvider>
<UnreadNotifsProvider>
<SafeAreaProvider>
<Shell />
</SafeAreaProvider>
</UnreadNotifsProvider>
</SelectedFeedProvider>
</LoggedOutViewProvider>
</ModerationOptsProvider>
</LabelDefsProvider>
</MessagesProvider>
</StatsigProvider>
</QueryProvider>
</React.Fragment>
Expand Down
13 changes: 12 additions & 1 deletion src/screens/Messages/Conversation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {AppBskyActorDefs} from '@atproto/api'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useNavigation} from '@react-navigation/native'
import {useFocusEffect, useNavigation} from '@react-navigation/native'
import {NativeStackScreenProps} from '@react-navigation/native-stack'

import {CommonNavigatorParams, NavigationProp} from '#/lib/routes/types'
import {useGate} from '#/lib/statsig/statsig'
import {useCurrentConvoId} from '#/state/messages/current-convo-id'
import {BACK_HITSLOP} from 'lib/constants'
import {isWeb} from 'platform/detection'
import {ChatProvider, useChat} from 'state/messages'
Expand All @@ -31,6 +32,16 @@ type Props = NativeStackScreenProps<
export function MessagesConversationScreen({route}: Props) {
const gate = useGate()
const convoId = route.params.conversation
const {setCurrentConvoId} = useCurrentConvoId()

useFocusEffect(
useCallback(() => {
setCurrentConvoId(convoId)
return () => {
setCurrentConvoId(undefined)
}
}, [convoId, setCurrentConvoId]),
)

if (!gate('dms')) return <ClipClopGate />

Expand Down
38 changes: 38 additions & 0 deletions src/state/messages/current-convo-id.tsx
Original file line number Diff line number Diff line change
@@ -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>
)
}
18 changes: 17 additions & 1 deletion src/state/messages/events/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -20,7 +21,7 @@ export function useMessagesEventBus() {
return ctx
}

export function MessagesEventBusProvider({
export function Temp_MessagesEventBusProvider({
children,
}: {
children: React.ReactNode
Expand Down Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions src/state/messages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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>
)
}

0 comments on commit 165fdb7

Please sign in to comment.