1+ import {
2+ collection ,
3+ doc ,
4+ getDoc ,
5+ getDocs ,
6+ getFirestore ,
7+ onSnapshot ,
8+ orderBy ,
9+ query ,
10+ serverTimestamp ,
11+ setDoc ,
12+ where ,
13+ } from 'firebase/firestore' ;
114import React from 'react' ;
215
316import { fetchUser , processRoomsQuery , ROOMS_COLLECTION_NAME } from '../common/utils' ;
@@ -17,26 +30,28 @@ import { useFirebaseUser } from './useFirebaseUser';
1730export const useRooms = ( orderByUpdatedAt ?: boolean ) => {
1831 const [ rooms , setRooms ] = React . useState < IRoom [ ] > ( [ ] )
1932 const { firebaseUser } = useFirebaseUser ( )
33+ const db = getFirestore ( ) ;
2034
2135 React . useEffect ( ( ) => {
2236 if ( ! firebaseUser ) {
2337 setRooms ( [ ] )
2438 return
2539 }
2640
27- const collection = orderByUpdatedAt
28- ? firestore ( )
29- . collection ( ROOMS_COLLECTION_NAME )
30- . where ( 'userIds' , 'array-contains' , firebaseUser . uid )
31- . orderBy ( 'updatedAt' , 'desc' )
32- : firestore ( )
33- . collection ( ROOMS_COLLECTION_NAME )
34- . where ( 'userIds' , 'array-contains' , firebaseUser . uid )
41+ let _collection ;
3542
36- return collection . onSnapshot ( async ( query ) => {
37- const newRooms = await processRoomsQuery ( { firebaseUser, query } )
43+ if ( orderByUpdatedAt ) {
44+ const col = collection ( db , ROOMS_COLLECTION_NAME )
45+ _collection = query ( col , where ( 'userIds' , 'array-contains' , firebaseUser . uid ) , orderBy ( 'updatedAt' , 'desc' ) )
46+ } else {
47+ const col = collection ( db , ROOMS_COLLECTION_NAME )
48+ _collection = query ( col , where ( 'userIds' , 'array-contains' , firebaseUser . uid ) )
49+ }
50+
51+ return onSnapshot ( _collection , async ( _query : any ) => {
52+ const newRooms = await processRoomsQuery ( { firebaseUser, query : _query } ) ;
3853
39- setRooms ( newRooms )
54+ setRooms ( newRooms ) ;
4055 } )
4156 } , [ firebaseUser , orderByUpdatedAt ] )
4257
@@ -61,21 +76,23 @@ export const useRooms = (orderByUpdatedAt?: boolean) => {
6176
6277 const roomUsers = [ currentUser ] . concat ( users )
6378
64- const room = await firestore ( )
65- . collection ( ROOMS_COLLECTION_NAME )
66- . add ( {
67- createdAt : firestore . FieldValue . serverTimestamp ( ) ,
79+ const _collection = collection ( db , ROOMS_COLLECTION_NAME ) ;
80+ const _docRef = doc ( _collection ) ;
81+ await setDoc ( _docRef , {
82+ createdAt : serverTimestamp ( ) ,
6883 imageUrl,
6984 metadata,
7085 name,
7186 type : 'group' ,
72- updatedAt : firestore . FieldValue . serverTimestamp ( ) ,
87+ updatedAt : serverTimestamp ( ) ,
7388 userIds : roomUsers . map ( ( u ) => u . id ) ,
7489 userRoles : roomUsers . reduce (
7590 ( prev , curr ) => ( { ...prev , [ curr . id ] : curr . role } ) ,
7691 { }
7792 ) ,
78- } )
93+ } ) ;
94+
95+ const room = await getDoc ( _docRef ) ;
7996
8097 return {
8198 id : room . id ,
@@ -94,12 +111,11 @@ export const useRooms = (orderByUpdatedAt?: boolean) => {
94111 ) => {
95112 if ( ! firebaseUser ) return
96113
97- const query = await firestore ( )
98- . collection ( ROOMS_COLLECTION_NAME )
99- . where ( 'userIds' , 'array-contains' , firebaseUser . uid )
100- . get ( )
114+ const _collection = collection ( db , ROOMS_COLLECTION_NAME ) ;
115+ const _query = query ( _collection , where ( 'userIds' , 'array-contains' , firebaseUser . uid ) ) ;
116+ const _docs = await getDocs ( _query ) ;
101117
102- const allRooms = await processRoomsQuery ( { firebaseUser, query } )
118+ const allRooms = await processRoomsQuery ( { firebaseUser, query : _docs } ) ;
103119
104120 const existingRoom = allRooms . find ( ( room ) => {
105121 if ( room . type === 'group' ) return false
@@ -118,18 +134,20 @@ export const useRooms = (orderByUpdatedAt?: boolean) => {
118134
119135 const users = [ currentUser ] . concat ( otherUser )
120136
121- const room = await firestore ( )
122- . collection ( ROOMS_COLLECTION_NAME )
123- . add ( {
124- createdAt : firestore . FieldValue . serverTimestamp ( ) ,
125- imageUrl : undefined ,
126- metadata,
127- name : undefined ,
128- type : 'direct' ,
129- updatedAt : firestore . FieldValue . serverTimestamp ( ) ,
130- userIds : users . map ( ( u ) => u . id ) ,
131- userRoles : undefined ,
132- } )
137+
138+ const _docRef = doc ( _collection ) ;
139+ await setDoc ( _docRef , {
140+ createdAt : serverTimestamp ( ) ,
141+ imageUrl : undefined ,
142+ metadata,
143+ name : undefined ,
144+ type : 'direct' ,
145+ updatedAt : serverTimestamp ( ) ,
146+ userIds : users . map ( ( u ) => u . id ) ,
147+ userRoles : undefined ,
148+ } )
149+
150+ const room = await getDoc ( _docRef ) ;
133151
134152 return {
135153 id : room . id ,
0 commit comments