Adopt room context provider pattern (#412)

This commit is contained in:
lukasIO 2025-04-15 20:26:07 +02:00 committed by GitHub
parent 7396247483
commit 8b2ee6c324
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 34 deletions

View File

@ -1,6 +1,6 @@
'use client'; 'use client';
import { formatChatMessageLinks, LiveKitRoom, VideoConference } from '@livekit/components-react'; import { formatChatMessageLinks, RoomContext, VideoConference } from '@livekit/components-react';
import { import {
ExternalE2EEKeyProvider, ExternalE2EEKeyProvider,
LogLevel, LogLevel,
@ -11,7 +11,7 @@ import {
type VideoCodec, type VideoCodec,
} from 'livekit-client'; } from 'livekit-client';
import { DebugMode } from '@/lib/Debug'; import { DebugMode } from '@/lib/Debug';
import { useMemo } from 'react'; import { useEffect, useMemo } from 'react';
import { decodePassphrase } from '@/lib/client-utils'; import { decodePassphrase } from '@/lib/client-utils';
import { SettingsMenu } from '@/lib/SettingsMenu'; import { SettingsMenu } from '@/lib/SettingsMenu';
@ -57,22 +57,26 @@ export function VideoConferenceClientImpl(props: {
}; };
}, []); }, []);
useEffect(() => {
room.connect(props.liveKitUrl, props.token, connectOptions).catch((error) => {
console.error(error);
});
room.localParticipant.enableCameraAndMicrophone().catch((error) => {
console.error(error);
});
}, [room, props.liveKitUrl, props.token, connectOptions]);
return ( return (
<LiveKitRoom <div className="lk-room-container">
room={room} <RoomContext.Provider value={room}>
token={props.token} <VideoConference
connectOptions={connectOptions} chatMessageFormatter={formatChatMessageLinks}
serverUrl={props.liveKitUrl} SettingsComponent={
audio={true} process.env.NEXT_PUBLIC_SHOW_SETTINGS_MENU === 'true' ? SettingsMenu : undefined
video={true} }
> />
<VideoConference <DebugMode logLevel={LogLevel.debug} />
chatMessageFormatter={formatChatMessageLinks} </RoomContext.Provider>
SettingsComponent={ </div>
process.env.NEXT_PUBLIC_SHOW_SETTINGS_MENU === 'true' ? SettingsMenu : undefined
}
/>
<DebugMode logLevel={LogLevel.debug} />
</LiveKitRoom>
); );
} }

View File

@ -7,9 +7,9 @@ import { SettingsMenu } from '@/lib/SettingsMenu';
import { ConnectionDetails } from '@/lib/types'; import { ConnectionDetails } from '@/lib/types';
import { import {
formatChatMessageLinks, formatChatMessageLinks,
LiveKitRoom,
LocalUserChoices, LocalUserChoices,
PreJoin, PreJoin,
RoomContext,
VideoConference, VideoConference,
} from '@livekit/components-react'; } from '@livekit/components-react';
import { import {
@ -20,6 +20,7 @@ import {
Room, Room,
DeviceUnsupportedError, DeviceUnsupportedError,
RoomConnectOptions, RoomConnectOptions,
RoomEvent,
} from 'livekit-client'; } from 'livekit-client';
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import React from 'react'; import React from 'react';
@ -164,6 +165,38 @@ function VideoConferenceComponent(props: {
}; };
}, []); }, []);
React.useEffect(() => {
room.on(RoomEvent.Disconnected, handleOnLeave);
room.on(RoomEvent.EncryptionError, handleEncryptionError);
room.on(RoomEvent.MediaDevicesError, handleError);
if (e2eeSetupComplete) {
room
.connect(
props.connectionDetails.serverUrl,
props.connectionDetails.participantToken,
connectOptions,
)
.catch((error) => {
handleError(error);
});
if (props.userChoices.videoEnabled) {
room.localParticipant.setCameraEnabled(true).catch((error) => {
handleError(error);
});
}
if (props.userChoices.audioEnabled) {
room.localParticipant.setMicrophoneEnabled(true).catch((error) => {
handleError(error);
});
}
}
return () => {
room.off(RoomEvent.Disconnected, handleOnLeave);
room.off(RoomEvent.EncryptionError, handleEncryptionError);
room.off(RoomEvent.MediaDevicesError, handleError);
};
}, [e2eeSetupComplete, room, props.connectionDetails, props.userChoices]);
const router = useRouter(); const router = useRouter();
const handleOnLeave = React.useCallback(() => router.push('/'), [router]); const handleOnLeave = React.useCallback(() => router.push('/'), [router]);
const handleError = React.useCallback((error: Error) => { const handleError = React.useCallback((error: Error) => {
@ -178,26 +211,15 @@ function VideoConferenceComponent(props: {
}, []); }, []);
return ( return (
<> <div className="lk-room-container">
<LiveKitRoom <RoomContext.Provider value={room}>
connect={e2eeSetupComplete}
room={room}
token={props.connectionDetails.participantToken}
serverUrl={props.connectionDetails.serverUrl}
connectOptions={connectOptions}
video={props.userChoices.videoEnabled}
audio={props.userChoices.audioEnabled}
onDisconnected={handleOnLeave}
onEncryptionError={handleEncryptionError}
onError={handleError}
>
<VideoConference <VideoConference
chatMessageFormatter={formatChatMessageLinks} chatMessageFormatter={formatChatMessageLinks}
SettingsComponent={SHOW_SETTINGS_MENU ? SettingsMenu : undefined} SettingsComponent={SHOW_SETTINGS_MENU ? SettingsMenu : undefined}
/> />
<DebugMode /> <DebugMode />
<RecordingIndicator /> <RecordingIndicator />
</LiveKitRoom> </RoomContext.Provider>
</> </div>
); );
} }