This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
/
index.jsx
220 lines (205 loc) · 6.15 KB
/
index.jsx
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import React, { useState, useEffect } from "react";
import { connect } from "react-redux";
import { saveComments, clearComments } from "../../store/User/actions";
import { getClient } from "../../lib/igClient";
import "simplebar/dist/simplebar.min.css";
import styles from "./styles.module.scss";
import CloseIcon from "../../images/down-arrow.svg";
import SendIcon from "../../images/direct.svg";
import Toggle from "../../components/Toggle";
import MuteIcon from "../../images/mute.svg";
import PinIcon from "../../images/pin.svg";
function Comments({
broadcastId,
comments,
dispatch,
profile,
open,
clickClose,
}) {
const [isMuted, setMuted] = useState(false);
const [inProgress, setInProgress] = useState(false);
const [userComment, setUserComment] = useState("");
const [isCommenting, setCommenting] = useState(false);
const [pinnedComment, setPinnedComment] = useState();
const [isFetchingComments, setFetchingComments] = useState(false);
const client = getClient();
let lastCommentTs =
comments && comments.length > 0 ? comments[0].created_at : 0;
const startComments = async () => {
setInProgress(true);
window.refreshInterval = setInterval(() => {
if (!isFetchingComments) {
fetchComments();
}
}, 2000);
setInProgress(false);
};
const stopComments = async () => {
setInProgress(true);
if (window.refreshInterval) {
clearInterval(window.refreshInterval);
window.refreshInterval = null;
}
setInProgress(false);
};
const fetchComments = async () => {
setFetchingComments(true);
try {
const { comments } = await client.live.getComment({
broadcastId,
lastCommentTs,
});
if (comments && comments.length > 0) {
setCommenting(false);
const newLastCommentTs = comments[0].created_at;
lastCommentTs = newLastCommentTs;
dispatch(saveComments(comments));
}
} catch (error) {
console.error(error);
}
setFetchingComments(false);
};
const pinComment = async (comment) => {
// check if current comment is pinned
if (pinnedComment && pinnedComment.pk === comment.pk) {
try {
setPinnedComment(null);
await client.live.unpinComment(broadcastId, comment.pk);
} catch (error) {
console.error(error);
}
} else {
try {
setPinnedComment(comment);
await client.live.pinComment(broadcastId, comment.pk);
} catch (error) {
console.error(error);
}
}
};
const addComment = async () => {
const comment = userComment;
// do not comment, if there is no comment text available
if (!(comment && comment.length > 0)) return;
setUserComment("");
setCommenting(true);
try {
await client.live.comment(broadcastId, comment);
} catch (error) {
console.error(error);
clickClose();
}
};
const toggleComments = async () => {
try {
if (!isMuted) {
stopComments();
client.live.muteComment(broadcastId);
setMuted(true);
} else {
startComments();
client.live.unmuteComment(broadcastId);
setMuted(false);
}
} catch (error) {
console.error(error);
}
};
useEffect(() => {
if (!isMuted && !window.refreshInterval) {
startComments();
}
return () => {
// stop refreshing the comments
stopComments();
dispatch(clearComments());
};
}, []);
const renderComments = (comments) => {
return comments.map((comment) => (
<div key={comment.pk} className={styles.comment}>
<img className={styles.profilePic} src={`${process.env.REACT_APP_IMAGE_PROXY}/${comment.user.profile_pic_url}`} />
<div className={styles.textContainer}>
<h4 className={styles.title}>{comment.user.username}</h4>
<p className={styles.text}>{comment.text}</p>
</div>
<div
className={`${styles.pinIcon} ${
pinnedComment
? pinnedComment.pk === comment.pk
? styles.active
: ""
: ""
}`}
onClick={() => pinComment(comment)}
>
<img src={PinIcon} className={styles.icon} />
</div>
</div>
));
};
return (
<div className={`${styles.commentsScreen} ${!open ? styles.close : ""}`}>
<div className={styles.header}>
<img src={`${process.env.REACT_APP_IMAGE_PROXY}/${profile.profile_pic_url}`} className={styles.headerIcon} />
<h2 className={styles.title}>Comments</h2>
{!inProgress ? (
<Toggle
checked={!isMuted}
onChange={() => {
toggleComments();
}}
disabled={inProgress}
/>
) : (
<></>
)}
<img
src={CloseIcon}
className={styles.closeIcon}
onClick={() => clickClose()}
></img>
</div>
{isMuted ? (
<div className={styles.commentsDiabled}>
<img src={MuteIcon} className={styles.muteIcon} />
<p className={styles.description}>
Comments are muted. Click on unmute comments to enable comments
</p>
{/* <Button buttontype="rounded">Unmute Comments</Button> */}
</div>
) : (
<>
<div className={styles.comments}>{renderComments(comments)}</div>
<form
className={styles.commentBox}
onSubmit={(e) => {
e.preventDefault();
addComment();
}}
>
<input
type="text"
className={styles.commentInput}
value={userComment}
onChange={(e) => setUserComment(e.target.value)}
placeholder={isCommenting ? "Loading..." : "Type and Press enter to send"}
/>
<button className={styles.sendButton} type="submit">
<img src={SendIcon} alt="" />
</button>
</form>
</>
)}
</div>
);
}
const mapStateToProps = function (state) {
return {
profile: state.user.profile,
comments: state.instagram.comments,
};
};
export default connect(mapStateToProps)(Comments);