import { OFFLINE_DATA_ICON } from "/js/icons.js"; import { notifySidePanelOpen, onlyOneSidePanelOpen } from "/js/panels.js"; import { isLoggedIn } from "/js/auth.js"; import { escapeHtml } from "/js/gpx.js"; const POLL_INTERVAL_MS = 3000; function formatBytes(bytes) { const mb = bytes / (1024 * 1024); return mb >= 1024 ? `${(mb / 1024).toFixed(1)} GB` : `${mb.toFixed(1)} MB`; } function formatElapsed(ms) { const totalSeconds = Math.max(0, Math.floor(ms / 1000)); const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; return minutes > 0 ? `${minutes}m ${seconds}s` : `${seconds}s`; } // "Building…" alone gave no way to tell a slow build from a stuck one — // elapsed time answers "is it alive" regardless of how long a given // selection takes. function formatRunning(status) { const elapsed = status.started_at ? ` (${formatElapsed(Date.now() - new Date(status.started_at).getTime())})` : ""; return `Building…${elapsed}`; } // The checklist of selectable countries (GET /api/offline-regions) plus // the "Apply changes" button that saves the ticked set (POST, same URL) // and hands it off — doesn't trigger a build itself, OfflineDataControl // does that across both files once the selection is saved. class RegionPicker { constructor(onApply) { this._onApply = onApply; } async mount(container) { const section = document.createElement("div"); section.className = "offline-data-panel__regions"; section.innerHTML = `
Regions
Checking…
`; this._statusEl = row.querySelector(".offline-data-panel__status"); container.appendChild(row); this._refreshStatus(); } async _refreshStatus() { let data; try { data = await (await fetch(this._statusUrl)).json(); } catch { this._statusEl.textContent = "Could not check status."; return; } this._statusEl.textContent = data.available ? `${formatBytes(data.size)} · updated ${new Date(data.mtime).toLocaleString([], { dateStyle: "short", timeStyle: "short" })}` : "Not generated yet."; } // Called by OfflineDataControl once RegionPicker's Apply has saved a // new selection — not tied to a button of this row's own. async triggerBuild() { this._statusEl.textContent = "Starting…"; try { const response = await fetch(this._buildUrl, { method: "POST" }); const data = await response.json().catch(() => ({})); if (!response.ok) throw new Error(data.error || `build failed: ${response.status}`); } catch (err) { this._statusEl.textContent = err.message; return; } this._statusEl.textContent = "Building…"; this._poll(); } // Self-rescheduling setTimeout loop, same shape as livetrack.js's // _scheduleRefresh — check status, only reschedule while still running. _poll() { setTimeout(async () => { let status; try { status = await (await fetch(this._buildStatusUrl)).json(); } catch { this._poll(); // transient failure — try again next tick return; } if (status.state === "running") { this._statusEl.textContent = formatRunning(status); this._poll(); return; } if (status.state === "error") { this._statusEl.textContent = `Build failed: ${status.message}`; } else { this._refreshStatus(); } }, POLL_INTERVAL_MS); } } // A sidebar panel wrapping the manual go-pmtiles workflow // docs/development.adoc documents — a region picker (which countries // should be available offline) plus, on "Apply changes", a rebuild of // offline.pmtiles/terrain.pmtiles covering the whole current selection // (see lib/maptool/pmtiles_builder.rb — there's no standalone per-country // file to individually delete; "remove a country" and "refresh a // country" are both just "change the selection, then Apply"). export class OfflineDataControl { constructor() { this._rows = [ new DataFileRow({ label: "Offline map data", statusUrl: "/api/offline-tiles", buildUrl: "/api/offline-tiles/build", buildStatusUrl: "/api/offline-tiles/build-status", }), new DataFileRow({ label: "Offline terrain data", statusUrl: "/api/terrain-tiles", buildUrl: "/api/terrain-tiles/build", buildStatusUrl: "/api/terrain-tiles/build-status", }), ]; this._regionPicker = new RegionPicker(() => this._rows.forEach((row) => row.triggerBuild())); } mount(sidebar) { this._container = document.createElement("div"); this._container.className = "sidebar-item"; this._container.innerHTML = ``; this._icon = this._container.querySelector(".sidebar-icon"); this._icon.addEventListener("click", () => this._togglePanel()); this._panel = document.getElementById("offline-data-panel"); this._panel.querySelector(".basemap-panel__close").addEventListener("click", () => this._togglePanel(false)); const body = this._panel.querySelector(".offline-data-panel__body"); this._regionPicker.mount(body); this._rows.forEach((row) => row.mount(body)); onlyOneSidePanelOpen(this._panel, () => this._icon.classList.remove("active")); sidebar.appendChild(this._container); } _togglePanel(force) { const open = force === undefined ? this._panel.hidden : force; this._panel.hidden = !open; this._icon.classList.toggle("active", open); if (open) notifySidePanelOpen(this._panel); } }