import {create} from "zustand"; // the type of a reduced program export type ReducedProgram = { phases: Record[] }; /** * the type definition of the programStore */ export type ProgramState = { // Basic store functionality: currentProgram: ReducedProgram; setProgramState: (state: ReducedProgram) => void; getProgramState: () => ReducedProgram; // Utility functions: // to avoid having to manually go through the entire state for every instance where data is required getPhaseIds: () => string[]; getNormsInPhase: (currentPhaseId: string) => Record[]; getGoalsInPhase: (currentPhaseId: string) => Record[]; getTriggersInPhase: (currentPhaseId: string) => Record[]; // if more specific utility functions are needed they can be added here: } /** * the ProgramStore can be used to access all information of the most recently sent program, * it contains basic functions to set and get the current program. * And it contains some utility functions that allow you to easily gain access * to the norms, triggers and goals of a specific phase. */ const useProgramStore = create((set, get) => ({ currentProgram: { phases: [] as Record[]}, /** * sets the current program by cloning the provided program using a structuredClone */ setProgramState: (program: ReducedProgram) => set({currentProgram: structuredClone(program)}), /** * gets the current program */ getProgramState: () => get().currentProgram, // utility functions: /** * gets the ids of all phases in the program */ getPhaseIds: () => get().currentProgram.phases.map(entry => entry["id"] as string), /** * gets the norms for the provided phase */ getNormsInPhase: (currentPhaseId) => { const program = get().currentProgram; const phase = program.phases.find(val => val["id"] === currentPhaseId); if (phase) { return phase["norms"] as Record[]; } throw new Error(`phase with id:"${currentPhaseId}" not found`) }, /** * gets the goals for the provided phase */ getGoalsInPhase: (currentPhaseId) => { const program = get().currentProgram; const phase = program.phases.find(val => val["id"] === currentPhaseId); if (phase) { return phase["goals"] as Record[]; } throw new Error(`phase with id:"${currentPhaseId}" not found`) }, /** * gets the triggers for the provided phase */ getTriggersInPhase: (currentPhaseId) => { const program = get().currentProgram; const phase = program.phases.find(val => val["id"] === currentPhaseId); if (phase) { return phase["triggers"] as Record[]; } throw new Error(`phase with id:"${currentPhaseId}" not found`) } })); export default useProgramStore;