46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { create } from "zustand";
|
|
import { immer } from "zustand/middleware/immer";
|
|
import type { ChannelId, UserId } from "~/lib/api/types";
|
|
|
|
interface UserVoiceState {
|
|
deaf: boolean;
|
|
muted: boolean;
|
|
}
|
|
|
|
interface ChannelVoiceState {
|
|
users: Record<UserId, UserVoiceState>;
|
|
}
|
|
|
|
interface ChannelsVoiceState {
|
|
channels: Record<ChannelId, ChannelVoiceState>;
|
|
addUser: (channelId: ChannelId, userId: UserId, userVoiceState: UserVoiceState) => void;
|
|
removeUser: (channelId: ChannelId, userId: UserId) => void;
|
|
removeChannel: (channelId: ChannelId) => void;
|
|
}
|
|
|
|
export const useChannelsVoiceStateStore = create<ChannelsVoiceState>()(
|
|
immer((set, get) => ({
|
|
channels: {},
|
|
addUser: (channelId, userId, userVoiceState) =>
|
|
set((state) => {
|
|
if (!state.channels[channelId]) {
|
|
state.channels[channelId] = {
|
|
users: {},
|
|
};
|
|
}
|
|
|
|
state.channels[channelId].users[userId] = userVoiceState;
|
|
}),
|
|
removeUser: (channelId, userId) =>
|
|
set((state) => {
|
|
if (state.channels[channelId]) {
|
|
delete state.channels[channelId].users[userId];
|
|
}
|
|
}),
|
|
removeChannel: (channelId) =>
|
|
set((state) => {
|
|
delete state.channels[channelId];
|
|
}),
|
|
})),
|
|
);
|