@@ -37,13 +37,17 @@ firebase.auth().onAuthStateChanged(async (user) => {
3737} ) ;
3838
3939function loadSidebarFromUserHistory ( history ) {
40- for ( const [ uid , name ] of Object . entries ( history ) ) {
41- firebase . firestore ( ) . collection ( "Users" ) . doc ( uid ) . get ( ) . then ( doc => {
42- const data = doc . data ( ) ;
43- const img = data . profileIMG || "/assets/img/default_user.jpeg" ;
44- add_to_sidebar ( name , img , uid , data . email || "" ) ;
40+ history . forEach ( userId => {
41+ firebase . firestore ( ) . collection ( "Users" ) . doc ( userId ) . get ( ) . then ( doc => {
42+ if ( doc . exists ) {
43+ const data = doc . data ( ) ;
44+ const name = data . name || "Unknown" ;
45+ const profilePic = data . profileIMG || "/assets/img/default_user.jpeg" ;
46+ const email = data . email || "" ;
47+ add_to_sidebar ( name , profilePic , userId , email ) ;
48+ }
4549 } ) ;
46- }
50+ } ) ;
4751}
4852
4953function add_to_sidebar ( name , profilePic , userId , email ) {
@@ -87,6 +91,25 @@ function updateSidebarList(filterText) {
8791 } ) ;
8892 sidebarList . appendChild ( li ) ;
8993 } ) ;
94+
95+ // Firebase email-based lookup for dynamic search
96+ if ( sidebarList . children . length === 0 && filterText . includes ( "@" ) ) {
97+ firebase . firestore ( ) . collection ( "Users" )
98+ . where ( "email" , "==" , filterText )
99+ . get ( )
100+ . then ( snapshot => {
101+ if ( ! snapshot . empty ) {
102+ const doc = snapshot . docs [ 0 ] ;
103+ const data = doc . data ( ) ;
104+ const userId = doc . id ;
105+ const name = data . name || "Unknown" ;
106+ const profilePic = data . profileIMG || "/assets/img/default_user.jpeg" ;
107+ const email = data . email || "" ;
108+ add_to_sidebar ( name , profilePic , userId , email ) ;
109+ updateSidebarList ( filterText ) ; // re-run to show only matching user
110+ }
111+ } ) ;
112+ }
90113}
91114
92115searchInput . addEventListener ( "input" , ( e ) => {
@@ -130,6 +153,9 @@ sendButton.addEventListener("click", () => {
130153 messageText : msg ,
131154 timestamp : firebase . firestore . FieldValue . serverTimestamp ( )
132155 } ) ;
156+ firebase . firestore ( ) . collection ( "Users" ) . doc ( CURRENT_USER_ID ) . update ( {
157+ userHistory : firebase . firestore . FieldValue . arrayUnion ( activeUserId )
158+ } ) ;
133159
134160 chatInput . value = "" ;
135161} ) ;
0 commit comments