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
8 changes: 8 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @baseapp-frontend/components

## 1.2.6

- Implements a new hook: `useMessagesListSubscriptionOnRn`
- Handles real-time message updates via GraphQL subscription (chatRoomOnMessage)
- Automatically subscribes/unsubscribes on screen focus using expo useFocusEffect. Ensures clean disposal of the subscription to avoid memory leaks or duplicate events
- Uses Relay's ConnectionHandler.getConnectionID and append new messages to the proper connection

## 1.2.5

### Patch Changes
Expand All @@ -8,6 +15,7 @@
- @baseapp-frontend/design-system@1.0.21
- @baseapp-frontend/authentication@5.0.1


## 1.2.4

### Minor Changes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { useMemo } from 'react'
import { useCallback, useMemo, useRef } from 'react'

import { ConnectionHandler, graphql, useSubscription } from 'react-relay'
import { useFocusEffect } from 'expo-router'
import {
ConnectionHandler,
Disposable,
Environment,
graphql,
requestSubscription,
useSubscription,
} from 'react-relay'

export const newMessageSubscription = graphql`
subscription useMessagesListSubscription($roomId: ID!, $profileId: ID!, $connections: [ID!]!) {
Expand Down Expand Up @@ -33,3 +41,43 @@ export const useMessagesListSubscription = (roomId: string, profileId: string) =

return useSubscription(config)
}

export const useMessagesListSubscriptionOnRn = (
roomId: string,
profileId: string,
environment: Environment,
) => {
const disposableRef = useRef<Disposable | null>(null)
const connectionID = useMemo(
() => ConnectionHandler.getConnectionID(roomId, 'chatRoom_allMessages'),
[roomId],
)

const config = useMemo(
() => ({
subscription: newMessageSubscription,
variables: {
roomId,
profileId,
connections: [connectionID],
},
onError: console.error,
}),
[roomId, profileId, connectionID],
)

// Subscribe to the messages list subscription when the component is focused
// and clean up the subscription when the component is unfocused
useFocusEffect(
useCallback(() => {
if (!roomId || !profileId) return undefined

disposableRef.current = requestSubscription(environment, config)

return () => {
disposableRef.current?.dispose?.()
disposableRef.current = null
}
}, [config, environment]),
)
}
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.5",
"version": "1.2.6",
"sideEffects": false,
"scripts": {
"babel:transpile": "babel modules -d tmp-babel --extensions .ts,.tsx --ignore '**/__tests__/**','**/__storybook__/**'",
Expand Down
Loading