diff --git a/packages/core/src/persistent-storage/device-settings.ts b/packages/core/src/persistent-storage/device-settings.ts deleted file mode 100644 index dd49ddd35..000000000 --- a/packages/core/src/persistent-storage/device-settings.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { cssPrefix } from '../constants'; -import { getLocalStorageObject, setLocalStorageObject } from './local-storage-helpers'; - -const DEVICE_SELECTION_KEY = `${cssPrefix}-device-settings` as const; - -export type DeviceSettings = { - videoInputEnabled: boolean; - audioInputEnabled: boolean; - videoInputDeviceId: string; - audioInputDeviceId: string; -}; - -const defaultDeviceSettings: DeviceSettings = { - videoInputEnabled: true, - audioInputEnabled: true, - videoInputDeviceId: '', - audioInputDeviceId: '', -}; - -/** - * Sets the device settings in local storage. - * @param deviceSettings - The device settings to be stored. - * @alpha - */ -export function setDeviceSettings(deviceSettings: DeviceSettings): void { - setLocalStorageObject(DEVICE_SELECTION_KEY, deviceSettings); -} - -/** - * Retrieves the device settings from local storage, or returns the default settings if none are found. - * @param fallback - The default device settings to use if none are found in local storage. - * @returns The retrieved device settings, or the default settings if none are found. - * @alpha - */ -export function getDeviceSettings(fallback?: DeviceSettings): DeviceSettings { - return getLocalStorageObject(DEVICE_SELECTION_KEY) ?? fallback ?? defaultDeviceSettings; -} diff --git a/packages/core/src/persistent-storage/index.ts b/packages/core/src/persistent-storage/index.ts index 47f707b3a..3e3a3d964 100644 --- a/packages/core/src/persistent-storage/index.ts +++ b/packages/core/src/persistent-storage/index.ts @@ -1 +1 @@ -export { setDeviceSettings, getDeviceSettings, type DeviceSettings } from './device-settings'; +export { saveUserChoices, loadUserChoices, type UserChoices } from './user-choices'; diff --git a/packages/core/src/persistent-storage/user-choices.ts b/packages/core/src/persistent-storage/user-choices.ts new file mode 100644 index 000000000..774c03e20 --- /dev/null +++ b/packages/core/src/persistent-storage/user-choices.ts @@ -0,0 +1,38 @@ +import { cssPrefix } from '../constants'; +import { getLocalStorageObject, setLocalStorageObject } from './local-storage-helpers'; + +const USER_CHOICES_KEY = `${cssPrefix}-device-settings` as const; + +export type UserChoices = { + videoInputEnabled: boolean; + audioInputEnabled: boolean; + videoInputDeviceId: string; + audioInputDeviceId: string; + username: string; +}; + +const defaultUserChoices: UserChoices = { + videoInputEnabled: true, + audioInputEnabled: true, + videoInputDeviceId: '', + audioInputDeviceId: '', + username: '', +}; + +/** + * Sets the user choices in local storage. + * @param deviceSettings - The device settings to be stored. + * @alpha + */ +export function saveUserChoices(deviceSettings: UserChoices): void { + setLocalStorageObject(USER_CHOICES_KEY, deviceSettings); +} + +/** + * Reads the user choices from local storage, or returns the default settings if none are found. + * @param defaults - The default device settings to use if none are found in local storage. + * @alpha + */ +export function loadUserChoices(defaults?: UserChoices): UserChoices { + return getLocalStorageObject(USER_CHOICES_KEY) ?? defaults ?? defaultUserChoices; +} diff --git a/packages/react/src/hooks/usePersistentUserChoices.ts b/packages/react/src/hooks/usePersistentUserChoices.ts index a6c2cb0d7..3e657a9ad 100644 --- a/packages/react/src/hooks/usePersistentUserChoices.ts +++ b/packages/react/src/hooks/usePersistentUserChoices.ts @@ -1,5 +1,5 @@ -import type { DeviceSettings } from '@livekit/components-core'; -import { getDeviceSettings, setDeviceSettings } from '@livekit/components-core'; +import type { UserChoices } from '@livekit/components-core'; +import { loadUserChoices, saveUserChoices } from '@livekit/components-core'; import * as React from 'react'; /** @@ -10,7 +10,7 @@ interface UsePersistentUserChoicesOptions { /** * The default value to use if reading from local storage returns no results or fails. */ - defaults?: DeviceSettings; + defaults?: UserChoices; /** * Whether to prevent saving the device settings to local storage. * @defaultValue false @@ -20,12 +20,12 @@ interface UsePersistentUserChoicesOptions { /** * A hook that provides access to user choices stored in local storage, such as - * selected media devices and their current state, as well as the user name. + * selected media devices and their current state (on or off), as well as the user name. * @alpha */ export function usePersistentUserChoices(options: UsePersistentUserChoicesOptions = {}) { - const [deviceSettings, setSettings] = React.useState( - getDeviceSettings(options.defaults), + const [deviceSettings, setSettings] = React.useState( + loadUserChoices(options.defaults), ); const saveAudioInputEnabled = React.useCallback((isEnabled: boolean) => { @@ -45,7 +45,7 @@ export function usePersistentUserChoices(options: UsePersistentUserChoicesOption if (options.preventSave === true) { return; } - setDeviceSettings(deviceSettings); + saveUserChoices(deviceSettings); }, [deviceSettings, options.preventSave]); return { diff --git a/packages/react/src/prefabs/PreJoin.tsx b/packages/react/src/prefabs/PreJoin.tsx index 197b051c8..0388790fc 100644 --- a/packages/react/src/prefabs/PreJoin.tsx +++ b/packages/react/src/prefabs/PreJoin.tsx @@ -250,6 +250,7 @@ export function PreJoin({ videoInputDeviceId: DEFAULT_USER_CHOICES.videoDeviceId, audioInputEnabled: DEFAULT_USER_CHOICES.audioEnabled, videoInputEnabled: DEFAULT_USER_CHOICES.videoEnabled, + username: DEFAULT_USER_CHOICES.username, }, preventSave: !saveDeviceSettings, });