2026-03-07 18:03:32 +11:00
|
|
|
import { createContext, useCallback, useContext } from 'react';
|
|
|
|
|
import { useAtom } from 'jotai';
|
|
|
|
|
import { CallPreferences, CallPreferencesAtom } from '../callPreferences';
|
2026-05-14 11:07:10 -04:00
|
|
|
import { useSetting } from './settings';
|
|
|
|
|
import { settingsAtom } from '../settings';
|
2026-03-07 18:03:32 +11:00
|
|
|
|
|
|
|
|
const CallPreferencesAtomContext = createContext<CallPreferencesAtom | null>(null);
|
|
|
|
|
export const CallPreferencesProvider = CallPreferencesAtomContext.Provider;
|
|
|
|
|
|
|
|
|
|
export const useCallPreferencesAtom = (): CallPreferencesAtom => {
|
|
|
|
|
const atom = useContext(CallPreferencesAtomContext);
|
|
|
|
|
if (!atom) {
|
|
|
|
|
throw new Error('CallPreferencesAtom not provided!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return atom;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useCallPreferences = (): CallPreferences & {
|
|
|
|
|
toggleMicrophone: () => void;
|
|
|
|
|
toggleVideo: () => void;
|
|
|
|
|
toggleSound: () => void;
|
|
|
|
|
} => {
|
|
|
|
|
const callPrefAtom = useCallPreferencesAtom();
|
|
|
|
|
const [pref, setPref] = useAtom(callPrefAtom);
|
2026-05-14 11:07:10 -04:00
|
|
|
const [cameraOnJoin] = useSetting(settingsAtom, 'cameraOnJoin');
|
2026-03-07 18:03:32 +11:00
|
|
|
|
|
|
|
|
const toggleMicrophone = useCallback(() => {
|
|
|
|
|
const microphone = !pref.microphone;
|
|
|
|
|
|
|
|
|
|
setPref({
|
|
|
|
|
microphone,
|
|
|
|
|
video: pref.video,
|
|
|
|
|
sound: !pref.sound && microphone ? true : pref.sound,
|
|
|
|
|
});
|
|
|
|
|
}, [setPref, pref]);
|
|
|
|
|
|
|
|
|
|
const toggleVideo = useCallback(() => {
|
|
|
|
|
const video = !pref.video;
|
|
|
|
|
|
|
|
|
|
setPref({
|
|
|
|
|
microphone: pref.microphone,
|
|
|
|
|
video,
|
|
|
|
|
sound: pref.sound,
|
|
|
|
|
});
|
|
|
|
|
}, [setPref, pref]);
|
|
|
|
|
|
|
|
|
|
const toggleSound = useCallback(() => {
|
|
|
|
|
const sound = !pref.sound;
|
|
|
|
|
|
|
|
|
|
setPref({
|
|
|
|
|
microphone: !sound ? false : pref.microphone,
|
|
|
|
|
video: pref.video,
|
|
|
|
|
sound,
|
|
|
|
|
});
|
|
|
|
|
}, [setPref, pref]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...pref,
|
2026-05-14 11:07:10 -04:00
|
|
|
video: cameraOnJoin ? pref.video : false,
|
2026-03-07 18:03:32 +11:00
|
|
|
toggleMicrophone,
|
|
|
|
|
toggleVideo,
|
|
|
|
|
toggleSound,
|
|
|
|
|
};
|
|
|
|
|
};
|