From d222656a01e88363bd7567843d1fba5fc625ac52 Mon Sep 17 00:00:00 2001 From: ocupe Date: Tue, 7 Nov 2023 15:01:59 +0100 Subject: [PATCH] Update user choices types --- packages/core/src/persistent-storage/index.ts | 7 +++- .../src/persistent-storage/user-choices.ts | 37 +++++++++++-------- .../src/hooks/usePersistentUserChoices.ts | 12 +++--- packages/react/src/index.ts | 1 + packages/react/src/prefabs/PreJoin.tsx | 36 +++++------------- packages/react/src/prefabs/index.ts | 8 +--- 6 files changed, 45 insertions(+), 56 deletions(-) diff --git a/packages/core/src/persistent-storage/index.ts b/packages/core/src/persistent-storage/index.ts index 3e3a3d964..2f9015594 100644 --- a/packages/core/src/persistent-storage/index.ts +++ b/packages/core/src/persistent-storage/index.ts @@ -1 +1,6 @@ -export { saveUserChoices, loadUserChoices, type UserChoices } from './user-choices'; +export { + saveUserChoices, + loadUserChoices, + type UserChoices, + type LocalUserChoices, +} from './user-choices'; diff --git a/packages/core/src/persistent-storage/user-choices.ts b/packages/core/src/persistent-storage/user-choices.ts index f863be4f1..6eb57d429 100644 --- a/packages/core/src/persistent-storage/user-choices.ts +++ b/packages/core/src/persistent-storage/user-choices.ts @@ -12,22 +12,22 @@ export type UserChoices = { * Whether video input is enabled. * @defaultValue `true` */ - videoInputEnabled: boolean; + videoEnabled: boolean; /** * Whether audio input is enabled. * @defaultValue `true` */ - audioInputEnabled: boolean; + audioEnabled: boolean; /** * The device ID of the video input device to use. * @defaultValue `''` */ - videoInputDeviceId: string; + videoDeviceId: string; /** * The device ID of the audio input device to use. * @defaultValue `''` */ - audioInputDeviceId: string; + audioDeviceId: string; /** * The username to use. * @defaultValue `''` @@ -35,21 +35,28 @@ export type UserChoices = { username: string; }; +export type LocalUserChoices = UserChoices & { + /** @deprecated This property will be removed without replacement. */ + e2ee: boolean; + /** @deprecated This property will be removed without replacement. */ + sharedPassphrase: string; +}; + const defaultUserChoices: UserChoices = { - videoInputEnabled: true, - audioInputEnabled: true, - videoInputDeviceId: '', - audioInputDeviceId: '', + videoEnabled: true, + audioEnabled: true, + videoDeviceId: '', + audioDeviceId: '', username: '', } as const; /** * Saves user choices to local storage. - * @param deviceSettings - The device settings to be stored. + * @param userChoices - The device settings to be stored. * @alpha */ export function saveUserChoices( - deviceSettings: UserChoices, + userChoices: UserChoices, /** * Whether to prevent saving user choices to local storage. */ @@ -58,7 +65,7 @@ export function saveUserChoices( if (preventSave === true) { return; } - setLocalStorageObject(USER_CHOICES_KEY, deviceSettings); + setLocalStorageObject(USER_CHOICES_KEY, userChoices); } /** @@ -77,10 +84,10 @@ export function loadUserChoices( preventLoad: boolean = false, ): UserChoices { const fallback: UserChoices = { - videoInputEnabled: defaults?.videoInputEnabled ?? defaultUserChoices.videoInputEnabled, - audioInputEnabled: defaults?.audioInputEnabled ?? defaultUserChoices.audioInputEnabled, - videoInputDeviceId: defaults?.videoInputDeviceId ?? defaultUserChoices.videoInputDeviceId, - audioInputDeviceId: defaults?.audioInputDeviceId ?? defaultUserChoices.audioInputDeviceId, + videoEnabled: defaults?.videoEnabled ?? defaultUserChoices.videoEnabled, + audioEnabled: defaults?.audioEnabled ?? defaultUserChoices.audioEnabled, + videoDeviceId: defaults?.videoDeviceId ?? defaultUserChoices.videoDeviceId, + audioDeviceId: defaults?.audioDeviceId ?? defaultUserChoices.audioDeviceId, username: defaults?.username ?? defaultUserChoices.username, }; diff --git a/packages/react/src/hooks/usePersistentUserChoices.ts b/packages/react/src/hooks/usePersistentUserChoices.ts index c313085dc..ce732d7c7 100644 --- a/packages/react/src/hooks/usePersistentUserChoices.ts +++ b/packages/react/src/hooks/usePersistentUserChoices.ts @@ -1,4 +1,4 @@ -import type { UserChoices } from '@livekit/components-core'; +import type { LocalUserChoices, UserChoices } from '@livekit/components-core'; import { loadUserChoices, saveUserChoices } from '@livekit/components-core'; import * as React from 'react'; @@ -10,7 +10,7 @@ export interface UsePersistentUserChoicesOptions { /** * The default value to use if reading from local storage returns no results or fails. */ - defaults?: Partial; + defaults?: Partial; /** * Whether to prevent saving to persistent storage. * @defaultValue false @@ -34,16 +34,16 @@ export function usePersistentUserChoices(options: UsePersistentUserChoicesOption ); const saveAudioInputEnabled = React.useCallback((isEnabled: boolean) => { - setSettings((prev) => ({ ...prev, audioInputEnabled: isEnabled })); + setSettings((prev) => ({ ...prev, audioEnabled: isEnabled })); }, []); const saveVideoInputEnabled = React.useCallback((isEnabled: boolean) => { - setSettings((prev) => ({ ...prev, videoInputEnabled: isEnabled })); + setSettings((prev) => ({ ...prev, videoEnabled: isEnabled })); }, []); const saveAudioInputDeviceId = React.useCallback((deviceId: string) => { - setSettings((prev) => ({ ...prev, audioInputDeviceId: deviceId })); + setSettings((prev) => ({ ...prev, audioDeviceId: deviceId })); }, []); const saveVideoInputDeviceId = React.useCallback((deviceId: string) => { - setSettings((prev) => ({ ...prev, videoInputDeviceId: deviceId })); + setSettings((prev) => ({ ...prev, videoDeviceId: deviceId })); }, []); const saveUsername = React.useCallback((username: string) => { setSettings((prev) => ({ ...prev, username: username })); diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts index 937b15f7f..94d5e2f29 100644 --- a/packages/react/src/index.ts +++ b/packages/react/src/index.ts @@ -13,5 +13,6 @@ export type { ReceivedChatMessage, MessageDecoder, MessageEncoder, + LocalUserChoices, UserChoices, } from '@livekit/components-core'; diff --git a/packages/react/src/prefabs/PreJoin.tsx b/packages/react/src/prefabs/PreJoin.tsx index 70d8464d8..f76e66225 100644 --- a/packages/react/src/prefabs/PreJoin.tsx +++ b/packages/react/src/prefabs/PreJoin.tsx @@ -15,25 +15,11 @@ import { import * as React from 'react'; import { MediaDeviceMenu } from './MediaDeviceMenu'; import { TrackToggle } from '../components/controls/TrackToggle'; -import type { UserChoices } from '@livekit/components-core'; +import type { LocalUserChoices } from '@livekit/components-core'; import { log } from '@livekit/components-core'; import { ParticipantPlaceholder } from '../assets/images'; import { useMediaDevices, usePersistentUserChoices } from '../hooks'; -/** - * @deprecated Use `UserChoices` instead. - * @public - */ -export type LocalUserChoices = { - username: string; - videoEnabled: boolean; - audioEnabled: boolean; - videoDeviceId: string; - audioDeviceId: string; - e2ee: boolean; - sharedPassphrase: string; -}; - const DEFAULT_USER_CHOICES: LocalUserChoices = { username: '', videoEnabled: true, @@ -242,10 +228,10 @@ export function PreJoin({ const [userChoices, setUserChoices] = React.useState(DEFAULT_USER_CHOICES); // TODO: Remove and pipe `defaults` object directly into `usePersistentUserChoices` once we fully switch from type `LocalUserChoices` to `UserChoices`. - const partialDefaults: Partial = { + const partialDefaults: Partial = { ...(defaults.audioDeviceId !== undefined && { audioInputDeviceId: defaults.audioDeviceId }), ...(defaults.videoDeviceId !== undefined && { videoInputDeviceId: defaults.videoDeviceId }), - ...(defaults.audioEnabled !== undefined && { audioInputEnabled: defaults.audioEnabled }), + ...(defaults.audioEnabled !== undefined && { audioEnabled: defaults.audioEnabled }), ...(defaults.videoEnabled !== undefined && { videoInputEnabled: defaults.videoEnabled }), ...(defaults.username !== undefined && { username: defaults.username }), }; @@ -264,17 +250,13 @@ export function PreJoin({ }); // Initialize device settings - const [audioEnabled, setAudioEnabled] = React.useState( - initialUserChoices.audioInputEnabled, - ); - const [videoEnabled, setVideoEnabled] = React.useState( - initialUserChoices.videoInputEnabled, - ); + const [audioEnabled, setAudioEnabled] = React.useState(initialUserChoices.audioEnabled); + const [videoEnabled, setVideoEnabled] = React.useState(initialUserChoices.videoEnabled); const [audioDeviceId, setAudioDeviceId] = React.useState( - initialUserChoices.audioInputDeviceId, + initialUserChoices.audioDeviceId, ); const [videoDeviceId, setVideoDeviceId] = React.useState( - initialUserChoices.videoInputDeviceId, + initialUserChoices.videoDeviceId, ); const [username, setUsername] = React.useState(initialUserChoices.username); // TODO: Remove `e2ee` and `sharedPassphrase` once deprecated `LocalUserChoices` type is removed. @@ -302,8 +284,8 @@ export function PreJoin({ const tracks = usePreviewTracks( { - audio: audioEnabled ? { deviceId: initialUserChoices.audioInputDeviceId } : false, - video: videoEnabled ? { deviceId: initialUserChoices.videoInputDeviceId } : false, + audio: audioEnabled ? { deviceId: initialUserChoices.audioDeviceId } : false, + video: videoEnabled ? { deviceId: initialUserChoices.videoDeviceId } : false, }, onError, ); diff --git a/packages/react/src/prefabs/index.ts b/packages/react/src/prefabs/index.ts index e63bfdd77..a8a5b2888 100644 --- a/packages/react/src/prefabs/index.ts +++ b/packages/react/src/prefabs/index.ts @@ -1,11 +1,5 @@ export { Chat, type ChatProps } from './Chat'; -export { - PreJoin, - PreJoinProps, - usePreviewDevice, - usePreviewTracks, - type LocalUserChoices, -} from './PreJoin'; +export { PreJoin, PreJoinProps, usePreviewDevice, usePreviewTracks } from './PreJoin'; export { VideoConference, type VideoConferenceProps } from './VideoConference'; export { ControlBar, type ControlBarProps, type ControlBarControls } from './ControlBar'; export { MediaDeviceMenu, type MediaDeviceMenuProps } from './MediaDeviceMenu';