-
Notifications
You must be signed in to change notification settings - Fork 3
/
Transcript.js
76 lines (69 loc) · 1.93 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
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
import React, {
Fragment,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { fetchTranscript } from "../utils/api";
import { GlobalStyles } from "./GlobalStyles";
import { useTranscription } from "@daily-co/daily-react";
const REFRESH_INTERVAL = 30000;
export const Transcript = ({ roomUrl }) => {
const [unhandledLines, setUnhandledLines] = useState(0);
const [transcript, setTranscript] = useState("");
const isScrolledDown = useRef(true);
const transcriptRef = useRef(null);
useTranscription({
onTranscriptionAppData: useCallback(() => {
setUnhandledLines((lines) => lines + 1);
}, []),
});
useEffect(() => {
const interval = setInterval(async () => {
if (unhandledLines === 0) return;
try {
setUnhandledLines(0);
const response = await fetchTranscript(roomUrl);
isScrolledDown.current =
transcriptRef.current.scrollTop >=
transcriptRef.current.scrollHeight -
transcriptRef.current.clientHeight;
setTranscript(response);
} catch {
// Failed to fetch transcript
}
}, REFRESH_INTERVAL);
return () => {
clearInterval(interval);
};
}, [roomUrl, unhandledLines]);
useEffect(() => {
if (!isScrolledDown.current) return;
transcriptRef.current?.scrollTo({
top: transcriptRef.current?.scrollHeight,
behavior: "smooth",
});
}, [transcript]);
return (
<div className="transcript" ref={transcriptRef}>
<h3>Transcript</h3>
{transcript
? transcript.split("\n").map((line, i) => (
<Fragment key={`transcript-${i}`}>
{i > 0 && <br />}
{line}
</Fragment>
))
: "No transcript available."}
<GlobalStyles />
<style jsx>{`
.transcript {
overflow-x: hidden;
overflow-y: auto;
padding: 8px;
}
`}</style>
</div>
);
};