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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
import {
html, useState, useEffect, useCallback,
} from './vendor/htm-preact.js';
import { t, getLocale, setLocale, LOCALES } from './i18n.js';
import * as downloads from './downloads.js';
import * as platform from './platform.js';
import { hubFetch } from './hub-client.js';
import { APPS } from './apps.js';
import {
PAGE_SIZE_PREF, PAGE_SIZE_DEFAULT, PAGE_SIZE_STEP, PAGE_SIZE_MAX, pageSizeFrom,
} from './pager.js';
const PAGE_SIZES = Array.from(
{ length: PAGE_SIZE_MAX / PAGE_SIZE_STEP }, (_, i) => (i + 1) * PAGE_SIZE_STEP);
export function SettingsPage({ user, theme, onThemeChange, groups, onPrefsChange }) {
const [locale, setLoc] = useState(getLocale);
const [muted, setMuted] = useState(
() => Object.fromEntries((groups || []).map(g => [g.id, !!g.muted])));
const [globalMute, setGlobalMute] = useState(false);
const [defaultTab, setDefaultTab] = useState('chat');
// Off by default (docs/MESHBAY_DESIGN.md §9.8): the ordinary expectation, matching
// Spotify/Deezer, is that the phone locks on its own idle timer while
// listening. This is for whoever would rather trade battery for it —
// e.g. to ride out the WebRTC screen-lock reconnect gap without waiting
// on the automatic recovery at all.
const [keepScreenOnAudio, setKeepScreenOnAudio] = useState(false);
const [pageSize, setPageSize] = useState(PAGE_SIZE_DEFAULT);
const onLocaleChange = useCallback((e) => {
const code = e.target.value;
setLocale(code);
setLoc(code);
window.location.reload();
}, []);
const onThemeSelect = useCallback((e) => {
onThemeChange(e.target.value);
}, [onThemeChange]);
useEffect(() => {
hubFetch('/v1/users/me/preferences', { token: user.token })
.then(prefs => {
if (prefs.notifications_disabled === 'true') setGlobalMute(true);
if (prefs.default_tab) setDefaultTab(prefs.default_tab);
if (prefs.music_keep_screen_on === 'true') setKeepScreenOnAudio(true);
setPageSize(pageSizeFrom(prefs));
})
.catch(() => {});
}, [user.token]);
const toggleKeepScreenOnAudio = useCallback(async () => {
const next = !keepScreenOnAudio;
setKeepScreenOnAudio(next);
try {
await hubFetch('/v1/users/me/preferences/music_keep_screen_on', {
method: 'PUT', token: user.token,
body: { value: next ? 'true' : 'false' },
});
// A string, matching what a fresh page load reads from the hub
// (prefs.music_keep_screen_on === 'true' above) — music-player.js
// compares against that same string, and userPrefs is one shared bag
// fed from both this immediate update and that load.
if (onPrefsChange) onPrefsChange({ music_keep_screen_on: next ? 'true' : 'false' });
} catch (err) {
setKeepScreenOnAudio(!next);
}
}, [keepScreenOnAudio, user.token, onPrefsChange]);
const toggleGlobalMute = useCallback(async () => {
const next = !globalMute;
setGlobalMute(next);
try {
await hubFetch('/v1/users/me/preferences/notifications_disabled', {
method: 'PUT', token: user.token,
body: { value: next ? 'true' : 'false' },
});
if (onPrefsChange) onPrefsChange({ notifications_disabled: next });
} catch (err) {
setGlobalMute(!next);
}
}, [globalMute, user.token, onPrefsChange]);
const toggleMute = useCallback(async (gid) => {
const next = !muted[gid];
setMuted(prev => ({ ...prev, [gid]: next }));
try {
await hubFetch(`/v1/groups/${gid}/mute`, {
method: 'POST', token: user.token, body: { muted: next },
});
} catch (err) {
setMuted(prev => ({ ...prev, [gid]: !next }));
}
}, [muted, user.token]);
const changeDefaultTab = useCallback(async (e) => {
const val = e.target.value;
setDefaultTab(val);
try {
await hubFetch('/v1/users/me/preferences/default_tab', {
method: 'PUT', token: user.token,
body: { value: val },
});
if (onPrefsChange) onPrefsChange({ default_tab: val });
} catch { setDefaultTab(defaultTab); }
}, [defaultTab, user.token, onPrefsChange]);
const changePageSize = useCallback(async (e) => {
const val = String(e.target.value);
setPageSize(Number(val));
try {
await hubFetch(`/v1/users/me/preferences/${PAGE_SIZE_PREF}`, {
method: 'PUT', token: user.token,
body: { value: val },
});
if (onPrefsChange) onPrefsChange({ [PAGE_SIZE_PREF]: val });
} catch { setPageSize(pageSize); }
}, [pageSize, user.token, onPrefsChange]);
const [dlMode, setDlMode] = useState(() => downloads.getMode());
const [dlDir, setDlDir] = useState(null);
const [dlError, setDlError] = useState('');
// Read from the hub rather than written here: the two constants that used to
// sit in this markup said 0.1.0 and MNP 0.1 long after both had moved on.
const [hubInfo, setHubInfo] = useState(null);
// On a desktop build, whether the OS is really holding the keys. Electron's
// safeStorage falls back to a fixed key when no keyring is running — a
// headless session, a minimal desktop — and does it silently. Somebody who
// believes the OS is protecting their keys deserves to be told when it is not.
const [keyBackend, setKeyBackend] = useState('');
// Changing the hub after the first run. Without this a typo on the first
// screen was permanent: the prompt only appears when no hub is set, so a
// wrong one left editing a JSON file by hand as the only way out.
const [hubInput, setHubInput] = useState('');
const [hubError, setHubError] = useState('');
useEffect(() => {
if (!platform.secrets.available) return;
platform.secrets.backend().then(setKeyBackend).catch(() => {});
}, []);
useEffect(() => {
hubFetch('/v1/hub/version').then(setHubInfo).catch(() => {});
}, []);
useEffect(() => {
// The desktop build remembers a path; the browser remembers a handle. Both
// answer "where do downloads go", and the row below renders either.
if (platform.folder.available) platform.folder.get().then(setDlDir);
else downloads.savedDirectory().then(setDlDir);
}, []);
const pickFolder = useCallback(async () => {
try {
if (platform.folder.available) {
const dir = await platform.folder.choose();
if (dir) setDlDir(dir);
return;
}
const handle = await downloads.chooseDirectory();
setDlDir(handle);
} catch (err) {
// Reported where the folder controls are. This used to be written into
// the node-key status, two sections away, where nobody was looking.
if (err.name !== 'AbortError') setDlError(err.message);
}
}, []);
return html`
<div>
<h2>${t('settings.title')}</h2>
<div class="settings-section">
<h3 class="settings-heading">${t('settings.downloads')}</h3>
${(downloads.SUPPORTED || platform.folder.available) && html`
<label class="settings-choice">
<input type="radio" name="dlmode" checked=${dlMode === 'auto'}
onChange=${() => { downloads.setMode('auto'); setDlMode('auto'); }} />
<span>
<strong>${t('settings.dl_auto')}</strong>
<span class="settings-hint">${t('settings.dl_auto_hint')}</span>
</span>
</label>
<label class="settings-choice">
<input type="radio" name="dlmode" checked=${dlMode === 'ask'}
onChange=${() => { downloads.setMode('ask'); setDlMode('ask'); }} />
<span>
<strong>${t('settings.dl_ask')}</strong>
<span class="settings-hint">${t('settings.dl_ask_hint')}</span>
</span>
</label>
<div class="settings-row" style="margin-top:10px">
<span class="settings-label">
${dlDir ? t('settings.dl_folder',
{ name: dlDir.name || String(dlDir) })
: t('settings.dl_no_folder')}
</span>
<span>
<button class="admin-btn" onClick=${pickFolder}>
${dlDir ? t('settings.dl_change') : t('settings.dl_choose')}
</button>
${dlDir && !dlDir.isDefault && html`
<button class="btn-secondary" onClick=${async () => {
if (platform.folder.available) await platform.folder.forget();
else await downloads.forgetDirectory();
setDlDir(null);
}}>${t('settings.dl_forget')}</button>
`}
</span>
</div>
${dlError && html`<p class="error-msg">${dlError}</p>`}
`}
</div>
<div class="settings-section">
<h3 class="settings-heading">${t('settings.appearance')}</h3>
<div class="settings-row">
<span class="settings-label">${t('settings.theme')}</span>
<select class="settings-select" value=${theme} onChange=${onThemeSelect}>
<option value="light">${t('settings.theme_light')}</option>
<option value="dark">${t('settings.theme_dark')}</option>
<option value="system">${t('settings.theme_system')}</option>
</select>
</div>
<div class="settings-row">
<span class="settings-label">${t('settings.language')}</span>
<select class="settings-select" value=${locale} onChange=${onLocaleChange}>
${LOCALES.map(l => html`
<option key=${l.code} value=${l.code}>${l.name}</option>
`)}
</select>
</div>
</div>
<div class="settings-section">
<h3 class="settings-heading">${t('settings.groups')}</h3>
<div class="settings-row">
<span class="settings-label">${t('settings.notif_global_disable')}</span>
<label class="toggle-switch">
<input type="checkbox" checked=${globalMute}
onChange=${toggleGlobalMute} />
<span class="toggle-switch-track"><span class="toggle-switch-thumb"></span></span>
</label>
</div>
${!globalMute && html`
<p class="settings-hint" style="margin-bottom:8px">${t('settings.notif_global_hint')}</p>
${groups.map(g => html`
<div class="settings-row" key=${g.id}>
<span class="settings-label">${g.name}</span>
<label class="toggle-switch">
<input type="checkbox" checked=${!muted[g.id]}
onChange=${() => toggleMute(g.id)} />
<span class="toggle-switch-track"><span class="toggle-switch-thumb"></span></span>
</label>
</div>
`)}
`}
</div>
<div class="settings-section">
<h3 class="settings-heading">${t('settings.defaults')}</h3>
<div class="settings-row">
<span class="settings-label">${t('settings.default_tab')}</span>
<select class="settings-select" value=${defaultTab}
onChange=${changeDefaultTab}>
${APPS.map((a) => html`<option key=${a.key} value=${a.key}>${t(a.labelKey)}</option>`)}
<option value="settings">${t('group.tab_settings')}</option>
</select>
</div>
<p class="settings-hint">${t('settings.default_tab_hint')}</p>
<div class="settings-row">
<span class="settings-label">${t('settings.media_page_size')}</span>
<select class="settings-select" value=${pageSize}
onChange=${changePageSize}>
${PAGE_SIZES.map((n) => html`<option key=${n} value=${n}>${n}</option>`)}
</select>
</div>
<p class="settings-hint">${t('settings.media_page_size_hint')}</p>
<div class="settings-row">
<span class="settings-label">${t('settings.music_keep_screen_on')}</span>
<label class="toggle-switch">
<input type="checkbox" checked=${keepScreenOnAudio}
onChange=${toggleKeepScreenOnAudio} />
<span class="toggle-switch-track"><span class="toggle-switch-thumb"></span></span>
</label>
</div>
<p class="settings-hint">${t('settings.music_keep_screen_on_hint')}</p>
</div>
${platform.isNative && html`
<div class="settings-section">
<h3 class="settings-heading">${t('settings.hub_heading')}</h3>
<div class="settings-row">
<span class="settings-label">${t('settings.hub_current')}</span>
<span class="settings-value">${platform.hubBase() || '—'}</span>
</div>
<p class="settings-hint">${t('settings.hub_hint')}</p>
<form onSubmit=${async (e) => {
e.preventDefault();
setHubError('');
try {
await window.meshbay.setHubBase(hubInput.trim());
} catch (err) { setHubError(platform.bridgeMessage(err)); }
}} style="display:flex;gap:8px">
<input type="text" placeholder=${platform.hubBase()}
value=${hubInput} onInput=${e => setHubInput(e.target.value)} />
<button class="admin-btn" type="submit">${t('settings.hub_change')}</button>
</form>
${hubError && html`<p class="error-msg">${hubError}</p>`}
</div>
`}
${keyBackend && html`
<div class="settings-section">
<h3 class="settings-heading">${t('settings.keys_heading')}</h3>
<div class="settings-row">
<span class="settings-label">${t('settings.keys_where')}</span>
<span class="settings-value">${keyBackend}</span>
</div>
${keyBackend === 'unprotected_fallback' && html`
<p class="error-msg">${t('settings.keys_unprotected')}</p>
`}
${keyBackend === 'unavailable' && html`
<p class="error-msg">${t('settings.keys_unavailable')}</p>
`}
</div>
`}
<div class="settings-section">
<h3 class="settings-heading">${t('settings.about')}</h3>
<div class="settings-row">
<span class="settings-label">${t('settings.version')}</span>
<span class="settings-value">${hubInfo ? hubInfo.hub : '—'}</span>
</div>
<div class="settings-row">
<span class="settings-label">${t('settings.protocol')}</span>
<span class="settings-value">
${hubInfo ? `MNP ${hubInfo.mnp} / MHP ${hubInfo.mhp}` : '—'}
</span>
</div>
</div>
</div>
`;
}
|