Skip to content

Commit 3370faa

Browse files
authored
fix: app crash when reach last index of undo stack (#111)
Avoid using an invalid layer index after undo operations.
1 parent c43357e commit 3370faa

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/keyboard/Keyboard.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export default function Keyboard() {
293293
);
294294

295295
let selectedBinding = useMemo(() => {
296-
if (keymap == null || selectedKeyPosition == null) {
296+
if (keymap == null || selectedKeyPosition == null || !keymap.layers[selectedLayerIndex]) {
297297
return null;
298298
}
299299

@@ -490,6 +490,16 @@ export default function Keyboard() {
490490
[conn, undoRedo, keymap]
491491
);
492492

493+
useEffect(() => {
494+
if (!keymap?.layers) return;
495+
496+
const layers = keymap.layers.length - 1;
497+
498+
if (selectedLayerIndex > layers) {
499+
setSelectedLayerIndex(layers);
500+
}
501+
}, [keymap, selectedLayerIndex]);
502+
493503
return (
494504
<div className="grid grid-cols-[auto_1fr] grid-rows-[1fr_minmax(10em,auto)] bg-base-300 max-w-full min-w-0 min-h-0">
495505
<div className="p-2 flex flex-col gap-2 bg-base-200 row-span-2">

0 commit comments

Comments
 (0)