turash/bugulma/frontend/hooks/useWizard.ts
Damir Mukimov 6347f42e20
Consolidate repositories: Remove nested frontend .git and merge into main repository
- Remove nested git repository from bugulma/frontend/.git
- Add all frontend files to main repository tracking
- Convert from separate frontend/backend repos to unified monorepo
- Preserve all frontend code and development history as tracked files
- Eliminate nested repository complexity for simpler development workflow

This creates a proper monorepo structure with frontend and backend
coexisting in the same repository for easier development and deployment.
2025-11-25 06:02:57 +01:00

67 lines
1.6 KiB
TypeScript

import { useCallback, useMemo, useState } from 'react';
interface UseWizardReturn {
currentStep: number;
nextStep: () => void;
prevStep: () => void;
goToStep: (step: number) => void;
isFirstStep: boolean;
isLastStep: boolean;
totalSteps: number;
}
/**
* Manages the state of a multi-step wizard component.
* It provides a simple API to navigate between steps.
* @param totalSteps - The total number of steps in the wizard.
* @returns An object containing the current wizard state and navigation methods.
* @example
* const { currentStep, nextStep, prevStep, isLastStep } = useWizard(3);
*/
export const useWizard = (totalSteps: number): UseWizardReturn => {
const [currentStep, setCurrentStep] = useState(1);
const nextStep = useCallback(() => {
setCurrentStep((step) => {
if (step < totalSteps) {
return step + 1;
}
return step;
});
}, [totalSteps]);
const prevStep = useCallback(() => {
setCurrentStep((step) => {
if (step > 1) {
return step - 1;
}
return step;
});
}, []);
const goToStep = useCallback(
(step: number) => {
if (step > 0 && step <= totalSteps) {
setCurrentStep(step);
}
},
[totalSteps]
);
const isFirstStep = currentStep === 1;
const isLastStep = currentStep === totalSteps;
return useMemo(
() => ({
currentStep,
nextStep,
prevStep,
goToStep,
isFirstStep,
isLastStep,
totalSteps,
}),
[currentStep, nextStep, prevStep, goToStep, isFirstStep, isLastStep, totalSteps]
);
};