| 1 | import { create } from 'zustand';
|
| 2 |
|
| 3 | export interface ActiveMoodComponent {
|
| 4 | id: string; // matches MoodComponentDefinition.id
|
| 5 | intensity: number; // 1–3
|
| 6 | }
|
| 7 |
|
| 8 | interface MoodEntryState {
|
| 9 | // The mood selected in the main screen (e.g. 'good')
|
| 10 | selectedMood: string | null;
|
| 11 |
|
| 12 | // The active components the user has added + their intensities
|
| 13 | components: ActiveMoodComponent[];
|
| 14 |
|
| 15 | // Actions
|
| 16 | setSelectedMood: (mood: string) => void;
|
| 17 | setComponents: (components: ActiveMoodComponent[]) => void;
|
| 18 | addComponent: (id: string) => void;
|
| 19 | removeComponent: (id: string) => void;
|
| 20 | setComponentIntensity: (id: string, intensity: number) => void;
|
| 21 | reset: () => void;
|
| 22 | }
|
| 23 |
|
| 24 | export const useMoodEntryStore = create<MoodEntryState>((set) => ({
|
| 25 | selectedMood: null,
|
| 26 | components: [],
|
| 27 |
|
| 28 | setSelectedMood: (mood) => set({ selectedMood: mood }),
|
| 29 |
|
| 30 | // Used by the modal to write back the full list at once
|
| 31 | setComponents: (components) => set({ components }),
|
| 32 |
|
| 33 | addComponent: (id) =>
|
| 34 | set((state) => {
|
| 35 | if (state.components.find((c) => c.id === id)) return state;
|
| 36 | return { components: [...state.components, { id, intensity: 1 }] };
|
| 37 | }),
|
| 38 |
|
| 39 | removeComponent: (id) =>
|
| 40 | set((state) => ({
|
| 41 | components: state.components.filter((c) => c.id !== id),
|
| 42 | })),
|
| 43 |
|
| 44 | setComponentIntensity: (id, intensity) =>
|
| 45 | set((state) => ({
|
| 46 | components: state.components.map((c) =>
|
| 47 | c.id === id ? { ...c, intensity } : c
|
| 48 | ),
|
| 49 | })),
|
| 50 |
|
| 51 | reset: () => set({ selectedMood: null, components: [] }),
|
| 52 | }));
|