fix: optimistic voice state so user appears in sidebar immediately

Previously, the voice channel sidebar only showed users after a server
round-trip (voice_state_update broadcast). After a deploy/reconnect,
this left the user invisible in the sidebar despite being connected.
Now joinVoiceChannel, leaveVoice, and the WS ready handler all
optimistically update voiceUsers for the local user immediately.
This commit is contained in:
Jannis Braun
2026-03-04 21:48:39 +01:00
parent a5ea7633fc
commit a5073f89fd
3 changed files with 46 additions and 18 deletions
+8 -1
View File
@@ -1,4 +1,5 @@
import { useVoiceStore } from '../stores/voiceStore';
import { useAuthStore } from '../stores/authStore';
import { getChannelOrigin } from '../stores/serverStore';
import { wsSend } from '../hooks/useWebSocket';
@@ -9,9 +10,11 @@ import { wsSend } from '../hooks/useWebSocket';
* broadcasts a leave event and the client cleans up stale voice state.
*/
export function joinVoiceChannel(channelId: string): void {
const { currentVoiceChannelId, setCurrentVoiceChannel } = useVoiceStore.getState();
const { currentVoiceChannelId, setCurrentVoiceChannel, addVoiceUser, removeVoiceUser } = useVoiceStore.getState();
if (currentVoiceChannelId === channelId) return;
const myId = useAuthStore.getState().user?.id;
// Leave old instance if switching cross-origin
if (currentVoiceChannelId) {
const oldOrigin = getChannelOrigin(currentVoiceChannelId);
@@ -19,8 +22,12 @@ export function joinVoiceChannel(channelId: string): void {
if (oldOrigin !== newOrigin) {
wsSend({ type: 'voice_leave' }, oldOrigin);
}
// Optimistic: immediately remove self from old channel
if (myId) removeVoiceUser(currentVoiceChannelId, myId);
}
setCurrentVoiceChannel(channelId);
wsSend({ type: 'voice_join', channelId }, getChannelOrigin(channelId));
// Optimistic: immediately show self in new channel
if (myId) addVoiceUser(channelId, myId);
}