Skip to content

Commit

Permalink
feat(stores): create sounds-state-store
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Aug 15, 2023
1 parent ce9df67 commit a81febc
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/stores/sounds-state-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { create } from 'zustand'
import { persist } from 'zustand/middleware'

export interface SoundState {
id: string
active: boolean
volume: number
}

interface Props {
sounds: SoundState[]
getSound: (id: string) => undefined | SoundState
setSound: (newState: SoundState) => void
bulkUpdate: (soundsStates: SoundState[]) => void
}

export const useSoundsStateStore = create<Props>()(
persist(
(set, get) => ({
sounds: [] as SoundState[],
getSound(id) {
const sound = get().sounds.filter(sound => sound.id === id)

if (sound.length > 0) return sound[0]
else return undefined
},
setSound(newState) {
const sounds = get().sounds.filter(sound => sound.id !== newState.id)
set({ sounds: [...sounds, newState] })
},
bulkUpdate(newStates) {
set({ sounds: newStates })
}
}),
{ name: 'sounds-store' }
)
)

0 comments on commit a81febc

Please sign in to comment.