11import  {  useCallback ,  useEffect ,  useRef ,  useState  }  from  'react' ; 
2- import  sound  from  '@/assets/sounds/sound_6.mp3' 
2+ import  sound  from  '@/assets/sounds/sound_6.mp3' ; 
33
44type  usePomodoroTimerProps  =  { 
55  workTime : number ; 
@@ -13,12 +13,18 @@ const usePomodoroTimer = ({ workTime, breakTime, onModeChange }: usePomodoroTime
1313  const  [ isActive ,  setIsActive ]  =  useState ( false ) ; 
1414  const  [ workedSeconds ,  setWorkedSeconds ]  =  useState ( 0 ) ; 
1515
16-   const  countSecondsRef  =  useRef ( 0 ) ; 
17-   const  intervalRef  =  useRef < number  |  null > ( null ) ; 
18- 
1916  const  prevWorkTimeRef  =  useRef ( workTime ) ; 
2017  const  prevBreakTimeRef  =  useRef ( breakTime ) ; 
2118
19+   // refs aux 
20+   const  isBreakRef  =  useRef ( isBreak ) ; 
21+   const  switchingRef  =  useRef ( false ) ; 
22+ 
23+   useEffect ( ( )  =>  { 
24+     isBreakRef . current  =  isBreak ; 
25+   } ,  [ isBreak ] ) ; 
26+ 
27+   // if settings change and timer off restart timer 
2228  useEffect ( ( )  =>  { 
2329    if  ( ! isActive )  { 
2430      if  ( prevWorkTimeRef . current  !==  workTime  ||  prevBreakTimeRef . current  !==  breakTime )  { 
@@ -32,43 +38,45 @@ const usePomodoroTimer = ({ workTime, breakTime, onModeChange }: usePomodoroTime
3238  const  switchMode  =  useCallback ( ( )  =>  { 
3339    const  alarm  =  new  Audio ( sound ) ; 
3440    alarm . volume  =  0.07 ; 
35-     alarm . play ( ) . catch ( ( )  =>  {  console . log ( 'No se pudo reproducir el sonido' ) ;  } ) ; 
41+     alarm . play ( ) . catch ( ( )  =>  { 
42+       console . log ( 'No se pudo reproducir el sonido' ) ; 
43+     } ) ; 
3644
3745    setIsBreak ( prev  =>  { 
3846      const  newIsBreak  =  ! prev ; 
3947      onModeChange ?.( newIsBreak ) ; 
4048      setTimeLeft ( newIsBreak  ? breakTime  *  60  : workTime  *  60 ) ; 
49+ 
50+       switchingRef . current  =  false ; 
51+ 
4152      return  newIsBreak ; 
4253    } ) ; 
4354  } ,  [ workTime ,  breakTime ,  onModeChange ] ) ; 
4455
56+   // interval control 
4557  useEffect ( ( )  =>  { 
46-     if  ( ! isActive )  { 
47-       if  ( intervalRef . current )  { 
48-         clearInterval ( intervalRef . current ) ; 
49-         intervalRef . current  =  null ; 
50-       } 
51-       return ; 
52-     } 
58+     if  ( ! isActive )  return ; 
5359
54-     intervalRef . current   =   window . setInterval ( ( )  =>  { 
60+     const   interval   =   setInterval ( ( )  =>  { 
5561      setTimeLeft ( prev  =>  { 
5662        if  ( prev  <=  0 )  { 
57-           switchMode ( ) ; 
58-           return  0 ; 
63+           if  ( ! switchingRef . current )  { 
64+             switchingRef . current  =  true ; 
65+             switchMode ( ) ; 
66+             return  isBreakRef . current  ? workTime  *  60  : breakTime  *  60 ; 
67+           } 
68+           return  prev ; 
5969        } 
6070        return  prev  -  1 ; 
6171      } ) ; 
6272
63-       // Incrementar  workedSeconds solo si no es break  
64-       setWorkedSeconds ( prev  =>  ( ! isBreak  ? prev  +  1  : prev ) ) ; 
73+       // workedSeconds only in work  
74+       setWorkedSeconds ( prev  =>  ( ! isBreakRef . current  ? prev  +  1  : prev ) ) ; 
6575    } ,  1000 ) ; 
6676
67-     return  ( )  =>  { 
68-       if  ( intervalRef . current )  clearInterval ( intervalRef . current ) ; 
69-     } ; 
70-   } ,  [ isActive ,  switchMode ,  isBreak ] ) ; 
7177
78+     return  ( )  =>  clearInterval ( interval ) ; 
79+   } ,  [ isActive ,  switchMode ] ) ; 
7280
7381  const  start  =  useCallback ( ( )  =>  { 
7482    if  ( ! isActive )  { 
@@ -86,17 +94,10 @@ const usePomodoroTimer = ({ workTime, breakTime, onModeChange }: usePomodoroTime
8694    stop ( ) ; 
8795    setIsBreak ( false ) ; 
8896    setTimeLeft ( workTime  *  60 ) ; 
89-     countSecondsRef . current  =  0 ; 
9097    setWorkedSeconds ( 0 ) ; 
9198    document . documentElement . style . backgroundColor  =  '#242424' ; 
9299  } ,  [ stop ,  workTime ] ) ; 
93100
94-   useEffect ( ( )  =>  { 
95-     return  ( )  =>  { 
96-       if  ( intervalRef . current )  clearInterval ( intervalRef . current ) ; 
97-     } ; 
98-   } ,  [ ] ) ; 
99- 
100101  return  { 
101102    timeLeft, 
102103    isBreak, 
@@ -108,4 +109,4 @@ const usePomodoroTimer = ({ workTime, breakTime, onModeChange }: usePomodoroTime
108109  } ; 
109110} ; 
110111
111- export  default  usePomodoroTimer ; 
112+ export  default  usePomodoroTimer ; 
0 commit comments