Repository navigation
Expand file tree
/
Copy pathConfigForm.tsx
More file actions
263 lines (244 loc) · 9.19 KB
/
Copy pathConfigForm.tsx
File metadata and controls
263 lines (244 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import type { TargetedSubmitEvent } from "preact";
import { token } from "../api/client";
import type { LoadingMode, Settings } from "../api/types";
import { ApplyBadge } from "../components/ApplyBadge";
import { Button } from "../components/Button";
import { ConfigSection } from "../components/ConfigSection";
import { HostnameField } from "../components/HostnameField";
import { clamp } from "../lib/math";
import { useRobot } from "../robot";
import { useStatus } from "../status/status";
import { useResettableState } from "../useResettableState";
import { AccessTokenField } from "./AccessTokenField";
import { useAccessToken } from "./useAccessToken";
import { useWifiNetworks } from "./useWifiNetworks";
import { WifiNetworksSection } from "./WifiNetworksSection";
const EYES_STYLES = ["classic", "kaomoji", "cover", "dots"];
// An emptied number field reads NaN; the stock volume stands in until the user types again.
const clampVolume = (value: number) => (Number.isNaN(value) ? 70 : clamp(value, 0, 100));
function draftFrom(settings: Settings) {
return {
hostname: settings.hostname,
sleepTimeout: String(settings.sleep_timeout),
continuousTimeout: String(settings.continuous_timeout),
volume: settings.volume,
welcome: settings.welcome,
serialLog: settings.serial_log,
loading: settings.loading,
eyesStyle: settings.eyes_style,
};
}
// Hostname and loading screen are read at boot, so a change to either waits for a reboot.
function saveMessage(previous: Settings, saved: Settings) {
const hostChanged = saved.hostname !== previous.hostname;
const loadingChanged = saved.loading !== previous.loading;
if (hostChanged && loadingChanged) {
return "Saved. Hostname and loading screen apply after reboot.";
}
if (hostChanged) {
return "Saved. Hostname applies after reboot.";
}
if (loadingChanged) {
return "Saved. Loading screen applies after reboot.";
}
return "Settings saved.";
}
export function ConfigForm({ settings }: { settings: Settings }) {
const { applySettings } = useRobot();
const { perform, setStatus } = useStatus();
const [draft, setDraft] = useResettableState(settings, draftFrom);
const accessToken = useAccessToken(settings);
const networks = useWifiNetworks(settings.wifi_networks, applySettings);
const set = (fields: Partial<typeof draft>) =>
setDraft((previous) => ({ ...previous, ...fields }));
async function save(event: TargetedSubmitEvent<HTMLFormElement>) {
event.preventDefault();
const newToken = accessToken.pendingValue;
await perform<Settings>({
path: "/settings",
request: {
method: "POST",
json: {
sleep_timeout: Number(draft.sleepTimeout),
hostname: draft.hostname,
volume: draft.volume,
welcome: draft.welcome,
serial_log: draft.serialLog,
continuous_timeout: Number(draft.continuousTimeout),
loading: draft.loading,
eyes_style: draft.eyesStyle,
...(newToken === undefined ? {} : { access_token: newToken }),
},
},
pending: "Saving…",
failure: "Save failed",
onSuccess: async (saved) => {
applySettings(saved);
if (newToken !== undefined) {
token.set(newToken);
}
const wifiChanged = networks.pending;
const wifiError = await networks.save();
if (wifiError) {
setStatus(`Settings saved. WiFi networks not saved: ${wifiError}`, "err");
return;
}
const wifiNote = wifiChanged ? " WiFi networks apply after reboot." : "";
setStatus(saveMessage(settings, saved) + wifiNote, "ok");
},
});
}
return (
<form onSubmit={save}>
<ConfigSection title="Network" apply="reboot">
<HostnameField
id="config-hostname"
value={draft.hostname}
onInput={(hostname) => set({ hostname })}
/>
</ConfigSection>
<WifiNetworksSection networks={networks} />
<ConfigSection title="Timeouts" apply="now">
<div className="form-group">
<label htmlFor="config-sleep">Sleep timeout (minutes)</label>
<input
type="number"
id="config-sleep"
min="1"
max="1440"
step="1"
required
value={draft.sleepTimeout}
onInput={(event) => set({ sleepTimeout: event.currentTarget.value })}
/>
<p className="hint">
Minutes in animation <code>none</code> before OLED blanks
</p>
</div>
<div className="form-group">
<label htmlFor="config-continuous">Continuous anim timeout (minutes)</label>
<input
type="number"
id="config-continuous"
min="1"
max="1440"
step="1"
required
value={draft.continuousTimeout}
onInput={(event) => set({ continuousTimeout: event.currentTarget.value })}
/>
<p className="hint">Max time for typing / reading / thinking / talking</p>
</div>
</ConfigSection>
<ConfigSection title="Audio" apply="now">
<div className="form-group">
<label htmlFor="config-volume">
Volume <span>{`${draft.volume}%`}</span>
</label>
<div className="range-row">
<input
type="range"
aria-label="Volume slider"
min="0"
max="100"
value={draft.volume}
onInput={(event) => set({ volume: clampVolume(event.currentTarget.valueAsNumber) })}
/>
<input
type="number"
id="config-volume"
min="0"
max="100"
step="1"
required
value={draft.volume}
onInput={(event) => set({ volume: clampVolume(event.currentTarget.valueAsNumber) })}
/>
</div>
</div>
</ConfigSection>
<ConfigSection title="Display" head>
<div className="form-group">
<div className="field-head">
<label htmlFor="config-eyes-style">Eyes style</label>
<ApplyBadge when="now" />
</div>
<select
id="config-eyes-style"
value={draft.eyesStyle}
onChange={(event) => set({ eyesStyle: event.currentTarget.value })}
>
{EYES_STYLES.map((style) => (
<option key={style} value={style}>
{style[0].toUpperCase() + style.slice(1)}
</option>
))}
</select>
<p className="hint">
Classic is the default procedural eyes. Kaomoji maps each animation to a face from the
expression library. Cover fills each half of the screen for a mask with eye holes;
animations change height with black margins. Dots draws a 12x12 circle per eye; size and
position follow the same poses as classic. Add new styles in firmware under{" "}
<code>display/eyes/styles/</code>.
</p>
</div>
</ConfigSection>
<ConfigSection title="Boot behavior">
<p className="config-section-desc">Loading screen changes take effect on next boot.</p>
<div className="form-group">
<div className="field-head">
<label htmlFor="config-loading">Boot loading screen</label>
<ApplyBadge when="reboot" />
</div>
<select
id="config-loading"
value={draft.loading}
onChange={(event) => set({ loading: event.currentTarget.value as LoadingMode })}
>
<option value="progress">Progress bar</option>
<option value="sleep_inertia">Sleep inertia (no IP display)</option>
</select>
<p className="hint" hidden={draft.loading !== "sleep_inertia" || draft.welcome}>
Head motion during boot needs Welcome enabled.
</p>
</div>
<div className="form-group">
<div className="field-head">
<span>Welcome on boot</span>
<ApplyBadge when="now" />
</div>
<div className="toggle-row">
<input
type="checkbox"
id="config-welcome"
checked={draft.welcome}
onChange={(event) => set({ welcome: event.currentTarget.checked })}
/>
<label htmlFor="config-welcome">Play welcome when Wi-Fi connects</label>
</div>
</div>
<div className="form-group">
<div className="field-head">
<span>Serial logging</span>
<ApplyBadge when="now" />
</div>
<div className="toggle-row">
<input
type="checkbox"
id="config-serial-log"
checked={draft.serialLog}
onChange={(event) => set({ serialLog: event.currentTarget.checked })}
/>
<label htmlFor="config-serial-log">Log to USB serial</label>
</div>
</div>
</ConfigSection>
<ConfigSection title="Security">
<AccessTokenField {...accessToken.field} />
</ConfigSection>
<Button type="submit" variant="primary">
Save settings
</Button>
</form>
);
}