File size: 1,989 Bytes
f1b8b13
88b9eae
d1f9048
f1b8b13
 
 
d1f9048
 
 
f1b8b13
d1f9048
3b5dd8c
f1b8b13
 
 
 
 
 
 
 
 
d1f9048
88b9eae
f1b8b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88b9eae
 
 
12ef2c2
d1f9048
f1b8b13
3b5dd8c
 
d1f9048
 
f1b8b13
d1f9048
c13f601
 
 
f1b8b13
d1f9048
 
 
88b9eae
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, { useContext, useEffect, useState } from 'react'
import Contact from '../Contact';
import "./Contacts.css";
import { UserContext } from '../../contexts/UserProvider';
import { ChatContext } from '../../contexts/ChatProvider';
import { SocketContext } from '../../contexts/SocketProvider';



function Contacts({ }) {

    // const [activeContact, setActiveContact] = useState({});
    const {
        contacts,
        setContacts,
        currentChat,
        setCurrentChat,
        lastMessages,
    } = useContext(ChatContext);
    const { user } = useContext(UserContext);
    const socket = useContext(SocketContext);
    // handleClick


    useEffect(() => {
        if (!socket) return;
        socket.on("contact", (contact) => {
            var newContacts = [...contacts, contact];
            
            newContacts.sort((a,b)=>b.last_message.send_at - a.last_message.send_at);

            
            setContacts(newContacts);
        });

        const cleanUp = () => {
            if (!socket) return;
            socket.off("contact");
        }
        return cleanUp;
    }, [socket])


    return (
        <div className='contacts'>


            {
                contacts.map((contact) => (
                    // console.log("contact.last_message",contact,contact.last_message),
                    // console.log(user.id),
                    <Contact
                        key={contact.id}
                        onClick={() => { setCurrentChat(contact) }}
                        profile_pic={"logo512.png"}
                        name={contact.id}
                        lastmessage={contact.chat_data.last_message ? contact.chat_data.last_message.content:null}
                        unseen_count={contact.chat_data.unseen_count ? contact.chat_data.unseen_count:null}
                        isActive={currentChat.id == contact.id}
                    />
                ))
            }
        </div>
    )
}

export default Contacts