Ensure voice mute/deafen status persists across disconnects and re-joins

This commit is contained in:
Jannis Braun
2026-02-19 18:02:00 +01:00
parent 5fae725675
commit 6deed231ab
4 changed files with 153 additions and 135 deletions
+4 -2
View File
@@ -115,16 +115,18 @@ class ConnectionManager {
} }
leaveAllVoice(userId: string): string | null { leaveAllVoice(userId: string): string | null {
let leftChannelId: string | null = null;
for (const [channelId, users] of this.voiceStates) { for (const [channelId, users] of this.voiceStates) {
if (users.has(userId)) { if (users.has(userId)) {
users.delete(userId); users.delete(userId);
if (users.size === 0) { if (users.size === 0) {
this.voiceStates.delete(channelId); this.voiceStates.delete(channelId);
} }
return channelId; leftChannelId = channelId;
break;
} }
} }
return null; return leftChannelId;
} }
getVoiceUsers(channelId: string): Set<string> { getVoiceUsers(channelId: string): Set<string> {
+2 -2
View File
@@ -222,7 +222,7 @@ export function useLiveKit() {
} }
} catch (err) { if (gen === _connectGeneration) setConnectionError('Failed to connect'); } } catch (err) { if (gen === _connectGeneration) setConnectionError('Failed to connect'); }
finally { if (gen === _connectGeneration) setIsConnecting(false); } finally { if (gen === _connectGeneration) setIsConnecting(false); }
}, [updateParticipants, handleDataReceived]); }, [updateParticipants, handleDataReceived, isMuted, isDeafened]);
const connectDm = useCallback(async (dmChannelId: string) => { const connectDm = useCallback(async (dmChannelId: string) => {
const gen = ++_connectGeneration; const gen = ++_connectGeneration;
@@ -267,7 +267,7 @@ export function useLiveKit() {
} }
} catch (err) { if (gen === _connectGeneration) setConnectionError('Failed to connect'); } } catch (err) { if (gen === _connectGeneration) setConnectionError('Failed to connect'); }
finally { if (gen === _connectGeneration) setIsConnecting(false); } finally { if (gen === _connectGeneration) setIsConnecting(false); }
}, [updateParticipants, handleDataReceived]); }, [updateParticipants, handleDataReceived, isMuted, isDeafened]);
const disconnect = useCallback(async () => { const disconnect = useCallback(async () => {
_connectGeneration++; _connectGeneration++;
+1
View File
@@ -56,6 +56,7 @@ function handleEvent(event: ServerEvent): void {
{ {
const { currentVoiceChannelId, isMuted: curMuted, isDeafened: curDeafened } = useVoiceStore.getState(); const { currentVoiceChannelId, isMuted: curMuted, isDeafened: curDeafened } = useVoiceStore.getState();
if (currentVoiceChannelId) { if (currentVoiceChannelId) {
console.log('[WebSocket] Re-syncing voice status on reconnect:', { currentVoiceChannelId, curMuted, curDeafened });
wsSend({ type: 'voice_join', channelId: currentVoiceChannelId }); wsSend({ type: 'voice_join', channelId: currentVoiceChannelId });
wsSend({ type: 'voice_status', isMuted: curMuted, isDeafened: curDeafened }); wsSend({ type: 'voice_status', isMuted: curMuted, isDeafened: curDeafened });
} }
+21 -6
View File
@@ -1,4 +1,5 @@
import { create } from 'zustand'; import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import type { ParticipantInfo } from '../hooks/useLiveKit'; import type { ParticipantInfo } from '../hooks/useLiveKit';
interface VoiceState { interface VoiceState {
@@ -55,7 +56,9 @@ interface VoiceState {
reset: () => void; reset: () => void;
} }
export const useVoiceStore = create<VoiceState>((set, get) => ({ export const useVoiceStore = create<VoiceState>()(
persist(
(set, get) => ({
voiceUsers: new Map(), voiceUsers: new Map(),
currentVoiceChannelId: null, currentVoiceChannelId: null,
isMuted: false, isMuted: false,
@@ -165,15 +168,11 @@ export const useVoiceStore = create<VoiceState>((set, get) => ({
// Leave voice without wiping the voiceUsers map (so sidebar still shows others) // Leave voice without wiping the voiceUsers map (so sidebar still shows others)
leaveVoice: () => set({ leaveVoice: () => set({
currentVoiceChannelId: null, currentVoiceChannelId: null,
isMuted: false,
isDeafened: false,
isCameraOn: false, isCameraOn: false,
isScreenSharing: false, isScreenSharing: false,
participants: [], participants: [],
connectionError: null, connectionError: null,
isLiveKitConnected: false, isLiveKitConnected: false,
inputVolume: 100,
outputVolume: 100,
focusedParticipantId: null, focusedParticipantId: null,
activeDmCall: null, activeDmCall: null,
outgoingCall: null, outgoingCall: null,
@@ -200,4 +199,20 @@ export const useVoiceStore = create<VoiceState>((set, get) => ({
deafenedUserIds: new Set(), deafenedUserIds: new Set(),
voiceUserStates: new Map(), voiceUserStates: new Map(),
}), }),
})); }),
{
name: 'opencord-voice-settings',
storage: createJSONStorage(() => localStorage),
// Only persist these keys. Maps and Sets are complex to serialize.
partialize: (state) => ({
currentVoiceChannelId: state.currentVoiceChannelId,
isMuted: state.isMuted,
isDeafened: state.isDeafened,
inputVolume: state.inputVolume,
outputVolume: state.outputVolume,
videoQuality: state.videoQuality,
noiseSuppression: state.noiseSuppression,
}),
}
)
);