File size: 2,372 Bytes
1444c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = `<li class="err">${esc(r.body.error || "Could not load devices")}</li>`; return; }
  $("gate").hidden = true; $("content").hidden = false;
  if (!r.body.length) { ul.innerHTML = '<li class="muted">No devices yet. Add your first phone above.</li>'; return; }
  ul.innerHTML = r.body.map((d) => `<li><div><strong>${ICON[d.kind] || ""} ${esc(d.label)}</strong><small>${esc(d.kind)} · ${esc(d.platform)}${d.phone ? " · " + esc(d.phone) : ""} · added ${new Date(d.created_at).toLocaleDateString()}</small></div><button class="btn danger" data-id="${esc(d.id)}" type="button">Remove</button></li>`).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 = '<div class="label">NOT AVAILABLE</div><p>The admin passcode is not configured on the server, so devices (which hold phone numbers) are locked. Set <code>ADMIN_PASSCODE</code> as a Space secret.</p>'; 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();