// ~/store/active-voice-channel.ts import { create } from 'zustand'; import type { Uuid } from '~/lib/api/types'; interface ActiveVoiceChannelState { serverId: Uuid | null; channelId: Uuid | null; // User's explicit intent to be in voice, helps differentiate between just selecting a channel and actually joining isVoiceActive: boolean; setActiveVoiceChannel: (serverId: Uuid | null, channelId: Uuid | null) => void; toggleVoiceActivation: (activate: boolean) => void; // To explicitly join/leave voice for the selected channel } export const useActiveVoiceChannelStore = create((set, get) => ({ serverId: null, channelId: null, isVoiceActive: false, setActiveVoiceChannel: (serverId, channelId) => { const current = get(); // If changing channels, implicitly deactivate voice from the old one. // The manager will then pick up the new channel and wait for isVoiceActive. if (current.channelId !== channelId && current.isVoiceActive) { set({ serverId, channelId, isVoiceActive: false }); // User needs to explicitly activate new channel } else { set({ serverId, channelId }); } console.log(`ActiveVoiceChannel: Selected Server: ${serverId}, Channel: ${channelId}`); }, toggleVoiceActivation: (activate) => { const { serverId, channelId } = get(); if (activate && (!serverId || !channelId)) { console.warn("ActiveVoiceChannel: Cannot activate voice without a selected server/channel."); set({ isVoiceActive: false }); return; } set({ isVoiceActive: activate }); console.log(`ActiveVoiceChannel: Voice activation set to ${activate} for ${serverId}/${channelId}`); }, }));