This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
/
index.jsx
310 lines (292 loc) · 8.77 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import React, { useState, useRef, useEffect } from "react";
import styles from "./styles.module.scss";
import { connect } from "react-redux";
import { LiveEntity, IgLoginRequiredError } from "instagram-private-api";
import LoadingBar from "../../components/LoadingBar";
import TextInput from "../../components/TextInput";
import Button from "../../components/Button";
import { useHistory } from "react-router-dom";
import CommentIcon from "../../images/comment.svg";
import Comments from "../Comments";
import { getClient, removeSession } from "../../lib/igClient";
import { clearComments } from "../../store/User/actions";
import CopyIcon from "../../images/copy.svg";
import copy from "copy-to-clipboard";
import Timer from "../../components/Timer";
import useTimer from "../../lib/timerHook";
import config from "../../utils/config";
import open from "open";
const openLinkInBrowser = (link) => {
open(link);
};
function Home({ profile, dispatch }) {
const client = getClient();
const history = useHistory();
if (!(profile && profile.username)) history.push("/");
const { username, full_name, profile_pic_url } = profile;
const [isLive, setLive] = useState(false);
const [isReady, setReady] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [broadcastId, setBroadcastId] = useState(null);
const [streamTitle, setStreamTitle] = useState("");
const [streamURL, setStreamURL] = useState("");
const [streamKey, setStreamKey] = useState("");
const [showComments, setShowComments] = useState(false);
const [duration, startTimer, stopTimer, clearTimer] = useTimer(0);
console.log({ profile })
// stop the live stream if it crosses 1 hour
// keeping buffer of 2 seconds to stop the stream
useEffect(() => {
if (duration >= config.STREAM_LIMIT - 2) {
stopLiveStream();
}
}, [duration]);
const iUrlRef = useRef();
const iKeyRef = useRef();
const startLiveStream = async () => {
setIsLoading(true);
try {
const { broadcast_id, upload_url } = await client.live.create({
// create a stream in 720x1280 (9:16)
previewWidth: 720,
previewHeight: 1280,
message: streamTitle,
});
setBroadcastId(broadcast_id);
const { stream_key, stream_url } = LiveEntity.getUrlAndKey({
broadcast_id,
upload_url,
});
setStreamURL(stream_url);
setStreamKey(stream_key);
setReady(true);
setIsLoading(false);
} catch (error) {
setIsLoading(false);
if (error instanceof IgLoginRequiredError) {
removeSession();
history.push("/");
// client.account.logout();
}
}
};
const goLive = async () => {
setIsLoading(true);
if (broadcastId) {
try {
await client.live.start(broadcastId);
await client.live.unmuteComment(broadcastId);
dispatch(clearComments());
clearTimer();
startTimer();
setLive(true);
setIsLoading(false);
} catch (error) {
setIsLoading(false);
setReady(false);
setLive(false);
if (error instanceof IgLoginRequiredError) {
removeSession();
history.push("/");
}
}
}
};
const stopLiveStream = async () => {
setIsLoading(true);
try {
await client.live.endBroadcast(broadcastId);
if (config.SAVE_BROADCAST_TO_STORIES) {
await client.live.addToPostLive(broadcastId);
}
} catch (error) {
if (error instanceof IgLoginRequiredError) {
removeSession();
history.push("/");
}
setIsLoading(false);
// return;
}
if (window.refreshInterval) {
clearInterval(window.refreshInterval);
}
stopTimer();
clearTimer();
setStreamTitle("");
setLive(false);
setReady(false);
setBroadcastId(null);
setStreamKey(null);
setStreamURL(null);
setIsLoading(false);
};
const logout = async () => {
removeSession();
client.account.logout();
history.push("/");
};
const getButtonAndLoaders = () => {
if (isLoading) {
return (
<div>
<LoadingBar />
</div>
);
} else if (isReady && !isLive) {
return (
<button className={styles.goLiveButton} onClick={goLive}>
Go Live!
</button>
);
} else if (isLive) {
return (
<button className={styles.stopButton} onClick={stopLiveStream}>
Stop Steam
</button>
);
} else {
return (
<div
style={{
display: `flex`,
flexDirection: `column`,
justifyContent: `center`,
}}
className={styles.animate}
>
<p className={styles.info}>
Explore our {" "}
<span
style={{ color: "var(--color-primary)", cursor: "pointer" }}
onClick={() =>
openLinkInBrowser(
"https://getstreamon.com/blog"
)
}
>
📚 Guides
</span> to learn how to use Streamon. <span
style={{ color: "var(--color-primary)", cursor: "pointer" }}
onClick={() =>
openLinkInBrowser(
"https://ko-fi.com/haxzie"
)
}
>
Support us 💸
</span> to help keep the project alive.
</p>
<form
onSubmit={(e) => {
e.preventDefault();
startLiveStream();
}}
style={{
display: `flex`,
flexDirection: `column`,
justifyContent: `center`,
}}
>
<TextInput
style={{
margin: `15px 0`,
minWidth: `300px`,
}}
value={streamTitle}
onChange={(e) => setStreamTitle(e.target.value)}
placeholder="What are you streaming?"
autoFocus={true}
/>
<button className={styles.liveButton}>Start Live Stream</button>
</form>
<Button onClick={() => logout()} buttontype="clear">
Logout
</Button>
</div>
);
}
};
const copyUrl = () => {
copy(streamURL);
iUrlRef.current.select();
};
const copyKey = () => {
copy(streamKey);
iKeyRef.current.select();
};
return (
<div className={styles.homePage}>
<div className={styles.pageContents}>
<div
className={`${styles.profilePicWrapper} ${
isLive ? styles.liveBorder : ""
}`}
>
<img src={`${process.env.REACT_APP_IMAGE_PROXY}/${profile_pic_url}`} className={styles.profilePic} />
{isLive ? <span className={`${styles.liveTag}`}> Live</span> : <></>}
</div>
<div className={styles.texts}>
<h4 className={styles.fullName}>{full_name}</h4>
<p className={styles.username}>@{username}</p>
</div>
{getButtonAndLoaders()}
{isReady && !isLive ? (
<>
<div className={styles.linkFields}>
<label>Stream URL</label>
<div className={styles.row}>
<TextInput value={streamURL} forwardRef={iUrlRef} readOnly />
<button className={styles.copyIcon} onClick={copyUrl}>
<img src={CopyIcon} />
</button>
</div>
<label>Stream Key</label>
<div className={styles.row}>
<TextInput
value={streamKey}
type="password"
forwardRef={iKeyRef}
readOnly
/>
<button className={styles.copyIcon} onClick={copyKey}>
<img src={CopyIcon} />
</button>
</div>
<Button onClick={() => setReady(false)} buttontype="ghost">
Cancel
</Button>
</div>
</>
) : (
<></>
)}
{isReady && isLive ? <Timer seconds={duration} maxLimit={config.STREAM_LIMIT}/> : <></>}
</div>
{isLive ? (
<div className={styles.popupContents}>
<div
className={`${styles.commentsFab}`}
onClick={() => setShowComments(!showComments)}
>
<img src={CommentIcon} alt="" />
Comments
</div>
<Comments
open={showComments}
broadcastId={broadcastId}
clickClose={() => setShowComments(!showComments)}
/>
</div>
) : (
<></>
)}
</div>
);
}
const mapStateToProps = function (state) {
return {
isLoggedIn: state.auth.isLoggedIn,
profile: state.user.profile,
};
};
export default connect(mapStateToProps)(Home);