chore(release): 0.2.5
[risinglegends.git] / src / client / chat.ts
1 import {Socket} from 'socket.io-client';
2 import {Message} from '../shared/message';
3
4 // disable chat temporarily to test widgetbot
5 let socket: Socket;
6 function $<T>(selector: string, root: any = document): T {
7   return root.querySelector(selector) as T;
8 }
9
10 function $$<T>(selector: string, root: any = document): T[] {
11   return Array.from(root.querySelectorAll(selector)) as T[];
12 }
13
14 function renderChatMessage(msg: Message) {
15   $<HTMLElement>('#chat-messages').innerHTML += `<div class="chat-message" title="${new Date(parseInt(msg.sentAt))}" id="${msg.id}">
16                              <span class="from">${msg.from}</span>
17                              <span class="message">${msg.msg}</span>
18                              </div>`;
19   $<HTMLElement>('#chat-messages').scrollTop = $<HTMLElement>('#chat-messages').scrollHeight;
20 }
21
22 function configureStandardChat() {
23   socket.on('chat', renderChatMessage);
24 }
25
26 export function configureChat(_socket: Socket) {
27   // disable chat temporarily to test discord chat
28   socket = _socket;
29
30   configureStandardChat();
31
32   console.log('Chat Configured');
33 }