Skip to content

Commit

Permalink
Update user choices types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocupe committed Nov 7, 2023
1 parent 6ad15f8 commit d222656
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 56 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/persistent-storage/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export { saveUserChoices, loadUserChoices, type UserChoices } from './user-choices';
export {
saveUserChoices,
loadUserChoices,
type UserChoices,
type LocalUserChoices,
} from './user-choices';
37 changes: 22 additions & 15 deletions packages/core/src/persistent-storage/user-choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,51 @@ 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 `''`
*/
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.
*/
Expand All @@ -58,7 +65,7 @@ export function saveUserChoices(
if (preventSave === true) {
return;
}
setLocalStorageObject(USER_CHOICES_KEY, deviceSettings);
setLocalStorageObject(USER_CHOICES_KEY, userChoices);
}

/**
Expand All @@ -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,
};

Expand Down
12 changes: 6 additions & 6 deletions packages/react/src/hooks/usePersistentUserChoices.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -10,7 +10,7 @@ export interface UsePersistentUserChoicesOptions {
/**
* The default value to use if reading from local storage returns no results or fails.
*/
defaults?: Partial<UserChoices>;
defaults?: Partial<LocalUserChoices>;
/**
* Whether to prevent saving to persistent storage.
* @defaultValue false
Expand All @@ -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 }));
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export type {
ReceivedChatMessage,
MessageDecoder,
MessageEncoder,
LocalUserChoices,
UserChoices,
} from '@livekit/components-core';
36 changes: 9 additions & 27 deletions packages/react/src/prefabs/PreJoin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<UserChoices> = {
const partialDefaults: Partial<LocalUserChoices> = {
...(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 }),
};
Expand All @@ -264,17 +250,13 @@ export function PreJoin({
});

// Initialize device settings
const [audioEnabled, setAudioEnabled] = React.useState<boolean>(
initialUserChoices.audioInputEnabled,
);
const [videoEnabled, setVideoEnabled] = React.useState<boolean>(
initialUserChoices.videoInputEnabled,
);
const [audioEnabled, setAudioEnabled] = React.useState<boolean>(initialUserChoices.audioEnabled);
const [videoEnabled, setVideoEnabled] = React.useState<boolean>(initialUserChoices.videoEnabled);
const [audioDeviceId, setAudioDeviceId] = React.useState<string>(
initialUserChoices.audioInputDeviceId,
initialUserChoices.audioDeviceId,
);
const [videoDeviceId, setVideoDeviceId] = React.useState<string>(
initialUserChoices.videoInputDeviceId,
initialUserChoices.videoDeviceId,
);
const [username, setUsername] = React.useState(initialUserChoices.username);
// TODO: Remove `e2ee` and `sharedPassphrase` once deprecated `LocalUserChoices` type is removed.
Expand Down Expand Up @@ -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,
);
Expand Down
8 changes: 1 addition & 7 deletions packages/react/src/prefabs/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit d222656

Please sign in to comment.