-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputMatrix.tsx
More file actions
151 lines (128 loc) · 4.89 KB
/
OutputMatrix.tsx
File metadata and controls
151 lines (128 loc) · 4.89 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { useState, useImperativeHandle, forwardRef, useEffect, useRef } from "react";
import { RoundedArrow } from "./RoundedArrow";
interface MatrixProps {
rows: number;
cols: number;
input?: boolean;
values?: number[][];
className?: string;
matrixName?: string;
animationState?: string;
}
export interface MatrixRef {
getValues: () => number[][];
}
export const OutputMatrix = forwardRef<MatrixRef, MatrixProps>(({ rows, cols, input = false, values, className, matrixName, animationState }, ref) => {
const defaultValues = Array.from({ length: rows }, (_, i) =>
Array.from({ length: cols }, (_, j) => values?.[i]?.[j] ?? 0)
);
const [matrixValues, setMatrixValues] = useState(defaultValues);
const [highlightedCells, setHighlightedCells] = useState<boolean[][]>(
Array.from({ length: rows }, () => Array(cols).fill(false))
);
const prevValuesRef = useRef<number[][]>(defaultValues);
// Add this useEffect to update values when animation state changes to "update"
useEffect(() => {
// Clear any existing highlights when animation state changes
setHighlightedCells(Array.from({ length: rows }, () => Array(cols).fill(false)));
if (values) {
const newValues = Array.from({ length: rows }, (_, i) =>
Array.from({ length: cols }, (_, j) => values?.[i]?.[j] ?? 0)
);
if (animationState === "idle") {
// Determine which cells have changed
const newHighlights = Array.from({ length: rows }, (_, i) =>
Array.from({ length: cols }, (_, j) =>
newValues[i][j] !== prevValuesRef.current[i]?.[j]
)
);
setMatrixValues(newValues);
setHighlightedCells(newHighlights);
prevValuesRef.current = newValues;
// Reset highlights after a short delay
const timer = setTimeout(() => {
setHighlightedCells(Array.from({ length: rows }, () => Array(cols).fill(false)));
}, 1000);
return () => clearTimeout(timer);
} else if (animationState === "init") {
// Just update values without highlighting when in init mode
setMatrixValues(newValues);
prevValuesRef.current = newValues;
}
}
}, [animationState, values, rows, cols]);
// Expose the getValues method to parent components
useImperativeHandle(ref, () => ({
getValues: () => matrixValues
}));
const bracket_height = 40 * rows + 8;
const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>, i: number, j: number) => {
const key = e.key;
// Only allow digits
if (!/^\d$/.test(key) || !input) {
e.preventDefault();
return;
}
const currentValue = matrixValues[i][j];
const currentStr = currentValue.toFixed(2);
// Shift digits to the left and add new digit to the right
const newStr = currentStr.slice(1) + key;
const newValue = parseFloat(newStr) * 10;
// Update the matrix value
const newMatrixValues = [...matrixValues];
newMatrixValues[i][j] = newValue;
setMatrixValues(newMatrixValues);
e.preventDefault();
};
return (
<div className="relative m-4">
{/* Left bracket */}
<div className="absolute -left-[10px] -top-[10px] -translate-y-1/2">
<RoundedArrow
points={[[0, 0], [-5, 0], [-5, -bracket_height], [0, -bracket_height]]}
color="#666"
strokeWidth={0.75}
className="transform"
radius={2}
arrowHeadSize={0}
/>
</div>
{/* Right bracket */}
<div className="absolute -right-[5px] -top-[10px] -translate-y-1/2">
<RoundedArrow
points={[[0, 0], [5, 0], [5, -bracket_height], [0, -bracket_height]]}
color="#666"
strokeWidth={0.75}
className="transform"
radius={2}
arrowHeadSize={0}
/>
</div>
<div className={`inline-grid gap-2 ${className}`} style={{ gridTemplateColumns: `repeat(${cols}, 60px)` }}>
{matrixValues.map((row, i) =>
row.map((val, j) => (
<input
key={`${i}-${j}`}
type="text"
inputMode="numeric"
value={val.toFixed(2)}
disabled={!input}
onKeyDown={(e) => input && handleKeyPress(e, i, j)}
readOnly
className={`text-center text-white font-mono text-sm px-2 py-1 bg-black border rounded ${
highlightedCells[i][j] ? 'border-blue-500' : 'border-neutral-700'
} ${
input ? "focus:outline-none focus:border-blue-500" : "text-neutral-500 cursor-not-allowed"
} transition-colors duration-300`}
/>
))
)}
</div>
<p className="mt-2 text-center text-sm font-light">
{matrixName}
</p>
</div>
);
});
// Add display name to the component
OutputMatrix.displayName = 'OutputMatrix';