Skip to content

Commit 95e81d4

Browse files
committed
remember last active layout on load
1 parent 1afeb1b commit 95e81d4

1 file changed

Lines changed: 45 additions & 3 deletions

File tree

Keypad.Flasher.Client/src/ch55xbl.tsx

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const MODIFIER_BITS = [
3535

3636
const STORAGE_PREFIX = "ch55x-config";
3737
const storageAvailable = typeof window !== "undefined" && !!window.localStorage;
38+
const LAST_DEVICE_KEY = `${STORAGE_PREFIX}:last-device`;
3839

3940
type StoredConfig = { bindings: BindingProfileDto | null; layout: DeviceLayoutDto | null };
4041

@@ -77,6 +78,30 @@ const saveStoredConfig = (bootloaderId: number[], config: StoredConfig) => {
7778
}
7879
};
7980

81+
const loadLastBootloaderId = (): number[] | null => {
82+
if (!storageAvailable) return null;
83+
try {
84+
const raw = window.localStorage.getItem(LAST_DEVICE_KEY);
85+
if (!raw) return null;
86+
const parsed = JSON.parse(raw);
87+
if (Array.isArray(parsed) && parsed.every((n) => typeof n === "number")) {
88+
return parsed as number[];
89+
}
90+
} catch {
91+
// ignore parse/storage errors
92+
}
93+
return null;
94+
};
95+
96+
const saveLastBootloaderId = (bootloaderId: number[]) => {
97+
if (!storageAvailable) return;
98+
try {
99+
window.localStorage.setItem(LAST_DEVICE_KEY, JSON.stringify(bootloaderId));
100+
} catch {
101+
// ignore storage errors
102+
}
103+
};
104+
80105
type KeyOption = { value: number; label: string };
81106
type KeyOptionGroup = { label: string; options: KeyOption[] };
82107

@@ -316,6 +341,7 @@ export default function CH55xBootloaderMinimal() {
316341
const [devMode, setDevMode] = useState<boolean>(false);
317342
const [debugFirmware, setDebugFirmware] = useState<boolean>(false);
318343
const [selectedProfile, setSelectedProfile] = useState<KnownDeviceProfile | null>(null);
344+
const [rememberedBootloaderId, setRememberedBootloaderId] = useState<number[] | null>(null);
319345
const [currentBindings, setCurrentBindings] = useState<BindingProfileDto | null>(null);
320346
const [selectedLayout, setSelectedLayout] = useState<DeviceLayoutDto | null>(null);
321347
const [editTarget, setEditTarget] = useState<EditTarget | null>(null);
@@ -394,6 +420,19 @@ export default function CH55xBootloaderMinimal() {
394420
}
395421
}, [devMode, debugFirmware]);
396422

423+
useEffect(() => {
424+
const lastId = loadLastBootloaderId();
425+
if (!lastId) return;
426+
setRememberedBootloaderId(lastId);
427+
const profile = findProfileForBootloaderId(lastId);
428+
setSelectedProfile(profile);
429+
const stored = loadStoredConfig(lastId);
430+
const nextLayout = stored?.layout ?? (profile?.layout ? cloneLayout(profile.layout) : null);
431+
const nextBindings = stored?.bindings ?? profile?.defaultBindings ?? null;
432+
setSelectedLayout(nextLayout);
433+
setCurrentBindings(nextBindings);
434+
}, []);
435+
397436
const disconnectClient = useCallback(async (nextStatus?: Status, reboot?: boolean) => {
398437
const client = clientRef.current;
399438
if (client) {
@@ -456,6 +495,8 @@ export default function CH55xBootloaderMinimal() {
456495
lastBootloaderIdRef.current = info.id;
457496
setConnectedInfo(info);
458497
const profile = findProfileForBootloaderId(info.id);
498+
setRememberedBootloaderId(info.id);
499+
saveLastBootloaderId(info.id);
459500
setSelectedProfile(profile);
460501
const stored = loadStoredConfig(info.id);
461502
if (!sameDevice || !selectedLayout) {
@@ -501,9 +542,10 @@ export default function CH55xBootloaderMinimal() {
501542
}, [disconnectClient]);
502543

503544
useEffect(() => {
504-
if (!connectedInfo) return;
505-
saveStoredConfig(connectedInfo.id, { bindings: currentBindings, layout: selectedLayout });
506-
}, [connectedInfo, currentBindings, selectedLayout]);
545+
const targetId = connectedInfo?.id ?? rememberedBootloaderId;
546+
if (!targetId) return;
547+
saveStoredConfig(targetId, { bindings: currentBindings, layout: selectedLayout });
548+
}, [connectedInfo, rememberedBootloaderId, currentBindings, selectedLayout]);
507549

508550
useEffect(() => {
509551
if (!webUsbAvailable || typeof navigator === "undefined" || !navigator.usb) return;

0 commit comments

Comments
 (0)