Merge pull request #80 from daily-demos/use-daily-event
use useDailyEvent hook - Daily react hooks
This commit is contained in:
commit
8bc1f0f7f3
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react';
|
||||
|
||||
import App from '@custom/basic-call/components/App';
|
||||
import { TranscriptionProvider } from '../contexts/TranscriptionProvider';
|
||||
import { TranscriptionProvider } from '@custom/shared/contexts/TranscriptionProvider';
|
||||
|
||||
// Extend our basic call app component with the Live Transcription context
|
||||
export const AppWithTranscription = () => (
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ import { Aside } from '@custom/shared/components/Aside';
|
|||
import Button from '@custom/shared/components/Button';
|
||||
import { useCallState } from '@custom/shared/contexts/CallProvider';
|
||||
import { useParticipants } from '@custom/shared/contexts/ParticipantsProvider';
|
||||
import { useTranscription } from '@custom/shared/contexts/TranscriptionProvider';
|
||||
import { useUIState } from '@custom/shared/contexts/UIStateProvider';
|
||||
import { useTranscription } from '../contexts/TranscriptionProvider';
|
||||
|
||||
export const TRANSCRIPTION_ASIDE = 'transcription';
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import React from 'react';
|
||||
|
||||
import { TrayButton } from '@custom/shared/components/Tray';
|
||||
import { useTranscription } from '@custom/shared/contexts/TranscriptionProvider';
|
||||
import { useUIState } from '@custom/shared/contexts/UIStateProvider';
|
||||
import { ReactComponent as IconTranscription } from '@custom/shared/icons/chat-md.svg';
|
||||
import { useTranscription } from '../contexts/TranscriptionProvider';
|
||||
import { TRANSCRIPTION_ASIDE } from './TranscriptionAside';
|
||||
|
||||
export const Tray = () => {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,11 @@ export async function getStaticProps() {
|
|||
|
||||
// Pass through domain as prop
|
||||
return {
|
||||
props: defaultProps,
|
||||
props: {
|
||||
...defaultProps,
|
||||
forceFetchToken: true,
|
||||
forceOwner: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -127,8 +127,9 @@ export const CallProvider = ({
|
|||
useEffect(() => {
|
||||
if (!daily) return;
|
||||
|
||||
if (cleanURLOnJoin)
|
||||
if (cleanURLOnJoin) {
|
||||
daily.on('joined-meeting', () => router.replace(`/${room}`));
|
||||
}
|
||||
|
||||
return () => daily.off('joined-meeting', () => router.replace(`/${room}`));
|
||||
}, [cleanURLOnJoin, daily, room, router]);
|
||||
|
|
|
|||
|
|
@ -2,24 +2,23 @@ import React, {
|
|||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
} from 'react';
|
||||
import { useCallState } from '@custom/shared/contexts/CallProvider';
|
||||
import { useDaily, useDailyEvent } from '@daily-co/daily-react-hooks';
|
||||
import { nanoid } from 'nanoid';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
export const TranscriptionContext = createContext();
|
||||
|
||||
export const TranscriptionProvider = ({ children }) => {
|
||||
const { callObject } = useCallState();
|
||||
const daily = useDaily();
|
||||
const [transcriptionHistory, setTranscriptionHistory] = useState([]);
|
||||
const [hasNewMessages, setHasNewMessages] = useState(false);
|
||||
const [isTranscribing, setIsTranscribing] = useState(false);
|
||||
|
||||
const handleNewMessage = useCallback(
|
||||
(e) => {
|
||||
const participants = callObject.participants();
|
||||
const participants = daily.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) {
|
||||
|
|
@ -37,7 +36,7 @@ export const TranscriptionProvider = ({ children }) => {
|
|||
|
||||
setHasNewMessages(true);
|
||||
},
|
||||
[callObject]
|
||||
[daily]
|
||||
);
|
||||
|
||||
const handleTranscriptionStarted = useCallback(() => {
|
||||
|
|
@ -55,21 +54,10 @@ export const TranscriptionProvider = ({ children }) => {
|
|||
setIsTranscribing(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!callObject) {
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log(`💬 Transcription provider listening for app messages`);
|
||||
|
||||
callObject.on('app-message', handleNewMessage);
|
||||
|
||||
callObject.on('transcription-started', handleTranscriptionStarted);
|
||||
callObject.on('transcription-stopped', handleTranscriptionStopped);
|
||||
callObject.on('transcription-error', handleTranscriptionError);
|
||||
|
||||
return () => callObject.off('app-message', handleNewMessage);
|
||||
}, [callObject, handleNewMessage, handleTranscriptionStarted, handleTranscriptionStopped, handleTranscriptionError]);
|
||||
useDailyEvent('app-message', handleNewMessage);
|
||||
useDailyEvent('transcription-started', handleTranscriptionStarted);
|
||||
useDailyEvent('transcription-stopped', handleTranscriptionStopped);
|
||||
useDailyEvent('transcription-error', handleTranscriptionError);
|
||||
|
||||
return (
|
||||
<TranscriptionContext.Provider
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
import { useCallState } from '../contexts/CallProvider';
|
||||
import { useDaily, useDailyEvent } from '@daily-co/daily-react-hooks';
|
||||
import { useSoundLoader } from './useSoundLoader';
|
||||
|
||||
/**
|
||||
* Convenience hook to play `join.mp3` when first other participants joins.
|
||||
*/
|
||||
export const useJoinSound = () => {
|
||||
const { callObject: daily } = useCallState();
|
||||
const daily = useDaily();
|
||||
const { joinSound } = useSoundLoader();
|
||||
const [playJoinSound, setPlayJoinSound] = useState(false);
|
||||
|
||||
|
|
@ -25,18 +25,12 @@ export const useJoinSound = () => {
|
|||
}, 2000);
|
||||
}, [daily]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!daily) return;
|
||||
const handleParticipantJoined = () => {
|
||||
// first other participant joined --> play sound
|
||||
if (!playJoinSound || Object.keys(daily.participants()).length !== 2)
|
||||
return;
|
||||
joinSound.play();
|
||||
};
|
||||
|
||||
daily.on('participant-joined', handleParticipantJoined);
|
||||
return () => {
|
||||
daily.off('participant-joined', handleParticipantJoined);
|
||||
};
|
||||
const handleParticipantJoined = useCallback(() => {
|
||||
// first other participant joined --> play sound
|
||||
if (!playJoinSound || Object.keys(daily.participants()).length !== 2)
|
||||
return;
|
||||
joinSound.play();
|
||||
}, [daily, joinSound, playJoinSound]);
|
||||
|
||||
useDailyEvent('participant-joined', handleParticipantJoined);
|
||||
};
|
||||
Loading…
Reference in New Issue