-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChatPage.tsx
174 lines (149 loc) · 4.2 KB
/
ChatPage.tsx
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { useState, useEffect } from "react";
import Gun from "gun/gun";
import "gun/lib/radix";
import "gun/lib/radisk";
import "gun/lib/store";
import "gun/lib/rindexed";
import { useLocation, useParams } from "react-router-dom";
import * as _ from "lodash";
import { AES, enc } from "crypto-js";
import { v4 } from "uuid";
import { useStore } from "../store/AppStore";
import dayjs from "dayjs";
type Message = {
messageId: string;
author: string;
content: string;
timestamp: number;
};
// This is only used for building up the connection
// Not for message communication
const relay = ""; // <- You have to specify your own gundb server url here
const gun = Gun(relay);
const ChatPage = (props: any) => {
const params = useParams();
const location = useLocation();
// Store
const storeUsername = useStore((state: any) => state.username);
const setStoreUsername = useStore((state: any) => state.setUsername);
const setStoreRoom = useStore((state: any) => state.setRoom);
// Encryption key
const [encryptionKey] = useState(location.hash);
// State
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState("");
useEffect(() => {
if (params.room) {
setStoreRoom(params.room);
}
if (storeUsername == null) {
const enteredUsername = window.prompt("Enter your name:") || "Anonymous";
setStoreUsername(enteredUsername);
}
getGunRoom()
.map()
.once((data: any) => {
// Check data type
if (!isMessage(data)) {
console.log("Malformed message", data);
return;
}
// This is our incoming message
let incomingMessage: Message = data;
// Check if message already exists
const alreadyExists = messages.some(msg => msg.messageId === incomingMessage.messageId);
if (alreadyExists) {
console.log("Message already exists", data);
return;
}
// Add to messages
console.log("Received", incomingMessage);
setMessages(prev => [...prev, incomingMessage]);
});
}, []);
const getGunRoom = () => {
return gun.get(params.room!);
};
const sendMessage = () => {
const message: Message = {
messageId: v4(),
author: storeUsername,
content: encryptString(input, encryptionKey),
timestamp: Date.now()
};
console.log("Send", message);
getGunRoom().set(message);
setInput("");
};
const encryptString = (content: string, key: string) => {
return AES.encrypt(content, key).toString();
};
const decryptString = (content: string, key: string) => {
try {
const decrypted = AES.decrypt(content, key).toString(enc.Utf8);
if (decrypted.length == 0) {
return null;
}
return decrypted;
} catch (error) {
return null;
}
};
const isMessage = (data: any) => {
return (
typeof data.messageId === "string" &&
typeof data.author === "string" &&
typeof data.content === "string" &&
typeof data.timestamp === "number"
);
};
const getMessages = () => {
return _.chain(messages)
.sort((a, b) => a.timestamp - b.timestamp)
.filter(msg => msg.content.length != 0)
.map(msg => {
const decryptedContent = decryptString(msg.content, encryptionKey);
if (decryptedContent) {
return {
...msg,
content: decryptedContent
};
}
})
.uniqBy("messageId")
.value();
};
return (
<div className="flex flex-col">
<div className="">
<ul className="p-4">
{getMessages().map(msg => {
if (msg) {
return (
<li key={msg.messageId} className="mb-2 text-white">
<strong className="font-semibold">
[{dayjs(msg.timestamp).format("DD.MM.YYYY HH:MM:ss")}] {msg.author}:
</strong>{" "}
{msg.content}
</li>
);
}
})}
</ul>
</div>
<form className="flex p-4" onSubmit={e => e.preventDefault()}>
<label className="mr-4 flex-1 rounded-md border border-slate-700">
<input
className="w-full resize-none appearance-none border-none bg-transparent py-2 px-2 leading-tight text-white focus:outline-none"
value={input}
onChange={e => setInput(e.target.value)}
/>
</label>
<button className="rounded-md bg-purple-800 py-2 px-8 font-bold text-white" onClick={sendMessage}>
Send
</button>
</form>
</div>
);
};
export default ChatPage;