-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathChatFooter.js
361 lines (338 loc) · 10 KB
/
ChatFooter.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { memo, useState, useEffect, useRef, useCallback, useMemo } from 'react';
import { Send, MicRounded, CancelRounded, CheckCircleRounded, AddPhotoAlternate } from '@mui/icons-material';
import db, { createTimestamp, fieldIncrement, audioStorage } from "./firebase";
import { useStateValue } from './StateProvider';
import { CSSTransition } from "react-transition-group";
import recorder from "./recorder.js";
import SpeedDial from '@mui/material/SpeedDial';
import SpeedDialAction from '@mui/material/SpeedDialAction';
import AttachFileIcon from '@mui/icons-material/AttachFile';
//import InsertDriveFileIcon from '@mui/icons-material/InsertDriveFile';
import "./ChatFooter.css";
const wait = time => new Promise(resolve => setTimeout(resolve, time));
function preventDefault(e) {
var code = e.keyCode || e.which;
if (code == 13) {
e.preventDefault();
return false;
}
};
function prevent(e) {
console.log("preventing default");
e.preventDefault();
}
const recordCompatible = Boolean(navigator.mediaDevices?.getUserMedia && window.MediaRecorder);
console.log({ recordCompatible });
const speedDialStyle = {
width: 46,
position: "absolute",
left: 0,
bottom: 0,
"& .css-7dv1rb-MuiButtonBase-root-MuiFab-root-MuiSpeedDial-fab": {
width: "46px !important",
height: "46px !important",
boxShadow: "none !important",
backgroundColor: "transparent !important",
color: "#757575 !important",
},
"& .css-7dv1rb-MuiButtonBase-root-MuiFab-root-MuiSpeedDial-fab:hover": {
backgroundColor: "transparent !important",
color: "#757575 !important",
boxShadow: "none !important",
},
"& .MuiSpeedDial-actions": {
paddingBottom: "38px"
},
}
export default memo(function ChatFooter({ input, handleFocus, change, sendMessage, setFocus, image, focus, state, token, roomID, setAudioID, handleFile, sendingMessage, handleMedia }) {
const [recording, setRecording] = useState(false);
const [recordingTimer, setRecordingTimer] = useState("00:00");
const [open, setOpen] = useState(false);
const [{ user }] = useStateValue();
const attachFileRef = useRef();
const attachMediaRef = useRef();
const recordingEl = useRef();
const inputRef = useRef();
const timerInterval = useRef();
const record = useRef();
const handleBlur = useCallback(function (event) {
if (!event.currentTarget.contains(event.relatedTarget) && !recording) {
setFocus(false)
} else {
setFocus(true);
}
}, [recording])
const sendAudio = useCallback(async function (audioFile, timer, audioName) {
if (!sendingMessage.current) {
sendingMessage.current = true;
/*update recording status on the database */
db.collection("rooms").doc(roomID).set({
[user.uid]: false,
}, { merge: true });
db.collection("rooms").doc(roomID).set({
lastMessage: {
audio: true,
message: "",
time: timer,
},
seen: false
}, { merge: true });
if (state.userID) {
db.collection("users").doc(state.userID).collection("chats").doc(roomID).set({
timestamp: createTimestamp(),
photoURL: user.photoURL ? user.photoURL : null,
name: user.displayName,
userID: user.uid,
unreadMessages: fieldIncrement(1),
}, { merge: true });
db.collection("users").doc(user.uid).collection("chats").doc(roomID).set({
timestamp: createTimestamp(),
photoURL: state.photoURL ? state.photoURL : null,
name: state.name,
userID: state.userID
}, { merge: true });
} else {
db.collection("users").doc(user.uid).collection("chats").doc(roomID).set({
timestamp: createTimestamp(),
photoURL: state.photoURL ? state.photoURL : null,
name: state.name,
});
};
const doc = await db.collection("rooms").doc(roomID).collection("messages").add({
name: user.displayName,
uid: user.uid,
timestamp: createTimestamp(),
time: new Date().toUTCString(),
audioUrl: "uploading",
transcript: "$state=loading",
audioName,
audioPlayed: false
});
await audioStorage.child(audioName).put(audioFile);
db.collection("transcripts").add({
audioName,
roomID,
messageID: doc.id,
short: false, //timer[1] === "0",
supportWebM: false//MediaRecorder.isTypeSupported("audio/webm")
});
const url = await audioStorage.child(audioName).getDownloadURL();
db.collection("rooms").doc(roomID).collection("messages").doc(doc.id).update({
audioUrl: url
});
if (state.userID && token !== "") {
db.collection("notifications").add({
userID: user.uid,
title: user.displayName,
body: "🎙️ " + timer,
photoURL: user.photoURL,
token: token
});
};
sendingMessage.current = false;
}
}, [state, token]);
const startRecording = useCallback(async function (e) {
e.preventDefault();
if (window.navigator.onLine) {
if (focus) {
//inputRef.current.focus();
}
await wait(150);
record.current = await recorder(roomID);
setAudioID(null);
inputRef.current.style.width = "calc(100% - 56px)"
await wait(305);
setRecording(true);
} else {
alert("No access to internet !!!");
}
}, [focus])
const stopRecording = useCallback(async function () {
if (focus) {
//inputRef.current.focus();
}
/*Update user recording status */
db.collection("rooms").doc(roomID).set({
[user.uid]: false,
}, { merge: true });
clearInterval(timerInterval.current);
const stopped = record.current.stop();
recordingEl.current.style.opacity = "0";
await wait(300);
setRecording(false);
inputRef.current.style.width = "calc(100% - 112px)";
const time = recordingTimer;
setRecordingTimer("00:00");
return [stopped, time];
}, [recordingTimer, focus])
const finishRecording = useCallback(async function () {
var [audio, time] = await stopRecording();
audio = await audio;
console.log("recorded audio: ", audio);
sendAudio(audio.audioFile, time, audio.audioName);
}, [sendAudio, stopRecording])
const pad = useCallback(function (val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}, []);
const timer = useCallback(function () {
const start = Date.now();
timerInterval.current = setInterval(setTime, 100);
function setTime() {
const delta = Date.now() - start; // milliseconds elapsed since start
const totalSeconds = Math.floor(delta / 1000);
setRecordingTimer(pad(parseInt(totalSeconds / 60)) + ":" + pad(totalSeconds % 60))
}
}, []);
useEffect(() => {
const a = async () => {
await wait(10);
recordingEl.current.style.opacity = "1";
await wait(100);
timer();
db.collection("rooms").doc(roomID).set({
[user.uid]: "recording",
}, { merge: true })
record.current.start();
}
if (recording) {
a();
}
}, [recording]);
const btnIcons = useMemo(() => recordCompatible ? (<>
<CSSTransition
in={(input !== "" || (input === "" && image.length > 0))}
timeout={{
enter: 400,
exit: 200,
}}
classNames="send__animate2"
>
<Send
style={{
width: 20,
height: 20,
color: "white"
}}
/>
</CSSTransition>
<CSSTransition
in={!(input !== "" || (input === "" && image.length > 0))}
timeout={{
enter: 400,
exit: 200,
}}
classNames="send__animate"
>
<MicRounded
style={{
width: 24,
height: 24,
color: "white"
}}
/>
</CSSTransition>
</>) : (
<Send
style={{
width: 20,
height: 20,
color: "white",
opacity: 1
}}
/>
), [input, image]);
const fileOpen = useCallback(() => {
setOpen(false);
attachFileRef.current.click();
}, []);
const mediaOpen = useCallback(() => {
setOpen(false);
attachMediaRef.current.click();
}, []);
const onOpen = useCallback(() => {
setOpen(true);
}, []);
const onClose = useCallback(() => {
setOpen(false);
}, []);
const blurInput = useCallback(() => {
inputRef.current.blur()
}, []);
return (
<div className="chat__footer" onBlur={handleBlur} >
<form
onKeyPress={input === "" ? preventDefault : null}
>
{/*<input ref={attachFileRef} id="attach-file" style={{ display: "none" }} type="file" onChange={handleFile} multiple />*/}
<input ref={attachMediaRef} id="attach-media" style={{ display: "none" }} accept="image/*" type="file" onChange={handleMedia} multiple />
<SpeedDial
sx={speedDialStyle}
icon={<AttachFileIcon />}
ariaLabel="attach_media-speed_dial"
transitionDuration={0}
open={open}
onOpen={onOpen}
onClose={onClose}
>
<SpeedDialAction
icon={<AddPhotoAlternate />}
tooltipTitle="Add photos"
onClick={!recording ? mediaOpen : null}
/>
{/*<SpeedDialAction
icon={<InsertDriveFileIcon />}
tooltipTitle="Add photos"
onClick={fileOpen}
/>*/}
</SpeedDial>
<input
ref={inputRef}
onClick={!recording ? handleFocus : blurInput}
onChange={!recording ? change : null}
onKeyPress={recording ? () => false : null}
onFocus={() => setFocus(true)}
placeholder="Type a message"
className="chat__footer--input"
value={input}
/>
<button
type="submit"
className="send__btn"
onClick={input !== "" || (input === "" && image.length > 0) ? sendMessage : recordCompatible && sendMessage ? startRecording : prevent}
>
{btnIcons}
</button>
</form>
{recording ?
<div ref={recordingEl} className="record">
<CancelRounded
style={{
width: 30,
height: 30,
color: "#F20519"
}}
onClick={stopRecording}
/>
<div>
<div className="record__redcircle"></div>
<div className="record__duration">{recordingTimer}</div>
</div>
<CheckCircleRounded
style={{
width: 30,
height: 30,
color: "#41BF49"
}}
onClick={finishRecording}
/>
</div> : null
}
</div>
);
});