11import { useEffect , useRef , useState } from 'react' ;
22import { getMessaging , AuthorizationStatus } from '@react-native-firebase/messaging' ;
3- import notifee from '@notifee/react-native' ;
43import { USER_TOKENS , USERS } from '../ChatUsers' ;
54import AsyncStore from '../utils/AsyncStore' ;
65
@@ -28,7 +27,7 @@ const requestAndroidPermission = async () => {
2827} ;
2928
3029export const useChatClient = ( ) => {
31- const [ chatClient , setChatClient ] = useState < StreamChat | null > ( null ) ;
30+ const [ chatClient , setChatClient ] = useState < null > ( null ) ;
3231 const [ isConnecting , setIsConnecting ] = useState ( true ) ;
3332
3433 const unsubscribePushListenersRef = useRef < ( ) => void > ( undefined ) ;
@@ -40,19 +39,6 @@ export const useChatClient = () => {
4039 const loginUser = async ( config : LoginConfig ) => {
4140 // unsubscribe from previous push listeners
4241 unsubscribePushListenersRef . current ?.( ) ;
43- const client = StreamChat . getInstance ( config . apiKey , {
44- timeout : 6000 ,
45- // logger: (type, msg) => console.log(type, msg)
46- } ) ;
47- setChatClient ( client ) ;
48-
49- const user = {
50- id : config . userId ,
51- image : config . userImage ,
52- name : config . userName ,
53- } ;
54- await client . connectUser ( user , config . userToken ) ;
55- await AsyncStore . setItem ( '@stream-rn-sampleapp-login-config' , config ) ;
5642
5743 const permissionAuthStatus = await messaging . hasPermission ( ) ;
5844 let isEnabled =
@@ -82,13 +68,11 @@ export const useChatClient = () => {
8268 ? providerNameOverride
8369 : ( provider ?. name ?? 'rn-fcm' ) ;
8470 const token = id === 'firebase' ? firebaseToken : ( apnsToken ?? firebaseToken ) ;
85- await client . addDevice ( token , id as PushProvider , client . userID , name ) ;
8671
8772 // Listen to new FCM tokens and register them with stream chat server.
8873 const unsubscribeTokenRefresh = messaging . onTokenRefresh ( async ( newFirebaseToken ) => {
8974 const newApnsToken = await messaging . getAPNSToken ( ) ;
9075 const newToken = id === 'firebase' ? newFirebaseToken : ( newApnsToken ?? firebaseToken ) ;
91- await client . addDevice ( newToken , id as PushProvider , client . userID , name ) ;
9276 } ) ;
9377 // show notifications when on foreground
9478 const unsubscribeForegroundMessageReceive = messaging . onMessage ( async ( remoteMessage ) => {
@@ -97,64 +81,28 @@ export const useChatClient = () => {
9781 ...rest ,
9882 ...( ( stream as unknown as Record < string , string > | undefined ) ?? { } ) , // extract and merge stream object if present
9983 } ;
100- const channelId = await notifee . createChannel ( {
101- id : 'foreground' ,
102- name : 'Foreground Messages' ,
103- } ) ;
10484 // create the android channel to send the notification to
10585 // display the notification on foreground
10686 const notification = remoteMessage . notification ?? { } ;
10787 const body = ( data . body ?? notification . body ) as string ;
10888 const title = ( data . title ?? notification . title ) as string ;
10989
110- if ( body && title ) {
111- await notifee . displayNotification ( {
112- android : {
113- channelId,
114- pressAction : {
115- id : 'default' ,
116- } ,
117- } ,
118- body,
119- title,
120- data,
121- } ) ;
122- }
12390 } ) ;
12491
12592 unsubscribePushListenersRef . current = ( ) => {
12693 unsubscribeTokenRefresh ( ) ;
12794 unsubscribeForegroundMessageReceive ( ) ;
12895 } ;
12996 }
130- setChatClient ( client ) ;
13197 } ;
13298
13399 const switchUser = async ( userId ?: string ) => {
134- if ( chatClient ?. userID ) {
135- return ;
136- }
137100
138101 setIsConnecting ( true ) ;
139102
140103 try {
141104 if ( userId ) {
142- await loginUser ( {
143- apiKey : 'yjrt5yxw77ev' ,
144- userId : USERS [ userId ] . id ,
145- userImage : USERS [ userId ] . image ,
146- userName : USERS [ userId ] . name ,
147- userToken : USER_TOKENS [ userId ] ,
148- } ) ;
149105 } else {
150- const config = await AsyncStore . getItem < LoginConfig | null > (
151- '@stream-rn-sampleapp-login-config' ,
152- null ,
153- ) ;
154-
155- if ( config ) {
156- await loginUser ( config ) ;
157- }
158106 }
159107 } catch ( e ) {
160108 console . warn ( e ) ;
@@ -164,8 +112,6 @@ export const useChatClient = () => {
164112
165113 const logout = async ( ) => {
166114 setChatClient ( null ) ;
167- chatClient ?. disconnectUser ( ) ;
168- await AsyncStore . removeItem ( '@stream-rn-sampleapp-login-config' ) ;
169115 } ;
170116
171117 useEffect ( ( ) => {
@@ -175,7 +121,6 @@ export const useChatClient = () => {
175121 } ;
176122 run ( ) ;
177123 return unsubscribePushListenersRef . current ;
178- // eslint-disable-next-line react-hooks/exhaustive-deps
179124 } , [ ] ) ;
180125
181126 return {
0 commit comments