ERP-node/frontend/stores/flowStepStore.ts

113 lines
3.0 KiB
TypeScript
Raw Normal View History

"use client";
import { create } from "zustand";
import { devtools } from "zustand/middleware";
/**
*
*
* ,
* / .
*/
interface FlowStepState {
/**
*
* key: flowComponentId (: "component-123")
* value: stepId ( ID) null ( )
*/
selectedSteps: Record<string, number | null>;
/**
*
* @param flowComponentId ID
* @param stepId ID (null이면 )
*/
setSelectedStep: (flowComponentId: string, stepId: number | null) => void;
/**
*
* @param flowComponentId ID
* @returns ID null
*/
getCurrentStep: (flowComponentId: string) => number | null;
/**
* ( )
*/
reset: () => void;
/**
* ( )
* @param flowComponentId ID
*/
resetFlow: (flowComponentId: string) => void;
}
export const useFlowStepStore = create<FlowStepState>()(
devtools(
(set, get) => ({
selectedSteps: {},
setSelectedStep: (flowComponentId, stepId) => {
set((state) => ({
selectedSteps: {
...state.selectedSteps,
[flowComponentId]: stepId,
},
}));
},
getCurrentStep: (flowComponentId) => {
const stepId = get().selectedSteps[flowComponentId] || null;
return stepId;
},
reset: () => {
set({ selectedSteps: {} });
},
resetFlow: (flowComponentId) => {
set((state) => {
const { [flowComponentId]: _, ...rest } = state.selectedSteps;
return { selectedSteps: rest };
});
},
}),
{ name: "FlowStepStore" }
)
);
/**
* Hook
*
* @example
* const currentStep = useCurrentFlowStep("component-123");
* if (currentStep === null) {
* // 단계가 선택되지 않음
* }
*/
export const useCurrentFlowStep = (flowComponentId: string | null | undefined) => {
return useFlowStepStore((state) => {
if (!flowComponentId) return null;
return state.getCurrentStep(flowComponentId);
});
};
/**
* Hook
*
* @example
* const steps = useMultipleFlowSteps(["component-123", "component-456"]);
* // { "component-123": 1, "component-456": null }
*/
export const useMultipleFlowSteps = (flowComponentIds: string[]) => {
return useFlowStepStore((state) => {
const result: Record<string, number | null> = {};
flowComponentIds.forEach((id) => {
result[id] = state.getCurrentStep(id);
});
return result;
});
};