aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/Chat.js
blob: 6bcff8377a09c8102467921566fbcea00b809dbf (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { useEffect, useState, useCallback } from 'react';
import { useParams } from 'react-router-dom';
import moment from 'moment';

import PM from './PM';
import MessageInput from './MessageInput';
import UserInfo from './UserInfo';

import { getChat, pm } from '../api';

import './Chat.css';
import { useVisitor } from './VisitorContext';
import { Helmet } from 'react-helmet';

/**
 * @typedef {Object} ChatProps
 * @property {EventSource} connection
 */

/**
 * Chat component
 * @param {ChatProps} props
 */
export default function Chat(props) {
    const [visitor] = useVisitor();
    const [chats, setChats] = useState([]);
    const params = useParams();

    let loadChat = useCallback((uname) => {
        const { hash } = visitor;
        setChats([]);
        if (hash && uname) {
            getChat(uname)
                .then(response => {
                    setChats(response.data);
                });
        }
    }, []);

    let onMessage = useCallback((json) => {
        const msg = JSON.parse(json.data);
        if (msg.user.uname === params.user) {
            setChats((oldChat) => {
                return [msg, ...oldChat];
            });
        }
    }, [params.user]);

    let onSend = (template) => {
        pm(template.to.uname, template.body)
            .then(res => {
                loadChat(params.user);
            }).catch(console.log);
    };
    useEffect(() => {
        if (props.connection.addEventListener) {
            props.connection.addEventListener('msg', onMessage);
        }
        loadChat(params.user);
        console.log(props.connection);
        return () => {
            if (props.connection.removeEventListener) {
                props.connection.removeEventListener('msg', onMessage);
            }
        };
    }, [props.connection, onMessage, loadChat, params.user]);
    const uname = params.user;
    return (
        <div className="msg-cont">
            <Helmet>
                <title>PM: {uname}</title>
            </Helmet>
            <UserInfo uname={uname} />
            {uname ? (
                <div className="chatroom">
                    <ul className="Chat_messages">
                        {
                            chats.map((chat) =>
                                <PM key={moment.utc(chat.timestamp).valueOf()} chat={chat} {...props} />
                            )
                        }
                    </ul>
                    <MessageInput data={{ mid: 0, timestamp: '0', to: { uname: uname } }} onSend={onSend}>
                        Reply...
                            </MessageInput>
                </div>
            ) : (
                    <div className="chatroom no-selection"><p>No chat selected</p></div>
                )
            }
        </div>
    );
}