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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user