.
This commit is contained in:
41
app/stores/server-channels-store.ts
Normal file
41
app/stores/server-channels-store.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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] }),
|
||||
})
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user