-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSidebarChat.js
52 lines (47 loc) · 1.35 KB
/
SidebarChat.js
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
import React, { useEffect, useState } from "react";
import "./SidebarChat.css";
import { Avatar } from "@material-ui/core";
import db from "./firebase";
import { Link } from "react-router-dom";
function SidebarChat({ id, name, addNewChat }) {
const [seed, setSeed] = useState("");
const [messages, setMessages] = useState("");
useEffect(() => {
if (id) {
db.collection("rooms")
.doc(id)
.collection("messages")
.orderBy("timestamp", "desc")
.onSnapshot((snapshot) =>
setMessages(snapshot.docs.map((doc) => doc.data()))
);
}
}, [id]);
useEffect(() => {
setSeed(Math.floor(Math.random() * 5000));
}, []);
const createChat = () => {
const roomName = prompt("Please enter name for chat");
if (roomName) {
db.collection("rooms").add({
name: roomName,
});
}
};
return !addNewChat ? (
<Link to={`/rooms/${id}`}>
<div className="sidebarChat">
<Avatar src={`https://avatars.dicebear.com/api/human/${seed}.svg`} />
<div className="sidebarChat__info">
<h2>{name}</h2>
<p>{messages[0]?.message}</p>
</div>
</div>
</Link>
) : (
<div onClick={createChat} className="sidebarChat">
<h2>Add new Chat</h2>
</div>
);
}
export default SidebarChat;