Remember the interval and the pinned rows across reloads (closes #212)
check / check (push) Successful in 4m26s

The page keeps the interval and the names of the pinned WAN hosts in
localStorage and starts with them on the next load. A browser with no
usable storage, or a saved value the page cannot use, starts at 3s with
datavi.be pinned. The interval menu's options are now built from one
list, so the saved interval can be marked selected.

Model: opus-5-5
This commit is contained in:
clawbot committed 2026-11-19 06:28:41 +00:00
1 parent dd119a0e46
commit 1e85a210f5
3 files changed
+175 -13

No files matched your search

+76 -11
View File
@@ -65,6 +65,12 @@ export const CONFIG = {
resizeDelay: 100,
};
// The interval menu's choices, in ms.
const INTERVAL_CHOICES = [1000, 2000, 3000, 5000, 10000, 15000, 30000, 60000];
// The WAN hosts pinned in a browser that has saved no pins.
const DEFAULT_PINNED = ["datavi.be"];
// WAN endpoints to monitor. These are used for the aggregate health/stats
// display (min/max/avg). Ordered: personal, LLM APIs, big-3 cloud, then
// CDN/hosting/other. A check follows a redirect and times the request to
@@ -350,9 +356,10 @@ export class HostState {
}
export class AppState {
constructor(localHosts) {
// pinnedNames: the names of the WAN hosts to pin.
constructor(localHosts, pinnedNames = DEFAULT_PINNED) {
this.wan = WAN_HOSTS.map(
(h) => new HostState(h, h.name === "datavi.be"),
(h) => new HostState(h, pinnedNames.includes(h.name)),
);
this.local = localHosts.map((h) => new HostState(h));
this.paused = false;
@@ -422,6 +429,60 @@ export class AppState {
}
}
// --- Saved Settings ----------------------------------------------------------
// The interval and the names of the pinned WAN hosts are kept in
// localStorage, so each browser starts with the ones it last had. A browser
// with no usable storage, or a saved value the page cannot use, starts with
// the defaults.
const INTERVAL_KEY = "netwatch-interval";
const PINNED_KEY = "netwatch-pinned";
// The saved interval in ms, or CONFIG.updateInterval if none is saved.
export function savedInterval() {
try {
const ms = Number(localStorage.getItem(INTERVAL_KEY));
if (INTERVAL_CHOICES.includes(ms)) return ms;
} catch {
// No usable storage.
}
return CONFIG.updateInterval;
}
// The saved names of the pinned WAN hosts, or DEFAULT_PINNED if none are
// saved. A saved name no WAN host has pins nothing.
export function savedPinnedNames() {
try {
const names = JSON.parse(localStorage.getItem(PINNED_KEY));
if (
Array.isArray(names) &&
names.every((name) => typeof name === "string")
)
return names;
} catch {
// No usable storage, or a saved value that is not JSON.
}
return DEFAULT_PINNED;
}
export function saveInterval(ms) {
saveSetting(INTERVAL_KEY, String(ms));
}
export function savePinnedNames(state) {
const names = state.wan.filter((h) => h.pinned).map((h) => h.name);
saveSetting(PINNED_KEY, JSON.stringify(names));
}
// A setting the browser cannot store lasts until the page is reloaded.
function saveSetting(key, value) {
try {
localStorage.setItem(key, value);
} catch {
// No usable storage.
}
}
// --- Reporting ---------------------------------------------------------------
// A random UUIDv4. `crypto.randomUUID` exists only in secure contexts
@@ -848,6 +909,14 @@ function wanRowsHTML(wanHosts) {
return rows.join("");
}
// The interval menu's options, with CONFIG.updateInterval selected.
function intervalOptionsHTML() {
return INTERVAL_CHOICES.map((ms) => {
const selected = ms === CONFIG.updateInterval ? " selected" : "";
return `<option value="${ms}"${selected}>${ms / 1000}s</option>`;
}).join("\n");
}
export function buildUI(state) {
const app = document.getElementById("app");
app.innerHTML = `
@@ -869,14 +938,7 @@ export function buildUI(state) {
<div class="flex items-center gap-2">
<label class="text-gray-400 text-sm">Interval:</label>
<select id="interval-select" class="bg-gray-800 border border-gray-600 text-white text-sm rounded-lg px-2 py-1 focus:outline-none focus:ring-2 focus:ring-blue-500 cursor-pointer">
<option value="1000">1s</option>
<option value="2000">2s</option>
<option value="3000" selected>3s</option>
<option value="5000">5s</option>
<option value="10000">10s</option>
<option value="15000">15s</option>
<option value="30000">30s</option>
<option value="60000">60s</option>
${intervalOptionsHTML()}
</select>
</div>
</div>
@@ -1520,7 +1582,8 @@ async function init() {
log.info(`Gateway detected: ${gateway.url}`);
}
const state = new AppState(localHosts);
CONFIG.updateInterval = savedInterval();
const state = new AppState(localHosts, savedPinnedNames());
buildUI(state);
log.info("UI built, starting tick loop");
@@ -1560,6 +1623,7 @@ async function init() {
if (!host) return;
host.pinned = !host.pinned;
log.notice(`${host.pinned ? "Pinned" : "Unpinned"} ${host.name}`);
savePinnedNames(state);
sortAndRebuildWAN(state);
});
@@ -1592,6 +1656,7 @@ async function init() {
.addEventListener("change", (e) => {
const newInterval = parseInt(e.target.value, 10);
CONFIG.updateInterval = newInterval;
saveInterval(newInterval);
log.notice(
`Interval changed to ${humanDuration(newInterval / 1000)}, history reset`,
);