-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSidebarChat.js
146 lines (136 loc) · 7.29 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
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
import { useEffect, memo, useRef, useState } from 'react';
import { Avatar } from '@mui/material';
import { CancelOutlined, SearchOutlined, Photo, MicRounded } from '@mui/icons-material';
import './SidebarChat.css';
import { Link } from 'react-router-dom';
import CircularProgress from '@mui/material/CircularProgress';
import { useStateValue } from './StateProvider';
//window
const emojiStyles = ["bottts-neutral", "thumbs", "fun-emoji", "icons", "shapes", "avataaars-neutral", "fun-emoji", "avataaars-neutral", "fun-emoji", "fun-emoji"];
function SidebarChat({ dataList, title, path, fetchList }) {
const [{ roomsData, page, pathID }, dispatch] = useStateValue();
const [scrollFetch, setScrollFetch] = useState(false);
const [list, setList] = useState(null);
const sidebarChatContainer = useRef();
useEffect(() => {
var a = null;
if (fetchList) {
a = function () {
if (parseInt(sidebarChatContainer.current.scrollTop) === sidebarChatContainer.current.scrollHeight - sidebarChatContainer.current.offsetHeight) {
fetchList(setScrollFetch);
};
};
if (dataList?.length > 0) {
sidebarChatContainer.current.addEventListener("scroll", a);
};
};
const clean = sidebarChatContainer.current;
return () => {
if (a) {
//console.log("removing event");
clean.removeEventListener("scroll", a);
};
};
}, [dataList, fetchList]);
useEffect(() => {
if (dataList?.length === list?.length) {
setTimeout(() => {
Array.from(document.querySelectorAll('.animate')).forEach((cur, i) => {
setTimeout(() => {
cur.classList.remove("animate");
}, 50 * i);
});
}, 10);
};
}, [dataList, list]);
useEffect(() => {
if (dataList) {
const arr = [];
dataList.forEach(data => {
if (data) {
const onlineState = roomsData[data.id]?.onlineState ? roomsData[data.id].onlineState : data.state;
const lastMessage = title === "Search Result" ? null : roomsData[data.id]?.lastMessage ? roomsData[data.id].lastMessage : data.lastMessage;
if (title === "Rooms" || title === "Search Result" || title === "Chats" && roomsData[data.id].lastMessage || roomsData[data.id].lastMessage === "" || title !== "Chats" && roomsData[data.id]) {
arr.push(<Link className="link" key={data.id} to={{
pathname: path ? `${path}/room/${data.id}` : `/room/${data.id}`,
state: {
photoURL: `${data.photoURL ? data.photoURL : `https://api.dicebear.com/8.x/${emojiStyles[data.id.charCodeAt(0) % 10]}/svg?seed=${data.id}`}`,
name: data.name,
userID: data.userID ? data.userID : null,
state: data.state
}
}} >
<div
className="sidebar__chat animate"
>
<div className="avatar__container">
<Avatar style={{ width: 45, height: 45 }} src={`${data.photoURL && data.userID ? data.photoURL : `https://api.dicebear.com/8.x/${emojiStyles[data.id.charCodeAt(0) % 10]}/svg?seed=${data.id}`}`} />
{onlineState === "online" ? <div className="online"></div> : null}
</div>
<div className="sidebar__chat--info">
<h2
dangerouslySetInnerHTML={{ __html: title === "Search Result" ? data._highlightResult.name?.value : data.name }}
style={{
width: page.width <= 760 ? page.width - 126 : page.width * 0.315 - 126,
marginBottom: lastMessage?.message || lastMessage ? 8 : 0,
}}
></h2>
<p style={{ width: page.width <= 760 ? page.width - 126 : page.width * 0.315 - 146 }}>
{lastMessage?.message ?
<><Photo style={{ width: 19, height: 19 }} /> <span style={{ width: page.width <= 760 ? page.width - 150 : page.width * 0.315 - 150 }}>{lastMessage.message}</span> </>
: lastMessage?.audio ?
<><MicRounded style={{ width: 19, height: 19 }} /><span style={{ width: page.width <= 760 ? page.width - 150 : page.width * 0.315 - 150 }}>{lastMessage.time}</span></>
: lastMessage?.message === "" ?
<><Photo style={{ width: 19, height: 19 }} /> <span style={{ width: page.width <= 760 ? page.width - 150 : page.width * 0.315 - 150 }}>Photo</span> </>
: lastMessage}
</p>
</div>
{data?.unreadMessages && pathID !== data.id ?
<div className="sidebar__chat--unreadMessages">
<div>
{data.unreadMessages}
</div>
</div>
: null}
</div>
</Link>);
}
} else {
arr.push(null);
}
});
setList(arr)
}
}, [dataList, roomsData, page, pathID]);
useEffect(() => {
if (page.width <= 760) {
dispatch({ type: "SET_PATH", path: path });
} else {
dispatch({ type: "SET_PATH", path: "" })
}
}, [path, page]);
return (
<div ref={sidebarChatContainer} className="sidebar__chat--container">
<h2 className={"animate"}>{title} </h2>
{dataList?.length > 0 ? list : dataList === null && title !== "Chats" ?
<div className="loader__container sidebar__loader">
<CircularProgress />
</div>
:
<div className="no-result">
<div>
<SearchOutlined />
<div className="cancel-root">
<CancelOutlined />
</div>
</div>
<h2>No {title} found </h2>
</div>
}
<div className="loader__container scrollFetch">
{scrollFetch ? <CircularProgress /> : null}
</div>
</div>
)
}
export default memo(SidebarChat);