- {isSearchAlgorithm ? (
+ {isSearching ? (
-
+
diff --git a/components/visualizer/SearchVisualization.tsx b/components/visualizer/SearchVisualization.tsx
index 13a36d1..47bb8c0 100644
--- a/components/visualizer/SearchVisualization.tsx
+++ b/components/visualizer/SearchVisualization.tsx
@@ -1,4 +1,7 @@
import { SearchStep } from "@/lib/types";
+import { useAlgorithm } from "@/context/AlgorithmContext";
+import { getAlgorithmByName } from "@/lib/algorithms";
+import { useState, useEffect } from "react";
interface SearchVisualizationProps {
step: SearchStep;
@@ -9,13 +12,61 @@ export default function SearchVisualization({
step,
}: SearchVisualizationProps) {
const { array, current, target, found, visited } = step;
+ const { state, dispatch } = useAlgorithm();
+ const [inputTarget, setInputTarget] = useState(target.toString());
+
+ // Update the input field when the target changes externally
+ useEffect(() => {
+ setInputTarget(target.toString());
+ }, [target]);
+
+ const handleTargetChange = (e: React.ChangeEvent
) => {
+ const newValue = e.target.value;
+ setInputTarget(newValue);
+
+ // Only process valid numbers
+ const newTarget = parseInt(newValue);
+ if (!isNaN(newTarget) && newTarget !== target) {
+ updateTarget(newTarget);
+ }
+ };
+
+ const updateTarget = (newTarget: number) => {
+ console.log(newTarget);
+ // Update the target value
+ dispatch({ type: "SET_TARGET", payload: newTarget });
+
+ // Regenerate visualization with the new target
+ const algorithmFunction = getAlgorithmByName(state.algorithm);
+ console.log(algorithmFunction);
+ if (algorithmFunction) {
+ try {
+ const viz = algorithmFunction(state.data, newTarget);
+ console.log(viz);
+ dispatch({ type: "GENERATE_VISUALIZATION", payload: viz });
+ } catch (error) {
+ console.error("Error generating visualization:", error);
+ }
+ }
+ };
return (
-
-
Searching for value:
-
{target}
-
+
+
+
+
+
+
+
Status:
void;
- onSetTarget?: () => void;
}
export default function VisualizerControls({
currentStep,
totalSteps,
onGenerateNewArray,
- onSetTarget,
}: VisualizerControlsProps) {
const { state, dispatch } = useAlgorithm();
const { isPlaying, speed } = state;
@@ -124,9 +122,9 @@ export default function VisualizerControls({
-
+
- Progress
+ Progress
Step {currentStep + 1} of {totalSteps}
@@ -210,30 +208,6 @@ export default function VisualizerControls({
-
- {onSetTarget && (
-
-
-
- )}
);
}