1
- import { useCallback , useRef } from 'react' ;
2
-
1
+ import React , { useCallback , useRef } from 'react' ;
3
2
import { speedOptions } from '../../constants' ;
4
-
5
3
import ResetIcon from '../icon/ResetIcon' ;
6
4
import NextStepIcon from '../icon/NextStepIcon' ;
7
5
import PreviousStepIcon from '../icon/PreviousStepIcon' ;
8
6
import PlayIcon from '../icon/PlayIcon' ;
9
-
10
7
import InputRange from '../InputRange/InputRange' ;
11
8
import Button from '../Button/Button' ;
12
9
import PauseIcon from '../icon/PauseIcon' ;
13
10
import DropDown from '../DropDown/DropDown' ;
11
+ import { AlgorithmState , PlayerState } from '../../types' ;
14
12
15
13
import './Player.css' ;
16
14
17
- const Player = ( {
15
+ type PlayerProps = {
16
+ selectAlgorithm : ( algorithm : string ) => void ;
17
+ goToNextStep : ( ) => void ;
18
+ goToPreviousStep : ( ) => void ;
19
+ resetAlgorithm : ( ) => void ;
20
+ setSpeed : ( speed : number ) => void ;
21
+ handleAlgorithmRun : ( ) => void ;
22
+ speed : number ;
23
+ selectedAlgorithm : string ;
24
+ algorithmState : AlgorithmState ;
25
+ playerState : PlayerState ;
26
+ } ;
27
+
28
+ const Player : React . FC < PlayerProps > = ( {
18
29
selectAlgorithm,
19
30
goToNextStep,
20
31
goToPreviousStep,
@@ -24,10 +35,11 @@ const Player = ({
24
35
handleAlgorithmRun,
25
36
selectedAlgorithm,
26
37
algorithmState,
27
- player ,
38
+ playerState ,
28
39
} ) => {
29
- const intervalRef = useRef ( null ) ;
30
- const startInterval = ( action ) => {
40
+ const intervalRef = useRef < NodeJS . Timeout | null > ( null ) ;
41
+
42
+ const startInterval = ( action : ( ) => void ) => {
31
43
if ( intervalRef . current === null ) {
32
44
intervalRef . current = setInterval ( ( ) => {
33
45
action ( ) ;
@@ -42,29 +54,29 @@ const Player = ({
42
54
intervalRef . current = null ;
43
55
} ;
44
56
45
- const handleMouseDown = ( action ) => {
57
+ const handleMouseDown = ( action : ( ) => void ) => {
46
58
action ( ) ;
47
59
startInterval ( action ) ;
48
60
} ;
49
61
const handleMouseUpOrLeave = ( ) => {
50
62
clearTimer ( ) ;
51
63
} ;
52
64
53
- const getButtonText = useCallback ( ( ) => {
65
+ const getButtonIcon = useCallback ( ( ) => {
54
66
if ( algorithmState === 'notStarted' ) {
55
67
return < PlayIcon /> ;
56
68
}
57
69
if ( algorithmState === 'finished' ) {
58
70
return < ResetIcon /> ;
59
71
}
60
72
61
- if ( player . playerState === 'play' ) {
73
+ if ( playerState === 'play' ) {
62
74
return < PauseIcon /> ;
63
75
}
64
76
{
65
77
return < PlayIcon /> ;
66
78
}
67
- } , [ algorithmState , player ] ) ;
79
+ } , [ algorithmState , playerState ] ) ;
68
80
69
81
return (
70
82
< div className = "player-container" >
@@ -84,7 +96,7 @@ const Player = ({
84
96
>
85
97
< PreviousStepIcon />
86
98
</ Button >
87
- < Button onClick = { handleAlgorithmRun } > { getButtonText ( ) } </ Button >
99
+ < Button onClick = { handleAlgorithmRun } > { getButtonIcon ( ) } </ Button >
88
100
89
101
< Button
90
102
onMouseDown = { ( ) => handleMouseDown ( goToNextStep ) }
0 commit comments