Skip to content

Commit 1b5062c

Browse files
authored
fix(ui): Default the keyboardLayout to en-US if not set (#512)
The recent fix to PasteModal will silently fail a paste if the keyboardLayout hasn't been selected in the settings yet, then when you look in Settings it looks like it's set to Belgian, but it's really just blank. Set it to default to en-US in both these places so it works like it did previously. Fixes #492
1 parent c1d771c commit 1b5062c

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

ui/src/components/popovers/PasteModal.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useRef, useState } from "react";
1+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
22
import { LuCornerDownLeft } from "react-icons/lu";
33
import { ExclamationCircleIcon } from "@heroicons/react/16/solid";
44
import { useClose } from "@headlessui/react";
@@ -39,6 +39,13 @@ export default function PasteModal() {
3939
state => state.setKeyboardLayout,
4040
);
4141

42+
// this ensures we always get the original en-US if it hasn't been set yet
43+
const safeKeyboardLayout = useMemo(() => {
44+
if (keyboardLayout && keyboardLayout.length > 0)
45+
return keyboardLayout;
46+
return "en-US";
47+
}, [keyboardLayout]);
48+
4249
useEffect(() => {
4350
send("getKeyboardLayout", {}, resp => {
4451
if ("error" in resp) return;
@@ -56,29 +63,28 @@ export default function PasteModal() {
5663
setPasteMode(false);
5764
setDisableVideoFocusTrap(false);
5865
if (rpcDataChannel?.readyState !== "open" || !TextAreaRef.current) return;
59-
if (!keyboardLayout) return;
60-
if (!chars[keyboardLayout]) return;
61-
66+
if (!safeKeyboardLayout) return;
67+
if (!chars[safeKeyboardLayout]) return;
6268
const text = TextAreaRef.current.value;
6369

6470
try {
6571
for (const char of text) {
66-
const { key, shift, altRight, deadKey, accentKey } = chars[keyboardLayout][char]
72+
const { key, shift, altRight, deadKey, accentKey } = chars[safeKeyboardLayout][char]
6773
if (!key) continue;
6874

69-
const keyz = [ keys[key] ];
70-
const modz = [ modifierCode(shift, altRight) ];
75+
const keyz = [ keys[key] ];
76+
const modz = [ modifierCode(shift, altRight) ];
7177

72-
if (deadKey) {
78+
if (deadKey) {
7379
keyz.push(keys["Space"]);
7480
modz.push(noModifier);
75-
}
76-
if (accentKey) {
81+
}
82+
if (accentKey) {
7783
keyz.unshift(keys[accentKey.key])
7884
modz.unshift(modifierCode(accentKey.shift, accentKey.altRight))
79-
}
85+
}
8086

81-
for (const [index, kei] of keyz.entries()) {
87+
for (const [index, kei] of keyz.entries()) {
8288
await new Promise<void>((resolve, reject) => {
8389
send(
8490
"keyboardReport",
@@ -92,13 +98,13 @@ export default function PasteModal() {
9298
},
9399
);
94100
});
95-
}
101+
}
96102
}
97103
} catch (error) {
98104
console.error(error);
99105
notifications.error("Failed to paste text");
100106
}
101-
}, [rpcDataChannel?.readyState, send, setDisableVideoFocusTrap, setPasteMode, keyboardLayout]);
107+
}, [rpcDataChannel?.readyState, send, setDisableVideoFocusTrap, setPasteMode, safeKeyboardLayout]);
102108

103109
useEffect(() => {
104110
if (TextAreaRef.current) {
@@ -148,7 +154,7 @@ export default function PasteModal() {
148154
// @ts-expect-error TS doesn't recognize Intl.Segmenter in some environments
149155
[...new Intl.Segmenter().segment(value)]
150156
.map(x => x.segment)
151-
.filter(char => !chars[keyboardLayout][char]),
157+
.filter(char => !chars[safeKeyboardLayout][char]),
152158
),
153159
];
154160

@@ -167,11 +173,11 @@ export default function PasteModal() {
167173
)}
168174
</div>
169175
</div>
170-
<div className="space-y-4">
176+
<div className="space-y-4">
171177
<p className="text-xs text-slate-600 dark:text-slate-400">
172-
Sending text using keyboard layout: {layouts[keyboardLayout]}
178+
Sending text using keyboard layout: {layouts[safeKeyboardLayout]}
173179
</p>
174-
</div>
180+
</div>
175181
</div>
176182
</div>
177183
</div>

ui/src/routes/devices.$id.settings.keyboard.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect } from "react";
1+
import { useCallback, useEffect, useMemo } from "react";
22

33
import { KeyboardLedSync, useSettingsStore } from "@/hooks/stores";
44
import { useJsonRpc } from "@/hooks/useJsonRpc";
@@ -20,6 +20,13 @@ export default function SettingsKeyboardRoute() {
2020
state => state.setKeyboardLedSync,
2121
);
2222

23+
// this ensures we always get the original en-US if it hasn't been set yet
24+
const safeKeyboardLayout = useMemo(() => {
25+
if (keyboardLayout && keyboardLayout.length > 0)
26+
return keyboardLayout;
27+
return "en-US";
28+
}, [keyboardLayout]);
29+
2330
const layoutOptions = Object.entries(layouts).map(([code, language]) => { return { value: code, label: language } })
2431
const ledSyncOptions = [
2532
{ value: "auto", label: "Automatic" },
@@ -69,7 +76,7 @@ export default function SettingsKeyboardRoute() {
6976
size="SM"
7077
label=""
7178
fullWidth
72-
value={keyboardLayout}
79+
value={safeKeyboardLayout}
7380
onChange={onKeyboardLayoutChange}
7481
options={layoutOptions}
7582
/>

0 commit comments

Comments
 (0)