-
Notifications
You must be signed in to change notification settings - Fork 14
/
Transcript.js
48 lines (41 loc) · 1.39 KB
/
Transcript.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
import React, { useEffect, useRef } from 'react';
import './Transcript.css';
/* eslint react/prop-types: 0 */
export const Transcript = ({ transcript }) => {
const ref = useRef();
const finalTranscript = [];
let currentSpeaker = undefined;
for (let i = 0; i < transcript.length; i++) {
const utterance = transcript[i];
const isLast = i === transcript.length - 1;
if (utterance.speaker !== currentSpeaker) {
currentSpeaker = utterance.speaker;
finalTranscript.push({ speaker: currentSpeaker, text: [] });
}
if (utterance.is_final || isLast) {
finalTranscript[finalTranscript.length - 1].text.push(
...utterance.words.map((i) => i.text)
);
continue;
}
}
useEffect(() => {
// scroll to bottom
if (ref.current) {
ref.current.scrollTop = ref.current.scrollHeight;
}
}, [transcript]);
return (
<div ref={ref} className="InMeeting-transcript">
{finalTranscript.map((item, index) => (
<p key={index}>
<span className="InMeeting-transcript-speaker">
{item.speaker || 'Unknown'}:
</span>
<span>{item.text.join(' ')}</span>
</p>
))}
</div>
);
};
export default Transcript;