@@ -136,22 +136,32 @@ export default function Waffle() {
136136 return result ;
137137 } , [ cycles ] )
138138
139+ const stepCountTracker = useMemo ( ( ) => {
140+ if ( cycleIndex < 0 ) {
141+ return 0 ;
142+ }
143+ if ( cycleIndex >= cycleSteps . length ) {
144+ return cycleSteps . length
145+ }
146+ return cycleIndex + 1
147+ } , [ cycleIndex , cycleSteps ] )
148+
139149 const next = useCallback ( ( ) => {
150+ setPuzzleState ( puzzleState . map ( ( item ) => { item . swap = false ; return item } ) ) ;
140151 const nextCycleIndex = cycleIndex + 1 ;
141- if ( nextCycleIndex < cycleSteps . length ) {
142- setPuzzleState ( puzzleState . map ( ( item ) => { item . swap = false ; return item } ) ) ;
143- setCycleIndex ( nextCycleIndex ) ;
152+ if ( nextCycleIndex < cycleSteps . length && cycleSteps [ nextCycleIndex ] ) {
144153 setPuzzleState ( swap ( puzzleState , cycleSteps [ nextCycleIndex ] ) )
145154 }
155+ setCycleIndex ( nextCycleIndex ) ;
146156 } , [ cycleIndex , puzzleState , cycleSteps ] )
147157
148158 const prev = useCallback ( ( ) => {
159+ setPuzzleState ( puzzleState . map ( ( item ) => { item . swap = false ; return item } ) ) ;
149160 const nextCycleIndex = cycleIndex - 1 ;
150- if ( nextCycleIndex >= 0 ) {
151- setPuzzleState ( puzzleState . map ( ( item ) => { item . swap = false ; return item } ) ) ;
152- setPuzzleState ( swap ( puzzleState , cycleSteps [ nextCycleIndex ] ) )
153- setCycleIndex ( nextCycleIndex ) ;
161+ if ( cycleIndex >= - 1 && cycleSteps [ cycleIndex ] ) {
162+ setPuzzleState ( swap ( puzzleState , cycleSteps [ cycleIndex ] ) )
154163 }
164+ setCycleIndex ( nextCycleIndex ) ;
155165 } , [ cycleIndex , puzzleState , cycleSteps ] )
156166
157167 return (
@@ -206,11 +216,11 @@ export default function Waffle() {
206216 </ div >
207217
208218 < div >
209- Step: { cycleIndex < 0 ? 0 : cycleIndex + 1 } / { cycleSteps . length }
219+ Step: { stepCountTracker } / { cycleSteps . length }
210220 </ div >
211221 < div className = { styles . buttonBox } >
212222 < button
213- disabled = { cycleIndex <= 0 }
223+ disabled = { cycleIndex < - 1 }
214224 className = { styles . nextprevbutton }
215225 onClick = { ( ) => {
216226 prev ( ) ;
@@ -219,7 +229,7 @@ export default function Waffle() {
219229 { '<' }
220230 </ button >
221231 < button
222- disabled = { cycleIndex >= cycleSteps . length - 1 }
232+ disabled = { cycleIndex >= cycleSteps . length }
223233 className = { styles . nextprevbutton }
224234 onClick = { ( ) => {
225235 next ( ) ;
0 commit comments