25 lines
974 B
TypeScript
25 lines
974 B
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
|
|
}
|
|
}),
|
|
addChannel: (channel: RecipientChannel) => set((state) => { state.channels[channel.id] = channel }),
|
|
removeChannel: (channelId: ChannelId) => set((state) => { delete state.channels[channelId] }),
|
|
})
|
|
)
|
|
) |