Skip to content

Commit 4d05866

Browse files
Nicellpetejohanson
authored andcommitted
refactor
1 parent b0e41c8 commit 4d05866

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/keyboard/Keyboard.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { BehaviorBindingPicker } from "../behaviors/BehaviorBindingPicker";
2929
import { produce } from "immer";
3030
import { LockStateContext } from "../rpc/LockStateContext";
3131
import { LockState } from "@zmkfirmware/zmk-studio-ts-client/core";
32-
import { LayoutZoom } from "./PhysicalLayout";
32+
import { deserializeLayoutZoom, LayoutZoom } from "./PhysicalLayout";
3333
import { useLocalStorageState } from "../misc/useLocalStorageState";
3434

3535
type BehaviorMap = Record<number, GetBehaviorDetailsResponse>;
@@ -175,12 +175,7 @@ export default function Keyboard() {
175175
);
176176

177177
const [keymapScale, setKeymapScale] = useLocalStorageState<LayoutZoom>("keymapScale", "auto", {
178-
deserialize: (value) => {
179-
if (value === "auto") {
180-
return "auto";
181-
}
182-
return parseFloat(value) || "auto";
183-
}
178+
deserialize: deserializeLayoutZoom,
184179
});
185180

186181
const [selectedLayerIndex, setSelectedLayerIndex] = useState<number>(0);
@@ -539,12 +534,8 @@ export default function Keyboard() {
539534
className="absolute top-2 right-2 h-8 rounded px-2"
540535
value={keymapScale}
541536
onChange={(e) => {
542-
const value = e.target.value;
543-
if (value === "auto") {
544-
setKeymapScale("auto");
545-
} else {
546-
setKeymapScale(parseFloat(value));
547-
}
537+
const value = deserializeLayoutZoom(e.target.value);
538+
setKeymapScale(value);
548539
}}
549540
>
550541
<option value="auto">Auto</option>

src/keyboard/PhysicalLayout.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export type KeyPosition = PropsWithChildren<{
2020

2121
export type LayoutZoom = number | "auto";
2222

23+
export function deserializeLayoutZoom(value: string): LayoutZoom {
24+
if (value === "auto") {
25+
return "auto";
26+
}
27+
return parseFloat(value) || "auto";
28+
}
29+
2330
interface PhysicalLayoutProps {
2431
positions: Array<KeyPosition>;
2532
selectedPosition?: number;

src/misc/useLocalStorageState.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import { useEffect, useState } from "react";
22

3-
export function useLocalStorageState<T>(key: string, defaultValue: T, options?: { serialize?: (value: T) => string; deserialize?: (value: string) => T; }) {
3+
function basicSerialize<T>(value: T): string {
4+
if (typeof value === "object") {
5+
return JSON.stringify(value);
6+
}
7+
return String(value);
8+
}
9+
10+
export function useLocalStorageState<T>(
11+
key: string,
12+
defaultValue: T,
13+
options?: {
14+
serialize?: (value: T) => string;
15+
deserialize?: (value: string) => T;
16+
},
17+
) {
418
const reactState = useState<T>(() => {
519
const savedValue = localStorage.getItem(key);
620
if (savedValue !== null) {
@@ -10,17 +24,15 @@ export function useLocalStorageState<T>(key: string, defaultValue: T, options?:
1024
return savedValue as T; // Assuming T is a string
1125
}
1226
return defaultValue;
13-
})
27+
});
1428

1529
const [state] = reactState;
1630

1731
useEffect(() => {
18-
const serializedState = options?.serialize?(state) ||
19-
((typeof state === 'object' && state !== null)
20-
? JSON.stringify(state) // Fallback for objects
21-
: String(state)); // Fallback for other types
32+
const serializedState =
33+
options?.serialize?.(state) || basicSerialize(state);
2234
localStorage.setItem(key, serializedState);
2335
}, [state, key, options]);
2436

2537
return reactState;
26-
}
38+
}

0 commit comments

Comments
 (0)