refactor: extract typed frontend core utilities

This commit is contained in:
sjg
2026-08-01 13:48:51 +02:00
parent 50e07e2715
commit e910e644d5
4 changed files with 67 additions and 47 deletions
@@ -9,6 +9,8 @@ import {
latLonToMaidenhead,
locatorToLatLon,
} from "./core/geo.js";
import { escapeHtml as escapeMapHtml } from "./core/dom.js";
import { loadSetting, saveSetting } from "./core/settings.js";
// --- Decoder registry (fetched from /decoders on load) ---
/** @type {Array<{id:string,label:string,activation:string,active_modes:string[],background_decode:boolean,bookmark_selectable:boolean}>} */
@@ -75,25 +77,6 @@ function hideUnsupportedDecoderTabs() {
});
}
// --- Persistent settings (localStorage) ---
const STORAGE_PREFIX = "trx_";
function saveSetting(key, value) {
try { localStorage.setItem(STORAGE_PREFIX + key, JSON.stringify(value)); } catch(e) {}
}
function loadSetting(key, fallback) {
try {
const v = localStorage.getItem(STORAGE_PREFIX + key);
return v !== null ? JSON.parse(v) : fallback;
} catch(e) { return fallback; }
}
function escapeMapHtml(input) {
return String(input)
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll("\"", "&quot;");
}
// --- Authentication ---
let authRole = null; // null (not authenticated), "rx" (read-only), or "control" (full access)
let authEnabled = true;
@@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
//
// SPDX-License-Identifier: GPL-2.0-or-later
export function escapeHtml(input: unknown): string {
return String(input)
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll("\"", "&quot;");
}
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
//
// SPDX-License-Identifier: GPL-2.0-or-later
const STORAGE_PREFIX = "trx_";
export function saveSetting(key: string, value: unknown): void {
try {
localStorage.setItem(`${STORAGE_PREFIX}${key}`, JSON.stringify(value));
} catch {
// Storage can be unavailable in private browsing or under a strict policy.
}
}
export function loadSetting<T>(key: string, fallback: T): T {
try {
const value = localStorage.getItem(`${STORAGE_PREFIX}${key}`);
return value === null ? fallback : JSON.parse(value) as T;
} catch {
return fallback;
}
}