const ICON = { phone: "📱", tablet: "▭", watch: "⌚", other: "◻" }; async function loadDevices() { const r = await api("/api/devices"); const ul = $("devs"); if (r.status === 401 || r.status === 503) return showGate(r); if (!r.ok) { ul.innerHTML = `
  • ${esc(r.body.error || "Could not load devices")}
  • `; return; } $("gate").hidden = true; $("content").hidden = false; if (!r.body.length) { ul.innerHTML = '
  • No devices yet. Add your first phone above.
  • '; return; } ul.innerHTML = r.body.map((d) => `
  • ${ICON[d.kind] || ""} ${esc(d.label)}${esc(d.kind)} · ${esc(d.platform)}${d.phone ? " · " + esc(d.phone) : ""} · added ${new Date(d.created_at).toLocaleDateString()}
  • `).join(""); ul.querySelectorAll("button[data-id]").forEach((b) => b.addEventListener("click", async () => { if (!confirm("Remove this device?")) return; const x = await api("/api/devices/" + encodeURIComponent(b.dataset.id), { method: "DELETE" }); if (x.ok) loadDevices(); else msg($("devMsg"), "err", x.body.error || "Could not remove device"); })); } function showGate(r) { $("content").hidden = true; $("gate").hidden = false; if (r.status === 503) { $("gate").innerHTML = '
    NOT AVAILABLE

    The admin passcode is not configured on the server, so devices (which hold phone numbers) are locked. Set ADMIN_PASSCODE as a Space secret.

    '; return; } signInForm($("gate"), loadDevices, "Devices hold phone numbers, so this page needs the admin passcode."); } $("devForm").addEventListener("submit", async (e) => { e.preventDefault(); const m = $("devMsg"); const label = $("label").value.trim(); if (!label) return msg(m, "err", "Give the device a name."); const body = { label, kind: $("kind").value, platform: $("platform").value }; const phone = $("phone").value.trim(); if (phone) body.phone = phone; const r = await api("/api/devices", { method: "POST", body }); if (r.status === 401) return showGate(r); if (r.ok) { msg(m, "ok", `Added ${r.body.label}${r.body.phone ? " (" + r.body.phone + ")" : ""} ✓`); $("devForm").reset(); loadDevices(); } else msg(m, "err", r.body.error || `Could not add device (${r.status})`); }); loadDevices();