11import { query , where , onSnapshot , collection , doc , updateDoc , arrayUnion , getDoc , serverTimestamp } from "firebase/firestore" ;
22import { createContext , useContext , useState , useEffect } from "react" ;
33import { db } from "../config/firebase" ;
4- import { useLoggedInUserInfo } from "../hooks/userHandler" ;
4+ import { useLoggedInUserInfo , useUserHandler } from "../hooks/userHandler" ;
55import { useAuth } from "./authContext" ;
66
77const ChatContext = createContext ( )
@@ -14,6 +14,7 @@ export function ChatProvider({ children }) {
1414
1515 const { currentUser } = useAuth ( )
1616 const { permanentUsernameOfLoggedInUser, userIdOfLoggedInUser } = useLoggedInUserInfo ( )
17+ const { getUser_Pfp_from_Username_Displayname } = useUserHandler ( )
1718 const chatCollectionRef = collection ( db , "chats" ) ;
1819 const usersCollectionRef = collection ( db , "users" ) ;
1920 const conversationsCollectionRef = collection ( db , "conversations" ) ;
@@ -68,54 +69,68 @@ export function ChatProvider({ children }) {
6869 var listOfDocs = [ ] ;
6970 var unsubscribe ;
7071 try {
71- const queryUser = query ( usersCollectionRef , where ( "userId" , "==" , currentUser . uid ) )
72+ const queryUser = query (
73+ usersCollectionRef ,
74+ where ( "userId" , "==" , currentUser . uid )
75+ ) ;
7276
73- unsubscribe = onSnapshot ( queryUser , ( snapshot ) => {
74- snapshot . forEach ( ( doc ) => {
75- const data = doc . data ( ) ;
76- const userChats = data . conversationList
77+ unsubscribe = onSnapshot ( queryUser , ( snapshot ) => {
78+ snapshot . forEach ( ( doc ) => {
79+ const userChats = doc . data ( ) . conversationList ;
80+ // const userChats = data.conversationList
7781 const id = doc . id ;
78- listOfDocs . push ( ...userChats ) ;
79- console . log ( userChats )
80- } )
81- setConversationList ( listOfDocs )
82- listOfDocs = [ ] ;
83- } )
82+ userChats . profilePic = ""
83+ listOfDocs . push ( ...userChats )
84+ } ) ;
85+ Promise . all ( listOfDocs . map ( ( data , index ) => {
86+ return new Promise ( ( resolve , reject ) => {
87+ getUser_Pfp_from_Username_Displayname ( data . chatName , ( pfpUrl ) => {
88+ console . log ( pfpUrl ) ;
89+ listOfDocs [ index ] . profilePic = pfpUrl ;
90+ resolve ( ) ;
91+ } ) ;
92+ } ) ;
93+ } ) ) . then ( ( ) => {
94+ // After all profilePic assignments are done, setConversationList
95+ setConversationList ( listOfDocs ) ;
96+ listOfDocs = [ ] ;
97+ } ) . catch ( ( error ) => {
98+ console . error ( error ) ;
99+ } ) ;
100+ } ) ;
84101 } catch ( err ) {
85102 console . error ( err ) ;
86103 }
87104
105+
88106 return ( ) => {
89107 unsubscribe ( ) ;
90108 } ;
91109 }
92110
93111 useEffect ( ( ) => {
94112 getConversationList ( )
113+ } , [ ] )
95114
96- // this is used to make chat realtime
97- const queryMessages = query ( conversationsCollectionRef ) ;
115+ useEffect ( ( ) => {
116+ const queryMessages = query ( conversationsCollectionRef , where ( "usersInvolved" , "array-contains" , permanentUsernameOfLoggedInUser ) ) ;
98117 var listOfMess = [ ]
99118 onSnapshot ( queryMessages , ( snapshot ) => {
100119 snapshot . forEach ( ( doc ) => {
101120 const messages = doc . data ( ) . messages ;
102121 const convoid = doc . id ;
103122 listOfMess . push ( { convoid, messages} ) ;
104123 } ) ;
105- console . log ( listOfMess , "mess" )
124+
106125 listOfMess . map ( ( doc ) => {
107126 setAllChats ( ( prevState ) => ( {
108127 ...prevState ,
109128 [ doc . convoid ] : { messages : doc . messages } ,
110- } ) ) ;
111- // console.log(doc.convoid)
112- } ) ;
113- console . log ( allChats , "oriko" )
129+ } ) )
130+ } )
114131 listOfMess = [ ]
115132 } ) ;
116- } , [ ] )
117-
118- useEffect ( ( ) => { console . log ( allChats ) } , [ allChats ] )
133+ } , [ permanentUsernameOfLoggedInUser ] )
119134
120135
121136 const value = {
0 commit comments