Skip to content

Commit ba018f5

Browse files
authored
fix: useMessagesListSubscription hook (#276)
- Abstract `useMessagesListSubscriptionOnRn` hook to native interface so it doesn't break web version due to import problems. In this case: expo-router won't be part of web applications so it will break when importing through the previous path. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Improved chat room subscription reliability on native platforms. * **Bug Fixes** * Resolved issues causing message subscription failures on the web version. * **Chores** * Updated package version to 1.2.7. * Updated changelog to reflect latest changes. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 91101a9 commit ba018f5

File tree

5 files changed

+57
-52
lines changed

5 files changed

+57
-52
lines changed

packages/components/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @baseapp-frontend/components
22

3+
## 1.2.7
4+
5+
### Minor Changes
6+
7+
- Abstract `useMessagesListSubscriptionOnRn` hook to native interface so it doesn't break web version due to import problems.
8+
39
## 1.2.6
410

511
- Implements a new hook: `useMessagesListSubscriptionOnRn`
@@ -15,7 +21,6 @@
1521
- @baseapp-frontend/design-system@1.0.21
1622
- @baseapp-frontend/authentication@5.0.1
1723

18-
1924
## 1.2.4
2025

2126
### Minor Changes
Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import { useCallback, useMemo, useRef } from 'react'
1+
import { useMemo } from 'react'
22

3-
import { useFocusEffect } from 'expo-router'
4-
import {
5-
ConnectionHandler,
6-
Disposable,
7-
Environment,
8-
graphql,
9-
requestSubscription,
10-
useSubscription,
11-
} from 'react-relay'
3+
import { ConnectionHandler, graphql, useSubscription } from 'react-relay'
124

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

4234
return useSubscription(config)
4335
}
44-
45-
export const useMessagesListSubscriptionOnRn = (
46-
roomId: string,
47-
profileId: string,
48-
environment: Environment,
49-
) => {
50-
const disposableRef = useRef<Disposable | null>(null)
51-
const connectionID = useMemo(
52-
() => ConnectionHandler.getConnectionID(roomId, 'chatRoom_allMessages'),
53-
[roomId],
54-
)
55-
56-
const config = useMemo(
57-
() => ({
58-
subscription: newMessageSubscription,
59-
variables: {
60-
roomId,
61-
profileId,
62-
connections: [connectionID],
63-
},
64-
onError: console.error,
65-
}),
66-
[roomId, profileId, connectionID],
67-
)
68-
69-
// Subscribe to the messages list subscription when the component is focused
70-
// and clean up the subscription when the component is unfocused
71-
useFocusEffect(
72-
useCallback(() => {
73-
if (!roomId || !profileId) return undefined
74-
75-
disposableRef.current = requestSubscription(environment, config)
76-
77-
return () => {
78-
disposableRef.current?.dispose?.()
79-
disposableRef.current = null
80-
}
81-
}, [config, environment]),
82-
)
83-
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { useCallback, useMemo, useRef } from 'react'
2+
3+
import { useFocusEffect } from 'expo-router'
4+
import { ConnectionHandler, Disposable, Environment, requestSubscription } from 'react-relay'
5+
6+
import { newMessageSubscription } from '../../../common'
7+
8+
export const useMessagesListSubscription = (
9+
roomId: string,
10+
profileId: string,
11+
environment: Environment,
12+
) => {
13+
const disposableRef = useRef<Disposable | null>(null)
14+
const connectionID = useMemo(
15+
() => ConnectionHandler.getConnectionID(roomId, 'chatRoom_allMessages'),
16+
[roomId],
17+
)
18+
19+
const config = useMemo(
20+
() => ({
21+
subscription: newMessageSubscription,
22+
variables: {
23+
roomId,
24+
profileId,
25+
connections: [connectionID],
26+
},
27+
onError: console.error,
28+
}),
29+
[roomId, profileId, connectionID],
30+
)
31+
32+
// Subscribe to the messages list subscription when the component is focused
33+
// and clean up the subscription when the component is unfocused
34+
useFocusEffect(
35+
useCallback(() => {
36+
if (!roomId || !profileId) return undefined
37+
38+
disposableRef.current = requestSubscription(environment, config)
39+
40+
return () => {
41+
disposableRef.current?.dispose?.()
42+
disposableRef.current = null
43+
}
44+
}, [config, environment]),
45+
)
46+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// export native messages components
2+
export * from './graphql/subscriptions/useMessagesListSubscription'

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@baseapp-frontend/components",
33
"description": "BaseApp components modules such as comments, notifications, messages, and more.",
4-
"version": "1.2.6",
4+
"version": "1.2.7",
55
"sideEffects": false,
66
"scripts": {
77
"babel:transpile": "babel modules -d tmp-babel --extensions .ts,.tsx --ignore '**/__tests__/**','**/__storybook__/**'",

0 commit comments

Comments
 (0)