49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import { create } from "zustand";
|
|
import { immer } from "zustand/middleware/immer";
|
|
import type { ChannelId, ServerChannel, ServerId } from "~/lib/api/types";
|
|
|
|
type ServerChannelsStore = {
|
|
channels: Record<ServerId, Record<ChannelId, ServerChannel>>;
|
|
addServer: (serverId: ServerId) => void;
|
|
addChannel: (channel: ServerChannel) => void;
|
|
addChannels: (channels: ServerChannel[]) => void;
|
|
removeChannel: (serverId: ServerId, channelId: ChannelId) => void;
|
|
removeServer: (serverId: ServerId) => void;
|
|
};
|
|
|
|
export const useServerChannelsStore = create<ServerChannelsStore>()(
|
|
immer((set, get) => ({
|
|
channels: {},
|
|
addServer: (serverId) =>
|
|
set((state) => {
|
|
state.channels[serverId] = {};
|
|
}),
|
|
addChannel: (channel) =>
|
|
set((state) => {
|
|
if (state.channels[channel.serverId] === undefined) {
|
|
state.channels[channel.serverId] = {};
|
|
}
|
|
|
|
state.channels[channel.serverId][channel.id] = channel;
|
|
}),
|
|
addChannels: (channels) =>
|
|
set((state) => {
|
|
for (const channel of channels) {
|
|
if (state.channels[channel.serverId] === undefined) {
|
|
state.channels[channel.serverId] = {};
|
|
}
|
|
|
|
state.channels[channel.serverId][channel.id] = channel;
|
|
}
|
|
}),
|
|
removeChannel: (serverId, channelId) =>
|
|
set((state) => {
|
|
delete state.channels[serverId][channelId];
|
|
}),
|
|
removeServer: (serverId) =>
|
|
set((state) => {
|
|
delete state.channels[serverId];
|
|
}),
|
|
})),
|
|
);
|