Skip to content

Commit 9f428f1

Browse files
committed
fix: app crash when reach last index of undo stack
1 parent e5ab3b5 commit 9f428f1

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
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">

src/keyboard/LayerPicker.tsx

+11-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface LayerPickerProps {
3232
onLayerNameChanged?: (
3333
id: number,
3434
oldName: string,
35-
newName: string
35+
newName: string,
3636
) => void | Promise<void>;
3737
}
3838

@@ -53,7 +53,7 @@ const EditLabelModal = ({
5353
handleSaveNewLabel: (
5454
id: number,
5555
oldName: string,
56-
newName: string | null
56+
newName: string | null,
5757
) => void;
5858
}) => {
5959
const ref = useModalRef(open);
@@ -115,7 +115,7 @@ export const LayerPicker = ({
115115
...props
116116
}: LayerPickerProps) => {
117117
const [editLabelData, setEditLabelData] = useState<EditLabelData | null>(
118-
null
118+
null,
119119
);
120120

121121
const layer_items = useMemo(() => {
@@ -135,10 +135,10 @@ export const LayerPicker = ({
135135

136136
onLayerClicked?.(layer_items.findIndex((l) => s.has(l.id)));
137137
},
138-
[onLayerClicked, layer_items]
138+
[onLayerClicked, layer_items],
139139
);
140140

141-
let { dragAndDropHooks } = useDragAndDrop({
141+
const { dragAndDropHooks } = useDragAndDrop({
142142
renderDropIndicator(target) {
143143
return (
144144
<DropIndicator
@@ -150,8 +150,8 @@ export const LayerPicker = ({
150150
getItems: (keys) =>
151151
[...keys].map((key) => ({ "text/plain": key.toLocaleString() })),
152152
onReorder(e) {
153-
let startIndex = layer_items.findIndex((l) => e.keys.has(l.id));
154-
let endIndex = layer_items.findIndex((l) => l.id === e.target.key);
153+
const startIndex = layer_items.findIndex((l) => e.keys.has(l.id));
154+
const endIndex = layer_items.findIndex((l) => l.id === e.target.key);
155155
onLayerMoved?.(startIndex, endIndex);
156156
},
157157
});
@@ -162,17 +162,17 @@ export const LayerPicker = ({
162162
onLayerNameChanged?.(id, oldName, newName);
163163
}
164164
},
165-
[onLayerNameChanged]
165+
[onLayerNameChanged],
166166
);
167167

168168
return (
169169
<div className="flex flex-col min-w-40">
170-
<div className="grid grid-cols-[1fr_auto_auto] items-center">
170+
<div className="grid grid-cols-[1fr_auto_auto] items-center gap-1">
171171
<Label className="after:content-[':'] text-sm">Layers</Label>
172172
{onRemoveClicked && (
173173
<button
174174
type="button"
175-
className="hover:text-primary-content hover:bg-primary rounded-sm"
175+
className="hover:text-primary-content hover:bg-primary rounded-sm disabled:opacity-50 disabled:pointer-events-none"
176176
disabled={!canRemove}
177177
onClick={onRemoveClicked}
178178
>
@@ -183,7 +183,7 @@ export const LayerPicker = ({
183183
<button
184184
type="button"
185185
disabled={!canAdd}
186-
className="hover:text-primary-content ml-1 hover:bg-primary rounded-sm disabled:text-gray-500 disabled:hover:bg-base-300 disabled:cursor-not-allowed"
186+
className="hover:text-primary-content hover:bg-primary rounded-sm disabled:opacity-50 disabled:pointer-events-none"
187187
onClick={onAddClicked}
188188
>
189189
<Plus className="size-4" />

0 commit comments

Comments
 (0)