import { useTranslation } from "react-i18next"; import StepIndicator from "../indicators/StepIndicator"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "../ui/dialog"; import { useReducer, useEffect } from "react"; import Step1NameAndType, { Step1FormData, } from "@/components/trigger/wizard/Step1NameAndType"; import Step2ConfigureData, { Step2FormData, } from "@/components/trigger/wizard/Step2ConfigureData"; import Step3ThresholdAndActions, { Step3FormData, } from "@/components/trigger/wizard/Step3ThresholdAndActions"; import { cn } from "@/lib/utils"; import { isDesktop } from "react-device-detect"; import { Trigger, TriggerAction, TriggerType } from "@/types/trigger"; const TRIGGER_STEPS = [ "wizard.steps.nameAndType", "wizard.steps.configureData", "wizard.steps.thresholdAndActions", ]; type TriggerWizardDialogProps = { open: boolean; onClose: () => void; selectedCamera: string; trigger?: Trigger | null; onCreate: ( enabled: boolean, name: string, type: TriggerType, data: string, threshold: number, actions: TriggerAction[], friendly_name: string, ) => void; onEdit: (trigger: Trigger) => void; isLoading?: boolean; }; type WizardState = { currentStep: number; step1Data?: Step1FormData; step2Data?: Step2FormData; step3Data?: Step3FormData; }; type WizardAction = | { type: "NEXT_STEP"; payload?: Partial } | { type: "PREVIOUS_STEP" } | { type: "SET_STEP_1"; payload: Step1FormData } | { type: "SET_STEP_2"; payload: Step2FormData } | { type: "SET_STEP_3"; payload: Step3FormData } | { type: "RESET" }; const initialState: WizardState = { currentStep: 0, }; function wizardReducer(state: WizardState, action: WizardAction): WizardState { switch (action.type) { case "SET_STEP_1": return { ...state, step1Data: action.payload, step2Data: undefined, step3Data: undefined, currentStep: 1, }; case "SET_STEP_2": return { ...state, step2Data: action.payload, currentStep: 2, }; case "SET_STEP_3": return { ...state, step3Data: action.payload, currentStep: 3, }; case "NEXT_STEP": return { ...state, ...action.payload, currentStep: state.currentStep + 1, }; case "PREVIOUS_STEP": return { ...state, currentStep: Math.max(0, state.currentStep - 1), }; case "RESET": return initialState; default: return state; } } export default function TriggerWizardDialog({ open, onClose, selectedCamera, trigger, onCreate, onEdit, isLoading, }: TriggerWizardDialogProps) { const { t } = useTranslation(["views/settings"]); const [wizardState, dispatch] = useReducer(wizardReducer, initialState); useEffect(() => { if (!open) { dispatch({ type: "RESET" }); } }, [open]); // Reset wizard state when opening for a different trigger or when creating new useEffect(() => { if (open) { dispatch({ type: "RESET" }); } }, [open, trigger]); const handleStep1Next = (data: Step1FormData) => { dispatch({ type: "SET_STEP_1", payload: data }); }; const handleStep2Next = (data: Step2FormData) => { dispatch({ type: "SET_STEP_2", payload: data }); }; const handleStep3Next = (data: Step3FormData) => { // Combine all step data and call the appropriate callback const combinedData = { ...wizardState.step1Data!, ...wizardState.step2Data!, ...data, }; if (trigger) { onEdit(combinedData); } else { onCreate( combinedData.enabled, combinedData.name, combinedData.type, combinedData.data, combinedData.threshold, combinedData.actions, combinedData.friendly_name || "", ); } // Remove handleClose() - let the parent component handle closing after save completes }; const handleBack = () => { dispatch({ type: "PREVIOUS_STEP" }); }; const handleClose = () => { dispatch({ type: "RESET" }); onClose(); }; return ( { if (!open && !isLoading) { handleClose(); } }} > { e.preventDefault(); }} > {t("triggers.wizard.title")} {wizardState.currentStep === 0 && ( {t("triggers.wizard.step1.description")} )} {wizardState.currentStep === 1 && ( {t("triggers.wizard.step2.description")} )} {wizardState.currentStep === 2 && ( {t("triggers.wizard.step3.description")} )}
{wizardState.currentStep === 0 && ( )} {wizardState.currentStep === 1 && wizardState.step1Data && ( )} {wizardState.currentStep === 2 && wizardState.step1Data && wizardState.step2Data && ( )}
); }