Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @baseapp-frontend/components

## 1.3.0

### Minor Changes

- Implements native version for `useRoomListSubscription` hook to subscribe and unsubscribe based on the application's state — e.g., when the app goes inactive and then returns to the foreground

## 1.2.9

### Patch Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,58 @@ export const RoomListSubscriptionQuery = graphql`
}
`

export const wasRemovedFromChatRoom = (
data: useRoomListSubscription$data | null | undefined,
profileId: string,
) =>
data?.chatRoomOnRoomUpdate?.removedParticipants?.some((node) => node?.profile?.id === profileId)

export const getRoomListSubscriptionConfig = (
profileId: string,
connections: string[],
selectedRoom: string | undefined,
resetChatRoom: VoidFunction,
onRemoval?: VoidFunction | undefined,
) => ({
subscription: RoomListSubscriptionQuery,
onError: console.error,
variables: { profileId, connections },
updater: (
store: RecordSourceSelectorProxy<unknown>,
data: useRoomListSubscription$data | null | undefined,
) => {
const roomId = data?.chatRoomOnRoomUpdate?.room?.node?.id
if (!roomId) return
if (wasRemovedFromChatRoom(data, profileId)) {
getChatRoomConnections(store, profileId).forEach((connectionRecord) =>
ConnectionHandler.deleteNode(connectionRecord, roomId),
)
} else {
const isArchived = data?.chatRoomOnRoomUpdate?.room?.node?.isArchived
getChatRoomConnections(
store,
profileId,
({ q, archived }) => q === '' && archived === isArchived,
).forEach((connectionRecord) => {
ConnectionHandler.deleteNode(connectionRecord, roomId)
const serverEdge = store.getRootField('chatRoomOnRoomUpdate')?.getLinkedRecord('room')
const edge = ConnectionHandler.buildConnectionEdge(store, connectionRecord, serverEdge)
if (edge) {
ConnectionHandler.insertEdgeBefore(connectionRecord, edge)
}
})
}
},
onNext: (data: useRoomListSubscription$data | null | undefined) => {
if (wasRemovedFromChatRoom(data, profileId)) {
if (selectedRoom && data?.chatRoomOnRoomUpdate?.room?.node?.id === selectedRoom) {
resetChatRoom()
}
onRemoval?.()
}
},
})

export const useRoomListSubscription = ({
connections,
profileId,
Expand All @@ -50,52 +102,11 @@ export const useRoomListSubscription = ({
}) => {
const { id: selectedRoom, resetChatRoom } = useChatRoom()

const config = useMemo(() => {
const wasRemovedFromChatRoom = (data: useRoomListSubscription$data | null | undefined) =>
data?.chatRoomOnRoomUpdate?.removedParticipants?.some(
(node) => node?.profile?.id === profileId,
)

return {
subscription: RoomListSubscriptionQuery,
onError: console.error,
variables: { profileId, connections },
updater: (
store: RecordSourceSelectorProxy<unknown>,
data: useRoomListSubscription$data | null | undefined,
) => {
const roomId = data?.chatRoomOnRoomUpdate?.room?.node?.id
if (!roomId) return
if (wasRemovedFromChatRoom(data)) {
getChatRoomConnections(store, profileId).forEach((connectionRecord) =>
ConnectionHandler.deleteNode(connectionRecord, roomId),
)
} else {
const isArchived = data?.chatRoomOnRoomUpdate?.room?.node?.isArchived
getChatRoomConnections(
store,
profileId,
({ q, archived }) => q === '' && archived === isArchived,
).forEach((connectionRecord) => {
ConnectionHandler.deleteNode(connectionRecord, roomId)
const serverEdge = store.getRootField('chatRoomOnRoomUpdate')?.getLinkedRecord('room')
const edge = ConnectionHandler.buildConnectionEdge(store, connectionRecord, serverEdge)
if (edge) {
ConnectionHandler.insertEdgeBefore(connectionRecord, edge)
}
})
}
},
onNext: (data: useRoomListSubscription$data | null | undefined) => {
if (wasRemovedFromChatRoom(data)) {
if (selectedRoom && data?.chatRoomOnRoomUpdate?.room?.node?.id === selectedRoom) {
resetChatRoom()
}
onRemoval?.()
}
},
}
}, [profileId, connections, onRemoval, selectedRoom, resetChatRoom])
const config = useMemo(
() =>
getRoomListSubscriptionConfig(profileId, connections, selectedRoom, resetChatRoom, onRemoval),
[profileId, connections, onRemoval, selectedRoom, resetChatRoom],
)

return useSubscription<useRoomListSubscriptionType>(config)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use-client'

import { useCallback, useMemo, useRef } from 'react'

import { useAppStateSubscription } from '@baseapp-frontend/utils/hooks/useAppStateSubscription'

import { useFocusEffect } from 'expo-router'
import { Disposable, Environment, requestSubscription } from 'react-relay'
import { GraphQLSubscriptionConfig } from 'relay-runtime'

import { useRoomListSubscription as useRoomListSubscriptionType } from '../../../../../__generated__/useRoomListSubscription.graphql'
import { getRoomListSubscriptionConfig } from '../../../common'
import useChatRoom from '../../../common/context/useChatRoom'

export const useRoomListSubscription = ({
connections,
profileId,
onRemoval,
environment,
}: {
connections: string[]
profileId: string
environment: Environment
onRemoval?: VoidFunction
isRemoval?: boolean
}) => {
const { id: selectedRoom, resetChatRoom } = useChatRoom()
const disposableRef = useRef<Disposable | null>(null)

const config = useMemo<GraphQLSubscriptionConfig<useRoomListSubscriptionType>>(
() =>
getRoomListSubscriptionConfig(profileId, connections, selectedRoom, resetChatRoom, onRemoval),
[profileId, connections, onRemoval, selectedRoom, resetChatRoom],
)

const subscribe = useCallback(() => {
if (!profileId) return
disposableRef.current?.dispose?.()
disposableRef.current = requestSubscription(environment, config)
}, [profileId, environment, config])

const unsubscribe = useCallback(() => {
disposableRef.current?.dispose?.()
disposableRef.current = null
}, [])

useFocusEffect(
useCallback(() => {
subscribe()
return () => {
unsubscribe()
}
}, [subscribe, unsubscribe]),
)

useAppStateSubscription(() => {
subscribe()
})
}
1 change: 1 addition & 0 deletions packages/components/modules/messages/native/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// export native messages components
export * from './graphql/subscriptions/useMessagesListSubscription'
export * from './graphql/subscriptions/useRoomListSubscription'
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@baseapp-frontend/components",
"description": "BaseApp components modules such as comments, notifications, messages, and more.",
"version": "1.2.9",
"version": "1.3.0",
"sideEffects": false,
"scripts": {
"babel:transpile": "babel modules -d tmp-babel --extensions .ts,.tsx --ignore '**/__tests__/**','**/__storybook__/**'",
Expand Down
47 changes: 24 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading