Skip to content

Commit 91e91e3

Browse files
Persist device preference in localStorage
1 parent 0250eb2 commit 91e91e3

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ const usePreviewPublisher = (): PreviewPublisherContextType => {
226226
if (publisherRef.current) {
227227
return;
228228
}
229+
// We reset user preferences as we want to start with both devices enabled
230+
setStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED, JSON.stringify(true));
231+
setStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED, JSON.stringify(true));
229232

230233
// Set videoFilter based on user's selected background
231234
let videoFilter: VideoFilter | undefined;
@@ -280,6 +283,7 @@ const usePreviewPublisher = (): PreviewPublisherContextType => {
280283
return;
281284
}
282285
publisherRef.current.publishVideo(!isVideoEnabled);
286+
setStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED, JSON.stringify(!isVideoEnabled));
283287
setIsVideoEnabled(!isVideoEnabled);
284288
if (setUser) {
285289
setUser((prevUser: UserType) => ({
@@ -304,6 +308,7 @@ const usePreviewPublisher = (): PreviewPublisherContextType => {
304308
}
305309
publisherRef.current.publishAudio(!isAudioEnabled);
306310
setIsAudioEnabled(!isAudioEnabled);
311+
setStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED, JSON.stringify(!isAudioEnabled));
307312
if (setUser) {
308313
setUser((prevUser: UserType) => ({
309314
...prevUser,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import getAccessDeniedError, {
1515
PublishingErrorType,
1616
} from '../../../utils/getAccessDeniedError/getAccessDeniedError';
1717
import applyBackgroundFilter from '../../../utils/backgroundFilter/applyBackgroundFilter/applyBackgroundFilter';
18+
import { setStorageItem, STORAGE_KEYS } from '../../../utils/storage';
1819

1920
type PublisherStreamCreatedEvent = Event<'streamCreated', Publisher> & {
2021
stream: Stream;
@@ -285,6 +286,7 @@ const usePublisher = (): PublisherContextType => {
285286
}
286287
publisherRef.current.publishVideo(!isVideoEnabled);
287288
setIsVideoEnabled(!isVideoEnabled);
289+
setStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED, JSON.stringify(!isVideoEnabled));
288290
};
289291

290292
/**
@@ -299,6 +301,7 @@ const usePublisher = (): PublisherContextType => {
299301
}
300302
publisherRef.current.publishAudio(!isAudioEnabled);
301303
setIsAudioEnabled(!isAudioEnabled);
304+
setStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED, JSON.stringify(!isAudioEnabled));
302305
setIsForceMuted(false);
303306
};
304307

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,18 @@ describe('usePublisherOptions', () => {
140140
});
141141
});
142142
});
143+
144+
it('should disable audio and video from storage options', async () => {
145+
vi.spyOn(OT, 'hasMediaProcessorSupport').mockReturnValue(true);
146+
setStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED, 'false');
147+
setStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED, 'true');
148+
149+
await deviceStore.init();
150+
mockUseUserContext.mockImplementation(() => mockUserContextWithCustomSettings);
151+
const { result } = renderHook(() => usePublisherOptions());
152+
await waitFor(() => {
153+
expect(result.current?.publishAudio).toBe(false);
154+
expect(result.current?.publishVideo).toBe(true);
155+
});
156+
});
143157
});

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import useUserContext from '../../../hooks/useUserContext';
99
import getInitials from '../../../utils/getInitials';
1010
import DeviceStore from '../../../utils/DeviceStore';
11+
import { getStorageItem, STORAGE_KEYS } from '../../../utils/storage';
1112

1213
/**
1314
* React hook to get PublisherProperties combining default options and options set in UserContext
@@ -53,8 +54,10 @@ const usePublisherOptions = (): PublisherProperties | null => {
5354
initials,
5455
insertDefaultUI: false,
5556
name,
56-
publishAudio: !!publishAudio,
57-
publishVideo: !!publishVideo,
57+
publishAudio:
58+
!!publishAudio && getStorageItem(STORAGE_KEYS.AUDIO_SOURCE_ENABLED) !== 'false',
59+
publishVideo:
60+
!!publishVideo && getStorageItem(STORAGE_KEYS.VIDEO_SOURCE_ENABLED) !== 'false',
5861
resolution: '1280x720',
5962
audioFilter,
6063
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)