Skip to content

Commit 3319399

Browse files
committed
update ts
1 parent 87234d1 commit 3319399

12 files changed

+40
-18
lines changed

src/App.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const App = () => {
5757
selectedAlgorithm={selectedAlgorithm}
5858
currentTrack={history.currentTrack}
5959
algorithmState={algorithmState}
60-
player={player}
60+
playerState={player.playerState}
6161
/>
6262
</section>
6363
);

src/components/Player/Player.jsx renamed to src/components/Player/Player.tsx

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
import { useCallback, useRef } from 'react';
2-
1+
import React, { useCallback, useRef } from 'react';
32
import { speedOptions } from '../../constants';
4-
53
import ResetIcon from '../icon/ResetIcon';
64
import NextStepIcon from '../icon/NextStepIcon';
75
import PreviousStepIcon from '../icon/PreviousStepIcon';
86
import PlayIcon from '../icon/PlayIcon';
9-
107
import InputRange from '../InputRange/InputRange';
118
import Button from '../Button/Button';
129
import PauseIcon from '../icon/PauseIcon';
1310
import DropDown from '../DropDown/DropDown';
11+
import { AlgorithmState, PlayerState } from '../../types';
1412

1513
import './Player.css';
1614

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> = ({
1829
selectAlgorithm,
1930
goToNextStep,
2031
goToPreviousStep,
@@ -24,10 +35,11 @@ const Player = ({
2435
handleAlgorithmRun,
2536
selectedAlgorithm,
2637
algorithmState,
27-
player,
38+
playerState,
2839
}) => {
29-
const intervalRef = useRef(null);
30-
const startInterval = (action) => {
40+
const intervalRef = useRef<NodeJS.Timeout | null>(null);
41+
42+
const startInterval = (action: () => void) => {
3143
if (intervalRef.current === null) {
3244
intervalRef.current = setInterval(() => {
3345
action();
@@ -42,29 +54,29 @@ const Player = ({
4254
intervalRef.current = null;
4355
};
4456

45-
const handleMouseDown = (action) => {
57+
const handleMouseDown = (action: () => void) => {
4658
action();
4759
startInterval(action);
4860
};
4961
const handleMouseUpOrLeave = () => {
5062
clearTimer();
5163
};
5264

53-
const getButtonText = useCallback(() => {
65+
const getButtonIcon = useCallback(() => {
5466
if (algorithmState === 'notStarted') {
5567
return <PlayIcon />;
5668
}
5769
if (algorithmState === 'finished') {
5870
return <ResetIcon />;
5971
}
6072

61-
if (player.playerState === 'play') {
73+
if (playerState === 'play') {
6274
return <PauseIcon />;
6375
}
6476
{
6577
return <PlayIcon />;
6678
}
67-
}, [algorithmState, player]);
79+
}, [algorithmState, playerState]);
6880

6981
return (
7082
<div className="player-container">
@@ -84,7 +96,7 @@ const Player = ({
8496
>
8597
<PreviousStepIcon />
8698
</Button>
87-
<Button onClick={handleAlgorithmRun}>{getButtonText()}</Button>
99+
<Button onClick={handleAlgorithmRun}>{getButtonIcon()}</Button>
88100

89101
<Button
90102
onMouseDown={() => handleMouseDown(goToNextStep)}

src/components/icon/NextStepIcon.jsx renamed to src/components/icon/NextStepIcon.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
const NextStepIcon = () => {
23
return (
34
<div>
File renamed without changes.

src/components/icon/PlayIcon.jsx renamed to src/components/icon/PlayIcon.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import React from 'react';
2+
13
const PlayIcon = () => {
24
return (
35
<svg

src/components/icon/PreviousStepIcon.jsx renamed to src/components/icon/PreviousStepIcon.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import React from 'react';
2+
13
export const PreviousStepIcon = () => {
24
return (
35
<svg

src/components/icon/ResetIcon.jsx renamed to src/components/icon/ResetIcon.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import React from 'react';
2+
13
const ResetIcon = () => {
24
return (
35
<svg

src/components/icon/ResumePlay.jsx renamed to src/components/icon/ResumePlay.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import React from 'react';
2+
13
const ResumePlay = () => {
24
return (
35
<svg

src/hooks/useAlgorithm.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { useCallback, useState } from 'react';
22
import { generateRandomArray } from '../utils/generateRandomArray';
33
import { algorithms } from '../algorithms';
4-
5-
type AlgorithmState = 'notStarted' | 'started' | 'finished';
4+
import { AlgorithmState } from '../types';
65

76
export const useAlgorithm = ({ history, player }) => {
87
const [selectedAlgorithm, setSelectedAlgorithm] = useState('quick');

src/hooks/usePlayer.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useEffect, useRef, useState } from 'react';
2-
3-
type PlayerState = 'forward' | 'backward' | 'pause' | 'play' | null;
2+
import { PlayerState } from '../types';
43

54
export const usePlayer = () => {
65
const [playerState, setPlayerState] = useState<PlayerState>(null);

src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type AlgorithmState = 'notStarted' | 'started' | 'finished';
2+
3+
export type PlayerState = 'forward' | 'backward' | 'pause' | 'play' | null;

0 commit comments

Comments
 (0)