1- import firestore from '@react-native-firebase/firestore' ;
2- import * as React from 'react' ;
1+ import {
2+ collection ,
3+ doc ,
4+ Firestore ,
5+ onSnapshot ,
6+ orderBy ,
7+ query ,
8+ serverTimestamp ,
9+ setDoc ,
10+ updateDoc ,
11+ } from 'firebase/firestore' ;
12+ import React from 'react' ;
313
414import { IRoom , MessageType } from './types' ;
515import { useFirebaseUser } from './useFirebaseUser' ;
616
717/** Returns a stream of messages from Firebase for a given room */
8- export const useMessages = ( room : IRoom ) => {
18+ export const useMessages = ( room : IRoom , db : Firestore ) => {
919 const [ messages , setMessages ] = React . useState < MessageType . Any [ ] > ( [ ] )
1020 const { firebaseUser } = useFirebaseUser ( )
1121
1222 React . useEffect ( ( ) => {
13- return firestore ( )
14- . collection ( `rooms/${ room . id } /messages` )
15- . orderBy ( 'createdAt' , 'desc' )
16- . onSnapshot ( ( query ) => {
17- const newMessages : MessageType . Any [ ] = [ ]
18-
19- query . forEach ( ( doc ) => {
20- // Ignore `authorId`, `createdAt` and `updatedAt` types here, not provided by the Firebase library
21- // type-coverage:ignore-next-line
22- const { authorId, createdAt, updatedAt, ...rest } = doc . data ( )
23+ const _collection = collection ( db , `rooms/${ room . id } /messages` ) ;
24+ const _query = query ( _collection , orderBy ( 'createdAt' , 'desc' ) ) ;
25+ const _onSnapshot = onSnapshot ( _query , ( query ) => {
26+ const newMessages : MessageType . Any [ ] = [ ]
27+
28+ query . forEach ( ( doc ) => {
29+ // Ignore `authorId`, `createdAt` and `updatedAt` types here, not provided by the Firebase library
30+ // type-coverage:ignore-next-line
31+ const { authorId, createdAt, updatedAt, ...rest } = doc . data ( )
32+
33+ // type-coverage:ignore-next-line
34+ const author = room . users . find ( ( u ) => u . id === authorId ) ?? {
35+ id : authorId as string ,
36+ }
2337
38+ newMessages . push ( {
39+ ...rest ,
40+ author,
2441 // type-coverage:ignore-next-line
25- const author = room . users . find ( ( u ) => u . id === authorId ) ?? {
26- id : authorId as string ,
27- }
28-
29- newMessages . push ( {
30- ...rest ,
31- author,
32- // type-coverage:ignore-next-line
33- createdAt : createdAt ?. toMillis ( ) ?? undefined ,
34- id : doc . id ,
35- // type-coverage:ignore-next-line
36- updatedAt : updatedAt ?. toMillis ( ) ?? undefined ,
37- } as MessageType . Any )
38- } )
39-
40- setMessages ( newMessages )
42+ createdAt : createdAt ?. toMillis ( ) ?? undefined ,
43+ id : doc . id ,
44+ // type-coverage:ignore-next-line
45+ updatedAt : updatedAt ?. toMillis ( ) ?? undefined ,
46+ } as MessageType . Any )
4147 } )
48+
49+ setMessages ( newMessages )
50+ } )
51+
52+ return _onSnapshot
4253 } , [ room . id , room . users ] )
4354
4455 /** Sends a message to the Firestore. Accepts any partial message. */
4556 const sendMessage = async ( message : MessageType . PartialAny ) => {
4657 if ( ! firebaseUser ) return
4758
48- await firestore ( )
49- . collection ( `rooms/ ${ room . id } /messages` )
50- . add ( {
51- ...message ,
52- authorId : firebaseUser . uid ,
53- createdAt : firestore . FieldValue . serverTimestamp ( ) ,
54- updatedAt : firestore . FieldValue . serverTimestamp ( ) ,
55- } )
59+ const _collection = collection ( db , `rooms/ ${ room . id } /messages` ) ;
60+ const _doc = doc ( _collection ) ;
61+ await setDoc ( _doc , {
62+ ...message ,
63+ authorId : firebaseUser . uid ,
64+ createdAt : serverTimestamp ( ) ,
65+ updatedAt : serverTimestamp ( ) ,
66+ } )
5667 }
5768
5869 /** Updates a message in the Firestore. Accepts any message.
@@ -68,14 +79,14 @@ export const useMessages = (room: IRoom) => {
6879 delete messageToSend . createdAt
6980 delete messageToSend . id
7081
71- await firestore ( )
72- . collection ( `rooms/ ${ room . id } /messages` )
73- . doc ( message . id )
74- . update ( {
75- ...messageToSend ,
76- authorId : message . author . id ,
77- updatedAt : firestore . FieldValue . serverTimestamp ( ) ,
78- } )
82+ const _collection = collection ( db , `rooms/ ${ room . id } /messages` ) ;
83+ const _doc = doc ( _collection , message . id ) ;
84+
85+ await updateDoc ( _doc , {
86+ ...messageToSend ,
87+ authorId : message . author . id ,
88+ updatedAt : serverTimestamp ( ) ,
89+ } )
7990 }
8091
8192 return { messages, sendMessage, updateMessage }
0 commit comments