/
VideoMessageRecorder.tsx
134 lines (117 loc) · 4.43 KB
/
VideoMessageRecorder.tsx
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
import type { IMessage, IRoom } from '@rocket.chat/core-typings';
import { css } from '@rocket.chat/css-in-js';
import { Box, ButtonGroup, Button, Icon, PositionAnimated } from '@rocket.chat/fuselage';
import { useTranslation, useToastMessageDispatch } from '@rocket.chat/ui-contexts';
import type { AllHTMLAttributes, RefObject } from 'react';
import React, { useRef, useEffect, useState } from 'react';
import { VideoRecorder, UserAction, USER_ACTIVITIES } from '../../../../app/ui/client';
import type { ChatAPI } from '../../../lib/chats/ChatAPI';
import { useChat } from '../../room/contexts/ChatContext';
type VideoMessageRecorderProps = {
rid: IRoom['_id'];
tmid?: IMessage['_id'];
chatContext?: ChatAPI; // TODO: remove this when the composer is migrated to React
reference: RefObject<HTMLElement>;
} & Omit<AllHTMLAttributes<HTMLDivElement>, 'is'>;
const videoContainerClass = css`
transform: scaleX(-1);
filter: FlipH;
@media (max-width: 500px) {
& > video {
width: 100%;
height: 100%;
}
}
`;
const VideoMessageRecorder = ({ rid, tmid, chatContext, reference }: VideoMessageRecorderProps) => {
const t = useTranslation();
const videoRef = useRef<HTMLVideoElement>(null);
const dispatchToastMessage = useToastMessageDispatch();
const [time, setTime] = useState<string | undefined>();
const [recordingState, setRecordingState] = useState<'idle' | 'loading' | 'recording'>('idle');
const [recordingInterval, setRecordingInterval] = useState<ReturnType<typeof setInterval> | null>(null);
const isRecording = recordingState === 'recording';
const sendButtonDisabled = !(VideoRecorder.cameraStarted.get() && !(recordingState === 'recording'));
const chat = useChat() ?? chatContext;
const stopVideoRecording = async (rid: IRoom['_id'], tmid?: IMessage['_id']) => {
if (recordingInterval) {
clearInterval(recordingInterval);
}
setRecordingInterval(null);
UserAction.stop(rid, USER_ACTIVITIES.USER_RECORDING, { tmid });
setRecordingState('idle');
};
const handleRecord = async () => {
if (recordingState === 'recording') {
VideoRecorder.stopRecording();
stopVideoRecording(rid, tmid);
} else {
VideoRecorder.record();
setRecordingState('recording');
UserAction.performContinuously(rid, USER_ACTIVITIES.USER_RECORDING, { tmid });
setTime('00:00');
const startTime = new Date();
setRecordingInterval(
setInterval(() => {
const now = new Date();
const distance = (now.getTime() - startTime.getTime()) / 1000;
const minutes = Math.floor(distance / 60);
const seconds = Math.floor(distance % 60);
setTime(`${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`);
}, 1000),
);
}
};
const handleSendRecord = async () => {
const cb = async (blob: Blob) => {
const fileName = `${t('Video_record')}.webm`;
const file = new File([blob], fileName, { type: 'video/webm' });
await chat?.flows.uploadFiles([file]);
chat?.composer?.setRecordingVideo(false);
};
VideoRecorder.stop(cb);
setTime(undefined);
stopVideoRecording(rid, tmid);
};
const handleCancel = () => {
VideoRecorder.stop();
chat?.composer?.setRecordingVideo(false);
setTime(undefined);
stopVideoRecording(rid, tmid);
};
useEffect(() => {
if (!window.MediaRecorder.isTypeSupported('video/webm; codecs=vp8,opus')) {
return dispatchToastMessage({ type: 'error', message: t('Browser_does_not_support_recording_video') });
}
VideoRecorder.start(videoRef.current);
return () => {
VideoRecorder.stop();
};
}, [dispatchToastMessage, t]);
return (
<PositionAnimated visible='visible' anchor={reference} placement='top-end'>
<Box bg='light' padding={4} borderRadius={4} elevation='2'>
<Box className={videoContainerClass} overflow='hidden' height={240} borderRadius={4}>
<video ref={videoRef} width={320} height={240} />
</Box>
<Box mbs={4} display='flex' justifyContent='space-between'>
<Button small onClick={handleRecord}>
<Box is='span' display='flex' alignItems='center'>
<Icon size={16} mie={time ? 4 : undefined} name={isRecording ? 'stop-unfilled' : 'rec'} />
{time && <span>{time}</span>}
</Box>
</Button>
<ButtonGroup>
<Button small onClick={handleCancel}>
{t('Cancel')}
</Button>
<Button primary small disabled={sendButtonDisabled} onClick={handleSendRecord}>
{t('Send')}
</Button>
</ButtonGroup>
</Box>
</Box>
</PositionAnimated>
);
};
export default VideoMessageRecorder;