34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { create } from "zustand";
|
|
import { immer } from "zustand/middleware/immer";
|
|
import type { ChannelId, RecipientChannel } from "~/lib/api/types";
|
|
|
|
type PrivateChannelsStore = {
|
|
channels: Record<ChannelId, RecipientChannel>;
|
|
addChannels: (channels: RecipientChannel[]) => void;
|
|
addChannel: (channel: RecipientChannel) => void;
|
|
removeChannel: (channelId: ChannelId) => void;
|
|
};
|
|
|
|
export const usePrivateChannelsStore = create<PrivateChannelsStore>()(
|
|
immer((set) => ({
|
|
channels: {},
|
|
addChannels: (channels: RecipientChannel[]) =>
|
|
set((state) => {
|
|
for (const channel of channels) {
|
|
state.channels[channel.id] = channel;
|
|
console.log("add channel", channel);
|
|
}
|
|
}),
|
|
addChannel: (channel: RecipientChannel) =>
|
|
set((state) => {
|
|
state.channels[channel.id] = {
|
|
...channel,
|
|
};
|
|
}),
|
|
removeChannel: (channelId: ChannelId) =>
|
|
set((state) => {
|
|
delete state.channels[channelId];
|
|
}),
|
|
})),
|
|
);
|