import { createResource, createSignal, For, Show } from "solid-js"; import { api, type ShareInfo, type TokenInfo } from "./api"; import { ago, until } from "./live"; import { useSession } from "./session"; /** * The only place individual phones are visible. * * Positions belong to the account, so a token is a credential and nothing else. * Listing them with last-seen is what makes a forgotten phone in a drawer — the * one that drags the live marker back and forth — visible and revocable. */ export default function Settings() { const me = useSession().me; const [tokens, { refetch }] = createResource(() => api.tokens()); const [message, setMessage] = createSignal(null); const now = Math.floor(Date.now() / 1000); async function revoke(t: TokenInfo) { if (!confirm(`Revoke "${t.name}"? That phone has to log in again.`)) return; await api.revokeToken(t.token_id); void refetch(); } async function revokeOthers() { if (!confirm("Revoke every device token on this account?")) return; const { revoked } = await api.revokeOthers(); setMessage(`${revoked} token(s) revoked`); void refetch(); } return (

account

{me.display_name} ({me.username}){me.is_admin ? " · admin" : ""}

devices

{(m) =>

{m()}

}
no devices logged in

}> {(t) => (
{t.name} {t.platform}
{t.last_seen_at ? `seen ${ago(t.last_seen_at, now)} ago` : "never seen"} {t.last_src_ip ? ` · ${t.last_src_ip}` : ""} {t.last_transport ? ` · ${t.last_transport}` : ""} {t.os_api_level ? ` · API ${t.os_api_level}` : ""}
)}
); } const PRECISIONS = [ { m: 0, label: "exact" }, { m: 100, label: "100 m" }, { m: 500, label: "500 m" }, { m: 1000, label: "1 km" }, { m: 5000, label: "5 km" }, ]; const EXPIRIES = [ { s: null, label: "never" }, { s: 3600, label: "1 hour" }, { s: 8 * 3600, label: "8 hours" }, { s: 24 * 3600, label: "24 hours" }, { s: 7 * 86400, label: "7 days" }, ]; function precisionWords(m: number): string { if (m === 0) return "exact"; return m < 1000 ? `rounded to ${m} m` : `rounded to ${m / 1000} km`; } /** * Outgoing shares: who can see this account, and how much. * * The whole grant is on one row because the three settings only mean anything * together — "bob, live position only, rounded to 1 km" is the sentence a user * has to be able to check at a glance before trusting it. */ function Sharing() { const [shares, { refetch }] = createResource(() => api.shares()); const [username, setUsername] = createSignal(""); const [trailVisible, setTrailVisible] = createSignal(true); const [precision, setPrecision] = createSignal(0); const [expiresIn, setExpiresIn] = createSignal(null); const [error, setError] = createSignal(null); const now = Math.floor(Date.now() / 1000); async function create(e: Event) { e.preventDefault(); setError(null); try { await api.createShare({ username: username(), trail_visible: trailVisible(), precision_m: precision(), expires_in_s: expiresIn(), }); setUsername(""); setTrailVisible(true); setPrecision(0); setExpiresIn(null); void refetch(); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } } async function revoke(sh: ShareInfo) { if (!confirm(`Stop sharing with ${sh.viewer_display_name}?`)) return; await api.revokeShare(sh.id); void refetch(); } return (

sharing

you are not sharing with anyone

}> {(sh) => ( )}
); } function PasswordForm() { const [current, setCurrent] = createSignal(""); const [next, setNext] = createSignal(""); const [status, setStatus] = createSignal(null); async function submit(e: Event) { e.preventDefault(); try { await api.changePassword(current(), next()); // Changing the password revokes every device token, by design. setStatus("changed — every device must log in again"); setCurrent(""); setNext(""); } catch (e) { setStatus(e instanceof Error ? e.message : String(e)); } } return (
setCurrent(e.currentTarget.value)} /> setNext(e.currentTarget.value)} /> {(s) =>

{s()}

}
); }