Skip to content

Commit

Permalink
fix: app crash when reach last index of undo stack (#111)
Browse files Browse the repository at this point in the history
Avoid using an invalid layer index after undo operations.
  • Loading branch information
pongstr authored Jan 16, 2025
1 parent c43357e commit 3370faa
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/keyboard/Keyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export default function Keyboard() {
);

let selectedBinding = useMemo(() => {
if (keymap == null || selectedKeyPosition == null) {
if (keymap == null || selectedKeyPosition == null || !keymap.layers[selectedLayerIndex]) {
return null;
}

Expand Down Expand Up @@ -490,6 +490,16 @@ export default function Keyboard() {
[conn, undoRedo, keymap]
);

useEffect(() => {
if (!keymap?.layers) return;

const layers = keymap.layers.length - 1;

if (selectedLayerIndex > layers) {
setSelectedLayerIndex(layers);
}
}, [keymap, selectedLayerIndex]);

return (
<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">
<div className="p-2 flex flex-col gap-2 bg-base-200 row-span-2">
Expand Down

0 comments on commit 3370faa

Please sign in to comment.