diff --git a/src/keyboard/Keyboard.tsx b/src/keyboard/Keyboard.tsx index e8ffaf7..e56b836 100644 --- a/src/keyboard/Keyboard.tsx +++ b/src/keyboard/Keyboard.tsx @@ -103,13 +103,13 @@ function useLayouts(): [ PhysicalLayout[] | undefined, React.Dispatch>, number, - React.Dispatch> + React.Dispatch>, ] { let connection = useContext(ConnectionContext); let lockState = useContext(LockStateContext); const [layouts, setLayouts] = useState( - undefined + undefined, ); const [selectedPhysicalLayoutIndex, setSelectedPhysicalLayoutIndex] = useState(0); @@ -137,7 +137,7 @@ function useLayouts(): [ if (!ignore) { setLayouts(response?.keymap?.getPhysicalLayouts?.layouts); setSelectedPhysicalLayoutIndex( - response?.keymap?.getPhysicalLayouts?.activeLayoutIndex || 0 + response?.keymap?.getPhysicalLayouts?.activeLayoutIndex || 0, ); } } @@ -171,12 +171,16 @@ export default function Keyboard() { console.log("Got the keymap!"); return keymap?.keymap?.getKeymap; }, - true + true, ); - const [keymapScale, setKeymapScale] = useLocalStorageState("keymapScale", "auto", { - deserialize: deserializeLayoutZoom, - }); + const [keymapScale, setKeymapScale] = useLocalStorageState( + "keymapScale", + "auto", + { + deserialize: deserializeLayoutZoom, + }, + ); const [selectedLayerIndex, setSelectedLayerIndex] = useState(0); const [selectedKeyPosition, setSelectedKeyPosition] = useState< @@ -208,7 +212,7 @@ export default function Keyboard() { } else { console.error( "Failed to set the active physical layout err:", - resp?.keymap?.setActivePhysicalLayout?.err + resp?.keymap?.setActivePhysicalLayout?.err, ); } } @@ -227,14 +231,14 @@ export default function Keyboard() { }; }); }, - [undoRedo, selectedPhysicalLayoutIndex] + [undoRedo, selectedPhysicalLayoutIndex], ); let doUpdateBinding = useCallback( (binding: BehaviorBinding) => { if (!keymap || selectedKeyPosition === undefined) { console.error( - "Can't update binding without a selected key position and loaded keymap" + "Can't update binding without a selected key position and loaded keymap", ); return; } @@ -259,7 +263,7 @@ export default function Keyboard() { setKeymap( produce((draft: any) => { draft.layers[layer].bindings[keyPosition] = binding; - }) + }), ); } else { console.error("Failed to set binding", resp.keymap?.setLayerBinding); @@ -282,18 +286,22 @@ export default function Keyboard() { setKeymap( produce((draft: any) => { draft.layers[layer].bindings[keyPosition] = oldBinding; - }) + }), ); } else { } }; }); }, - [conn, keymap, undoRedo, selectedLayerIndex, selectedKeyPosition] + [conn, keymap, undoRedo, selectedLayerIndex, selectedKeyPosition], ); let selectedBinding = useMemo(() => { - if (keymap == null || selectedKeyPosition == null) { + if ( + keymap == null || + selectedKeyPosition == null || + !keymap.layers[selectedLayerIndex] + ) { return null; } @@ -324,7 +332,7 @@ export default function Keyboard() { return () => doMove(end, start); }); }, - [undoRedo] + [undoRedo], ); const addLayer = useCallback(() => { @@ -341,7 +349,7 @@ export default function Keyboard() { produce((draft: any) => { draft.layers.push(resp.keymap!.addLayer!.ok!.layer); draft.availableLayers--; - }) + }), ); setSelectedLayerIndex(newSelection); @@ -368,12 +376,12 @@ export default function Keyboard() { produce((draft: any) => { draft.layers.splice(layerIndex, 1); draft.availableLayers++; - }) + }), ); } else { console.error("Remove error", resp.keymap?.removeLayer?.err); throw new Error( - "Failed to remove layer:" + resp.keymap?.removeLayer?.err + "Failed to remove layer:" + resp.keymap?.removeLayer?.err, ); } } @@ -402,12 +410,12 @@ export default function Keyboard() { produce((draft: any) => { draft.layers.splice(layerIndex, 1); draft.availableLayers++; - }) + }), ); } else { console.error("Remove error", resp.keymap?.removeLayer?.err); throw new Error( - "Failed to remove layer:" + resp.keymap?.removeLayer?.err + "Failed to remove layer:" + resp.keymap?.removeLayer?.err, ); } } @@ -427,13 +435,13 @@ export default function Keyboard() { produce((draft: any) => { draft.layers.splice(atIndex, 0, resp!.keymap!.restoreLayer!.ok); draft.availableLayers--; - }) + }), ); setSelectedLayerIndex(atIndex); } else { console.error("Remove error", resp.keymap?.restoreLayer?.err); throw new Error( - "Failed to restore layer:" + resp.keymap?.restoreLayer?.err + "Failed to restore layer:" + resp.keymap?.restoreLayer?.err, ); } } @@ -468,14 +476,14 @@ export default function Keyboard() { setKeymap( produce((draft: any) => { const layer_index = draft.layers.findIndex( - (l: Layer) => l.id == layerId + (l: Layer) => l.id == layerId, ); draft.layers[layer_index].name = name; - }) + }), ); } else { throw new Error( - "Failed to change layer name:" + resp.keymap?.setLayerProps + "Failed to change layer name:" + resp.keymap?.setLayerProps, ); } } @@ -487,9 +495,19 @@ export default function Keyboard() { }; }); }, - [conn, undoRedo, keymap] + [conn, undoRedo, keymap], ); + useEffect(() => { + if (!keymap?.layers) return; + + const layers = keymap.layers.length - 1; + + if (selectedLayerIndex > layers) { + setSelectedLayerIndex(layers); + } + }, [keymap, selectedLayerIndex]); + return (
diff --git a/src/keyboard/LayerPicker.tsx b/src/keyboard/LayerPicker.tsx index 1584191..3fb3270 100644 --- a/src/keyboard/LayerPicker.tsx +++ b/src/keyboard/LayerPicker.tsx @@ -32,7 +32,7 @@ interface LayerPickerProps { onLayerNameChanged?: ( id: number, oldName: string, - newName: string + newName: string, ) => void | Promise; } @@ -53,7 +53,7 @@ const EditLabelModal = ({ handleSaveNewLabel: ( id: number, oldName: string, - newName: string | null + newName: string | null, ) => void; }) => { const ref = useModalRef(open); @@ -115,7 +115,7 @@ export const LayerPicker = ({ ...props }: LayerPickerProps) => { const [editLabelData, setEditLabelData] = useState( - null + null, ); const layer_items = useMemo(() => { @@ -135,10 +135,10 @@ export const LayerPicker = ({ onLayerClicked?.(layer_items.findIndex((l) => s.has(l.id))); }, - [onLayerClicked, layer_items] + [onLayerClicked, layer_items], ); - let { dragAndDropHooks } = useDragAndDrop({ + const { dragAndDropHooks } = useDragAndDrop({ renderDropIndicator(target) { return ( [...keys].map((key) => ({ "text/plain": key.toLocaleString() })), onReorder(e) { - let startIndex = layer_items.findIndex((l) => e.keys.has(l.id)); - let endIndex = layer_items.findIndex((l) => l.id === e.target.key); + const startIndex = layer_items.findIndex((l) => e.keys.has(l.id)); + const endIndex = layer_items.findIndex((l) => l.id === e.target.key); onLayerMoved?.(startIndex, endIndex); }, }); @@ -162,17 +162,17 @@ export const LayerPicker = ({ onLayerNameChanged?.(id, oldName, newName); } }, - [onLayerNameChanged] + [onLayerNameChanged], ); return (
-
+
{onRemoveClicked && (