import { useSelector } from "react-redux"; import { RootState, useAppDispatch } from "../redux/store"; import { fetchHotspotDataForKey, setCurrentSlot, setCurrentStep, setSlotImperfectionData, } from "../redux/AOPSlice"; import { useEffect } from "react"; import { setAnnotationForTargetSlot } from "../redux/canvasSlice"; export function useAOPSlot() { const steps = useSelector((state: RootState) => state.AOP.steps); const activeSlotIndex = useSelector( (state: RootState) => state.AOP.activeSlotIndex ); const activeStepIndex = useSelector( (state: RootState) => state.AOP.activeStepIndex ); const { lockNavigation } = useSelector( (appState: RootState) => appState.Util ); const slotList = steps.length > 0 ? steps[activeStepIndex].slots : []; const activeSlot = slotList[activeSlotIndex]; const { hotspotData, hotspotPart, hotspotCategory, hotspotCategoryGroup, hotspotCondition, } = useSelector((state: RootState) => state.Canvas); const dispatch = useAppDispatch(); const incrementSlot = () => { if (!lockNavigation && activeSlotIndex + 1 <= slotList.length - 1) { dispatch( setSlotImperfectionData({ ...hotspotData, hotspotCategoryGroup: hotspotCategoryGroup ? hotspotCategoryGroup : undefined, hotspotCategory: hotspotCategory ? hotspotCategory : undefined, hotspotPart: hotspotPart ? hotspotPart : undefined, hotspotCondition: hotspotCondition ? hotspotCondition : undefined, }) ); dispatch(setCurrentSlot((activeSlotIndex as number) + 1)); } }; const setSOPSlotFromIndex = (index: number) => { if (!lockNavigation) { dispatch( setSlotImperfectionData({ ...hotspotData, hotspotCategoryGroup: hotspotCategoryGroup ? hotspotCategoryGroup : undefined, hotspotCategory: hotspotCategory ? hotspotCategory : undefined, hotspotPart: hotspotPart ? hotspotPart : undefined, hotspotCondition: hotspotCondition ? hotspotCondition : undefined, }) ); dispatch(setCurrentSlot(index)); } }; const decrementSlot = () => { if (!lockNavigation && activeSlotIndex - 1 >= 0) { dispatch( setSlotImperfectionData({ ...hotspotData, hotspotCategoryGroup: hotspotCategoryGroup ? hotspotCategoryGroup : undefined, hotspotCategory: hotspotCategory ? hotspotCategory : undefined, hotspotPart: hotspotPart ? hotspotPart : undefined, hotspotCondition: hotspotCondition ? hotspotCondition : undefined, }) ); dispatch(setCurrentSlot((activeSlotIndex as number) - 1)); } }; useEffect(() => { if (slotList.length > 0) { const metaData = activeSlot.imperfectionMetaData ? activeSlot.imperfectionMetaData : {}; if ( activeSlot.slotType !== "DEFAULT" && !activeSlot.locked && activeSlot.refKey ) { dispatch(fetchHotspotDataForKey(activeSlot.refKey as string)); dispatch( setAnnotationForTargetSlot({ stepIndex: activeStepIndex, slotIndex: activeSlotIndex, slotId: activeSlot.slotId, stepId: steps[activeStepIndex].stepId, slotTitle: activeSlot.title, stepTitle: steps[activeStepIndex].title, metaData: { ...metaData, }, }) ); } } }, [slotList]); return { activeSlotIndex, activeSlot, slotList, incrementSlot, decrementSlot, setSOPSlotFromIndex, }; } export function useAOPStep() { const steps = useSelector((state: RootState) => state.AOP.steps); const activeStepIndex = useSelector( (state: RootState) => state.AOP.activeStepIndex ); const { lockNavigation } = useSelector( (appState: RootState) => appState.Util ); const dispatch = useAppDispatch(); const incrementStep = () => { if (!lockNavigation && activeStepIndex + 1 <= steps.length - 1) { dispatch(setCurrentStep(activeStepIndex + 1)); } }; const decrementStep = () => { if (!lockNavigation && activeStepIndex - 1 >= 0) { dispatch(setCurrentStep(activeStepIndex - 1)); } }; return { steps, activeStepIndex, incrementStep, decrementStep, activeStep: steps[activeStepIndex], }; }