-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNav.tsx
130 lines (123 loc) · 3.76 KB
/
Nav.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { MutableRefObject, useState } from "react";
import { usePathfinding } from "../hooks/usePathfinding";
import { useTile } from "../hooks/useTile";
import {
EXTENDED_SLEEP_TIME,
MAZES,
PATHFINDING_ALGORITHMS,
SLEEP_TIME,
SPEEDS,
} from "../utils/constants";
import { resetGrid } from "../utils/resetGrid";
import { AlgorithmType, MazeType, SpeedType } from "../utils/types";
import { Select } from "./Select";
import { useSpeed } from "../hooks/useSpeed";
import { runMazeAlgorithm } from "../utils/runMazeAlgorithm";
import { PlayButton } from "./PlayButton";
import { runPathfindingAlgorithm } from "../utils/runPathfindingAlgorithm";
import { animatePath } from "../utils/animatePath";
export function Nav({
isVisualizationRunningRef,
}: {
isVisualizationRunningRef: MutableRefObject<boolean>;
}) {
const [isDisabled, setIsDisabled] = useState(false);
const {
maze,
setMaze,
grid,
setGrid,
isGraphVisualized,
setIsGraphVisualized,
algorithm,
setAlgorithm,
} = usePathfinding();
const { startTile, endTile } = useTile();
const { speed, setSpeed } = useSpeed();
const handleGenerateMaze = (maze: MazeType) => {
if (maze === "NONE") {
setMaze(maze);
resetGrid({ grid, startTile, endTile });
return;
}
setMaze(maze);
setIsDisabled(true);
runMazeAlgorithm({
maze,
grid,
startTile,
endTile,
setIsDisabled,
speed,
});
const newGrid = grid.slice();
setGrid(newGrid);
setIsGraphVisualized(false);
};
const handlerRunVisualizer = () => {
if (isGraphVisualized) {
setIsGraphVisualized(false);
resetGrid({ grid: grid.slice(), startTile, endTile });
return;
}
const { traversedTiles, path } = runPathfindingAlgorithm({
algorithm,
grid,
startTile,
endTile,
});
animatePath(traversedTiles, path, startTile, endTile, speed);
setIsDisabled(true);
isVisualizationRunningRef.current = true;
setTimeout(() => {
const newGrid = grid.slice();
setGrid(newGrid);
setIsGraphVisualized(true);
setIsDisabled(false);
isVisualizationRunningRef.current = false;
}, SLEEP_TIME * (traversedTiles.length + SLEEP_TIME * 2) + EXTENDED_SLEEP_TIME * (path.length + 60) * SPEEDS.find((s) => s.value === speed)!.value);
};
return (
<div className="flex items-center justify-center min-h-[4.5rem] border-b shadow-gray-600 sm:px-5 px-0">
<div className="flex items-center lg:justify-between justify-center w-full sm:w-[52rem]">
<h1 className="lg:flex hidden w-[40%] text-2xl pl-1">
Pathfinding Visualizer
</h1>
<div className="flex sm:items-end items-center justify-start sm:justify-between sm:flex-row flex-col sm:space-y-0 space-y-3 sm:py-0 py-4 sm:space-x-4">
<Select
label="Maze"
value={maze}
options={MAZES}
isDisabled={isDisabled}
onChange={(e) => {
handleGenerateMaze(e.target.value as MazeType);
}}
/>
<Select
label="Graph"
value={algorithm}
isDisabled={isDisabled}
options={PATHFINDING_ALGORITHMS}
onChange={(e) => {
setAlgorithm(e.target.value as AlgorithmType);
}}
/>
<Select
label="Speed"
value={speed}
options={SPEEDS}
isDisabled={isDisabled}
onChange={(e) => {
setSpeed(parseInt(e.target.value) as SpeedType);
}}
/>
<PlayButton
isDisabled={isDisabled}
isGraphVisualized={isGraphVisualized}
handlerRunVisualizer={handlerRunVisualizer}
/>
</div>
</div>
</div>
);
}