11import type { SearchStep } from "@/lib/types" ;
2- import { useAlgorithm } from "@/context/AlgorithmContext" ;
3- import { getSearchAlgorithm } from "@/lib/algorithms" ;
4- import { useState , useEffect } from "react" ;
52
6- interface SearchVisualizationProps {
3+ interface ArrayCellsProps {
74 step : SearchStep ;
8- maxValue : number ;
95}
106
11- export default function SearchVisualization ( {
12- step,
13- } : SearchVisualizationProps ) {
7+ export default function ArrayCells ( { step } : ArrayCellsProps ) {
148 const { array, current, target, found, visited } = step ;
15- const { state, dispatch } = useAlgorithm ( ) ;
16- const [ inputTarget , setInputTarget ] = useState ( target . toString ( ) ) ;
17-
18- // Update the input field when the target changes externally
19- useEffect ( ( ) => {
20- setInputTarget ( target . toString ( ) ) ;
21- } , [ target ] ) ;
22-
23- const handleTargetChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
24- const newValue = e . target . value ;
25- setInputTarget ( newValue ) ;
26-
27- // Only process valid numbers
28- const newTarget = parseInt ( newValue ) ;
29- if ( ! isNaN ( newTarget ) && newTarget !== target ) {
30- updateTarget ( newTarget ) ;
31- }
32- } ;
33-
34- const updateTarget = ( newTarget : number ) => {
35- // Update the target value
36- dispatch ( { type : "SET_TARGET" , payload : newTarget } ) ;
37-
38- // Regenerate visualization with the new target
39- const algorithmFunction = getSearchAlgorithm ( state . algorithm ) ;
40- if ( algorithmFunction ) {
41- try {
42- const viz = algorithmFunction ( state . data , newTarget ) ;
43- dispatch ( { type : "GENERATE_VISUALIZATION" , payload : viz } ) ;
44- } catch ( error ) {
45- console . error ( "Error generating visualization:" , error ) ;
46- }
47- }
48- } ;
499
5010 const isNotFound = current === - 1 && visited . length > 0 ;
5111 let statusClass = "text-gray-600" ;
@@ -60,37 +20,22 @@ export default function SearchVisualization({
6020
6121 return (
6222 < div className = "flex w-full flex-col items-center space-y-8 bg-white p-6" >
63- < div className = "mb-4 flex w-full flex-wrap items-center gap-4" >
64- < div className = "flex items-center gap-2" >
65- < label htmlFor = "target-input" className = "font-medium text-gray-600" >
66- Target Value:
67- </ label >
68- < input
69- id = "target-input"
70- type = "number"
71- value = { inputTarget }
72- onChange = { handleTargetChange }
73- className = "w-16 rounded border border-gray-300 px-2 py-1 text-center text-gray-600"
74- />
75- </ div >
76-
77- < div className = "ml-auto font-medium text-gray-600" >
23+ < div className = "mb-4 flex w-full items-center justify-end" >
24+ < div className = "font-medium text-gray-600" >
7825 Status:
7926 < span className = { `ml-2 font-bold ${ statusClass } ` } > { statusText } </ span >
8027 </ div >
8128 </ div >
8229
8330 < div className = "flex w-full items-center justify-center" >
8431 { array . map ( ( value , index ) => {
85- // Determine the element's state for styling
86- let stateClass = "bg-blue-400" ; // Default state
87-
32+ let stateClass = "bg-blue-400" ;
8833 if ( found && index === current ) {
89- stateClass = "bg-green-500" ; // Found state
34+ stateClass = "bg-green-500" ;
9035 } else if ( index === current && ! found ) {
91- stateClass = "bg-yellow-400" ; // Current examining state
36+ stateClass = "bg-yellow-400" ;
9237 } else if ( visited . includes ( index ) && ! found ) {
93- stateClass = "bg-gray-400" ; // Already visited state
38+ stateClass = "bg-gray-400" ;
9439 }
9540
9641 return (
0 commit comments