1
- import { useEffect , useState } from " react" ;
2
- import { selectionSort } from " ./components/algorithms/selectionSort" ;
3
- import { quickSort } from " ./components/algorithms/quickSort" ;
4
- import Array from " ./components/Array/Array" ;
5
- import { bubbleSort } from " ./components/algorithms/bubbleSort" ;
6
- import { insertionSort } from " ./components/algorithms/insertionSort" ;
7
- import { useAlgorithmState } from " ./components/hooks/useAlgorithmState" ;
8
- import { useEvaluationState } from " ./components/hooks/useEvaluationState" ;
9
- import PlayIcon from " ./components/icon/PlayIcon" ;
10
- import PauseIcon from " ./components/icon/PauseIcon" ;
11
- import ResumePlay from " ./components/icon/ResumePlay" ;
12
- import Player from " ./components/Player/Player" ;
13
- import { disableScroll } from " ./utils/disableScroll" ;
1
+ import { useEffect , useState } from ' react' ;
2
+ import { selectionSort } from ' ./components/algorithms/selectionSort' ;
3
+ import { quickSort } from ' ./components/algorithms/quickSort' ;
4
+ import Array from ' ./components/Array/Array' ;
5
+ import { bubbleSort } from ' ./components/algorithms/bubbleSort' ;
6
+ import { insertionSort } from ' ./components/algorithms/insertionSort' ;
7
+ import { useAlgorithmState } from ' ./components/hooks/useAlgorithmState' ;
8
+ import { useEvaluationState } from ' ./components/hooks/useEvaluationState' ;
9
+ import PlayIcon from ' ./components/icon/PlayIcon' ;
10
+ import PauseIcon from ' ./components/icon/PauseIcon' ;
11
+ import ResumePlay from ' ./components/icon/ResumePlay' ;
12
+ import Player from ' ./components/Player/Player' ;
13
+ import { disableScroll } from ' ./utils/disableScroll' ;
14
14
15
15
// type AlgState = 'started' | 'notStarted' | 'paused' | 'finished'
16
16
@@ -55,13 +55,13 @@ const App = () => {
55
55
} , [ shouldSort ] ) ;
56
56
57
57
const handleAlgorithmRun = async ( ) => {
58
- if ( evalState === " notStarted" ) {
58
+ if ( evalState === ' notStarted' ) {
59
59
await handleSort ( ) ;
60
- } else if ( evalState === " started" ) {
61
- setEvalState ( " paused" ) ;
62
- } else if ( evalState === " paused" ) {
63
- setEvalState ( " started" ) ;
64
- } else if ( evalState === " finished" ) {
60
+ } else if ( evalState === ' started' ) {
61
+ setEvalState ( ' paused' ) ;
62
+ } else if ( evalState === ' paused' ) {
63
+ setEvalState ( ' started' ) ;
64
+ } else if ( evalState === ' finished' ) {
65
65
resetArray ( ) ;
66
66
resetHistory ( ) ;
67
67
setShouldSort ( true ) ;
@@ -70,8 +70,8 @@ const App = () => {
70
70
71
71
const handleSort = async ( ) => {
72
72
try {
73
- setEvalState ( " started" ) ;
74
- if ( selectedAlgorithm === " selection" ) {
73
+ setEvalState ( ' started' ) ;
74
+ if ( selectedAlgorithm === ' selection' ) {
75
75
await selectionSort (
76
76
array ,
77
77
setArray ,
@@ -81,16 +81,17 @@ const App = () => {
81
81
speedRef ,
82
82
updateHistory ,
83
83
) ;
84
- } else if ( selectedAlgorithm === " bubble" ) {
84
+ } else if ( selectedAlgorithm === ' bubble' ) {
85
85
await bubbleSort (
86
86
array ,
87
87
setArray ,
88
88
setActiveIndex ,
89
89
setCompareIndex ,
90
90
evalStateRef ,
91
91
speedRef ,
92
+ updateHistory ,
92
93
) ;
93
- } else if ( selectedAlgorithm === " quick" ) {
94
+ } else if ( selectedAlgorithm === ' quick' ) {
94
95
await quickSort (
95
96
array ,
96
97
setActiveIndex ,
@@ -99,33 +100,35 @@ const App = () => {
99
100
setCompareIndex ,
100
101
evalStateRef ,
101
102
speedRef ,
103
+ updateHistory ,
102
104
) ;
103
- } else if ( selectedAlgorithm === " insertion" ) {
105
+ } else if ( selectedAlgorithm === ' insertion' ) {
104
106
await insertionSort (
105
107
array ,
106
108
setArray ,
107
109
setActiveIndex ,
108
110
setCompareIndex ,
109
111
evalStateRef ,
110
112
speedRef ,
113
+ updateHistory ,
111
114
) ;
112
115
}
113
- setEvalState ( " finished" ) ;
116
+ setEvalState ( ' finished' ) ;
114
117
resetTracking ( ) ;
115
118
} catch ( e ) {
116
- if ( e . message === " cancelSort" ) {
117
- console . log ( " Sort cancelled" ) ;
119
+ if ( e . message === ' cancelSort' ) {
120
+ console . log ( ' Sort cancelled' ) ;
118
121
resetTracking ( ) ;
119
122
}
120
123
}
121
124
} ;
122
125
123
126
const getButtonText = ( ) => {
124
- if ( evalState === " notStarted" ) {
127
+ if ( evalState === ' notStarted' ) {
125
128
return < ResumePlay /> ;
126
- } else if ( evalState === " started" ) {
129
+ } else if ( evalState === ' started' ) {
127
130
return < PauseIcon /> ;
128
- } else if ( evalState === " paused" ) {
131
+ } else if ( evalState === ' paused' ) {
129
132
return < ResumePlay /> ;
130
133
} else {
131
134
return < PlayIcon /> ;
@@ -134,21 +137,21 @@ const App = () => {
134
137
135
138
const selectAlgorithm = ( newAlgorithm ) => {
136
139
resetTracking ( ) ;
137
- setEvalState ( " notStarted" ) ;
140
+ setEvalState ( ' notStarted' ) ;
138
141
setSelectedAlgorithm ( newAlgorithm ) ;
139
142
resetHistory ( ) ;
140
143
resetArray ( ) ;
141
144
} ;
142
145
143
146
const resetAlgorithm = ( ) => {
144
- setEvalState ( " notStarted" ) ;
147
+ setEvalState ( ' notStarted' ) ;
145
148
resetTracking ( ) ;
146
149
resetArray ( ) ;
147
150
resetHistory ( ) ;
148
151
} ;
149
152
150
153
const barWidth = window . screen . width / array . length ;
151
- disableScroll ( )
154
+ disableScroll ( ) ;
152
155
153
156
return (
154
157
< section >
0 commit comments