Skip to content

Commit fe3fa51

Browse files
Persist device preference in localStorage
1 parent 1a694e0 commit fe3fa51

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

frontend/src/Context/PreviewPublisherProvider/usePreviewPublisher/usePreviewPublisher.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ const usePreviewPublisher = (): PreviewPublisherContextType => {
229229
if (publisherRef.current) {
230230
return;
231231
}
232+
// We reset user preferences as we want to start with both devices enabled
233+
setStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED, 'true');
234+
setStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED, 'true');
232235

233236
// Set videoFilter based on user's selected background
234237
let videoFilter: VideoFilter | undefined;
@@ -283,6 +286,7 @@ const usePreviewPublisher = (): PreviewPublisherContextType => {
283286
return;
284287
}
285288
publisherRef.current.publishVideo(!isVideoEnabled);
289+
setStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED, isVideoEnabled ? 'true' : 'false');
286290
setIsVideoEnabled(!isVideoEnabled);
287291
if (setUser) {
288292
setUser((prevUser: UserType) => ({
@@ -307,6 +311,7 @@ const usePreviewPublisher = (): PreviewPublisherContextType => {
307311
}
308312
publisherRef.current.publishAudio(!isAudioEnabled);
309313
setIsAudioEnabled(!isAudioEnabled);
314+
setStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED, isAudioEnabled ? 'true' : 'false');
310315
if (setUser) {
311316
setUser((prevUser: UserType) => ({
312317
...prevUser,

frontend/src/Context/PublisherProvider/usePublisher/usePublisher.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import usePublisherQuality, { NetworkQuality } from '../usePublisherQuality/useP
1212
import usePublisherOptions from '../usePublisherOptions';
1313
import useSessionContext from '../../../hooks/useSessionContext';
1414
import applyBackgroundFilter from '../../../utils/backgroundFilter/applyBackgroundFilter/applyBackgroundFilter';
15+
import { setStorageItem, STORAGE_KEYS } from '../../../utils/storage';
1516

1617
type PublisherStreamCreatedEvent = Event<'streamCreated', Publisher> & {
1718
stream: Stream;
@@ -291,6 +292,7 @@ const usePublisher = (): PublisherContextType => {
291292
}
292293
publisherRef.current.publishVideo(!isVideoEnabled);
293294
setIsVideoEnabled(!isVideoEnabled);
295+
setStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED, isVideoEnabled ? 'true' : 'false');
294296
};
295297

296298
/**
@@ -305,6 +307,7 @@ const usePublisher = (): PublisherContextType => {
305307
}
306308
publisherRef.current.publishAudio(!isAudioEnabled);
307309
setIsAudioEnabled(!isAudioEnabled);
310+
setStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED, isAudioEnabled ? 'true' : 'false');
308311
setIsForceMuted(false);
309312
};
310313

frontend/src/Context/PublisherProvider/usePublisherOptions/usePublisherOptions.spec.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,18 @@ describe('usePublisherOptions', () => {
182182
});
183183
});
184184
});
185+
186+
it('should disable audio and video from storage options', async () => {
187+
vi.spyOn(OT, 'hasMediaProcessorSupport').mockReturnValue(true);
188+
setStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED, 'false');
189+
setStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED, 'true');
190+
191+
await deviceStore.init();
192+
mockUseUserContext.mockImplementation(() => mockUserContextWithCustomSettings);
193+
const { result } = renderHook(() => usePublisherOptions());
194+
await waitFor(() => {
195+
expect(result.current?.publishAudio).toBe(false);
196+
expect(result.current?.publishVideo).toBe(true);
197+
});
198+
});
185199
});

frontend/src/Context/PublisherProvider/usePublisherOptions/usePublisherOptions.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import useUserContext from '../../../hooks/useUserContext';
99
import getInitials from '../../../utils/getInitials';
1010
import DeviceStore from '../../../utils/DeviceStore';
1111
import useConfigContext from '../../../hooks/useConfigContext';
12+
import { getStorageItem, STORAGE_KEYS } from '../../../utils/storage';
1213

1314
/**
1415
* React hook to get PublisherProperties combining default options and options set in UserContext
@@ -51,14 +52,17 @@ const usePublisherOptions = (): PublisherProperties | null => {
5152
const videoFilter: VideoFilter | undefined =
5253
backgroundFilter && hasMediaProcessorSupport() ? backgroundFilter : undefined;
5354

55+
const isAudioEnabled = getStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED) === 'true';
56+
const isVideoEnabled = getStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED) === 'true';
57+
5458
setPublisherOptions({
5559
audioFallback: { publisher: true },
5660
audioSource,
5761
initials,
5862
insertDefaultUI: false,
5963
name,
60-
publishAudio: allowAudioOnJoin && publishAudio,
61-
publishVideo: allowVideoOnJoin && publishVideo,
64+
publishAudio: allowAudioOnJoin && publishAudio && isAudioEnabled,
65+
publishVideo: allowVideoOnJoin && publishVideo && isVideoEnabled,
6266
resolution: defaultResolution,
6367
audioFilter,
6468
videoFilter,

frontend/src/utils/storage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export const STORAGE_KEYS = {
22
AUDIO_SOURCE: 'audioSource',
3+
AUDIO_SOURCE_ENABLED: 'audioSourceEnabled',
34
VIDEO_SOURCE: 'videoSource',
5+
VIDEO_SOURCE_ENABLED: 'videoSourceEnabled',
46
NOISE_SUPPRESSION: 'noiseSuppression',
57
BACKGROUND_REPLACEMENT: 'backgroundReplacement',
68
USERNAME: 'username',

0 commit comments

Comments
 (0)