Listen for events and manage button state
This commit is contained in:
parent
ea9edfde1d
commit
eb8c7e2b1f
|
|
@ -11,8 +11,9 @@ export const TRANSCRIPTION_ASIDE = 'transcription';
|
||||||
export const TranscriptionAside = () => {
|
export const TranscriptionAside = () => {
|
||||||
const { callObject } = useCallState();
|
const { callObject } = useCallState();
|
||||||
const { showAside, setShowAside } = useUIState();
|
const { showAside, setShowAside } = useUIState();
|
||||||
const { _sendMessage, transcriptionHistory } = useTranscription();
|
const { transcriptionHistory } = useTranscription();
|
||||||
const { _allParticipants, isOwner } = useParticipants();
|
const [isTranscribing, setIsTranscribing] = useState(false);
|
||||||
|
const { isOwner } = useParticipants();
|
||||||
|
|
||||||
const msgWindowRef = useRef();
|
const msgWindowRef = useRef();
|
||||||
|
|
||||||
|
|
@ -27,30 +28,35 @@ export const TranscriptionAside = () => {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function startTranscription() {
|
async function startTranscription() {
|
||||||
callObject.startTranscription();
|
setIsTranscribing(true);
|
||||||
|
await callObject.startTranscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopTranscription() {
|
async function stopTranscription() {
|
||||||
callObject.stopTranscription();
|
setIsTranscribing(false);
|
||||||
|
await callObject.stopTranscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Aside onClose={() => setShowAside(false)}>
|
<Aside onClose={() => setShowAside(false)}>
|
||||||
{isOwner && (
|
{isOwner && (
|
||||||
<div className="owner-actions">
|
<div className="owner-actions">
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
fullWidth
|
fullWidth
|
||||||
size="tiny"
|
size="tiny"
|
||||||
|
disabled={isTranscribing}
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
startTranscription()
|
startTranscription()
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
Start transcribing
|
{isTranscribing ? 'Transcribing' : 'Start transcribing'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
fullWidth
|
fullWidth
|
||||||
size="tiny"
|
size="tiny"
|
||||||
|
disabled={!isTranscribing}
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
stopTranscription()
|
stopTranscription()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,16 +15,19 @@ export const TranscriptionProvider = ({ children }) => {
|
||||||
const { callObject } = useCallState();
|
const { callObject } = useCallState();
|
||||||
const [transcriptionHistory, setTranscriptionHistory] = useState([]);
|
const [transcriptionHistory, setTranscriptionHistory] = useState([]);
|
||||||
const [hasNewMessages, setHasNewMessages] = useState(false);
|
const [hasNewMessages, setHasNewMessages] = useState(false);
|
||||||
|
const [isTranscribing, setIsTranscribing] = useState(false);
|
||||||
|
|
||||||
const handleNewMessage = useCallback(
|
const handleNewMessage = useCallback(
|
||||||
(e) => {
|
(e) => {
|
||||||
|
const participants = callObject.participants();
|
||||||
|
// Collect only transcription messages, and gather enough
|
||||||
|
// words to be able to post messages at sentence intervals
|
||||||
if (e.fromId === 'transcription' && e.data?.is_final) {
|
if (e.fromId === 'transcription' && e.data?.is_final) {
|
||||||
console.log(`${JSON.stringify(e.data)}: ${JSON.stringify(callObject.participants())}`)
|
|
||||||
|
|
||||||
const sender = e.data.user_name
|
|
||||||
? e.data.user_name
|
|
||||||
: 'Guest';
|
|
||||||
|
|
||||||
|
// Get the sender's current display name or the local name
|
||||||
|
const sender = e.data?.session_id !== participants.local.session_id
|
||||||
|
? participants[e.data.session_id].user_name
|
||||||
|
: participants.local.user_name;
|
||||||
|
|
||||||
setTranscriptionHistory((oldState) => [
|
setTranscriptionHistory((oldState) => [
|
||||||
...oldState,
|
...oldState,
|
||||||
|
|
@ -37,6 +40,16 @@ export const TranscriptionProvider = ({ children }) => {
|
||||||
[callObject]
|
[callObject]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const handleTranscriptionStarted = useCallback(() => {
|
||||||
|
console.log('💬 Live stream started');
|
||||||
|
setIsTranscribing(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleTranscriptionStopped = useCallback(() => {
|
||||||
|
console.log('💬 Live stream stopped');
|
||||||
|
setIsTranscribing(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!callObject) {
|
if (!callObject) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -46,12 +59,16 @@ export const TranscriptionProvider = ({ children }) => {
|
||||||
|
|
||||||
callObject.on('app-message', handleNewMessage);
|
callObject.on('app-message', handleNewMessage);
|
||||||
|
|
||||||
|
callObject.on('transcription-started', handleTranscriptionStarted);
|
||||||
|
callObject.on('transcription-stopped', handleTranscriptionStopped);
|
||||||
|
|
||||||
return () => callObject.off('app-message', handleNewMessage);
|
return () => callObject.off('app-message', handleNewMessage);
|
||||||
}, [callObject, handleNewMessage]);
|
}, [callObject, handleNewMessage, handleTranscriptionStarted, handleTranscriptionStopped]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TranscriptionContext.Provider
|
<TranscriptionContext.Provider
|
||||||
value={{
|
value={{
|
||||||
|
isTranscribing,
|
||||||
transcriptionHistory,
|
transcriptionHistory,
|
||||||
hasNewMessages,
|
hasNewMessages,
|
||||||
setHasNewMessages,
|
setHasNewMessages,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue