import { render } from 'preact'; import { useEffect, useState } from 'preact/hooks'; import './tokens.css'; import { api, setToken, type KeyRow, type InviteView } from './api'; import { buildAdminToken, nip07 } from './nip98'; // ── login ───────────────────────────────────────────────────────────────────── function Login({ onDone }: { onDone: () => void }) { const [busy, setBusy] = useState(false); const [err, setErr] = useState(null); const signIn = async () => { setBusy(true); setErr(null); try { const signer = nip07(); if (!signer) { throw new Error( 'No NIP-07 signer found. Enable your Smirk wallet (or nos2x/Alby) on this origin, then reload.', ); } const c = await api.challenge(); const token = await buildAdminToken(c, signer); const { access_token } = await api.verify(token, c.challenge); setToken(access_token); onDone(); } catch (e) { setErr(e instanceof Error ? e.message : 'Sign-in failed'); } finally { setBusy(false); } }; return (
🐸

Smirk Operator Console

Sign in with an admin Nostr key on your instance's allowlist.

{err &&

{err}

}
); } // ── shared ──────────────────────────────────────────────────────────────────── function Section({ title, children }: { title: string; children: unknown }) { return (

{title}

{children}
); } function useAsync(fn: () => Promise, deps: unknown[] = []) { const [data, setData] = useState(null); const [err, setErr] = useState(null); const reload = () => { setErr(null); fn() .then(setData) .catch((e) => setErr(e instanceof Error ? e.message : String(e))); }; // eslint-disable-next-line react-hooks/exhaustive-deps useEffect(reload, deps); return { data, err, reload }; } // ── status ──────────────────────────────────────────────────────────────────── function StatusTab() { const { data, err } = useAsync(() => api.features()); return (
{err &&

{err}

}
{JSON.stringify(data, null, 2)}
); } // ── admin keys ──────────────────────────────────────────────────────────────── function KeysTab() { const { data, err, reload } = useAsync(() => api.keys()); const [pubkey, setPubkey] = useState(''); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(null); const add = async () => { setBusy(true); setMsg(null); try { await api.addKey(pubkey.trim().toLowerCase()); setPubkey(''); reload(); } catch (e) { setMsg(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); } }; const revoke = async (id: string) => { if (!confirm('Revoke this admin key?')) return; setMsg(null); try { await api.revokeKey(id); reload(); } catch (e) { setMsg(e instanceof Error ? e.message : String(e)); } }; return (
{err &&

{err}

} {(data ?? []).map((k: KeyRow) => ( ))}
{k.pubkey.slice(0, 16)}… {k.status} {k.status !== 'revoked' && ( )}
setPubkey((e.target as HTMLInputElement).value)} />
{msg &&

{msg}

}
); } // ── invites ─────────────────────────────────────────────────────────────────── function InvitesTab() { const { data, err, reload } = useAsync(() => api.invites()); const [count, setCount] = useState(5); const [label, setLabel] = useState(''); const [minted, setMinted] = useState(null); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(null); const mint = async () => { setBusy(true); setMsg(null); try { const r = await api.mintInvites(count, label.trim() || undefined); setMinted(r.codes); reload(); } catch (e) { setMsg(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); } }; return (
{err &&

{err}

}
setCount(Number((e.target as HTMLInputElement).value))} /> setLabel((e.target as HTMLInputElement).value)} />
{msg &&

{msg}

} {minted && (
New codes — copy now, they are shown once:
{minted.join('\n')}
)} {(data?.invites ?? []).map((i: InviteView) => ( ))}
{i.code_prefix}… {i.label ?? ''} {i.created_at.slice(0, 10)} {i.status}
); } // ── config ──────────────────────────────────────────────────────────────────── type Overlay = Record>; interface ConfigResp { effective: Overlay; overlay: Overlay; versions: Record; runtime_class: Record; restart_pending: boolean; } function ConfigTab() { const { data, err, reload } = useAsync(() => api.config() as Promise); const [edit, setEdit] = useState({}); const [msg, setMsg] = useState(null); useEffect(() => { if (!data) return; // Seed edit state for sections we are NOT already editing, and preserve // in-progress edits — a reload after saving one section must not discard // unsaved edits in the others. setEdit((prev) => { const next = { ...prev }; for (const section of Object.keys(data.effective)) { if (!(section in next)) next[section] = structuredClone(data.effective[section]); } return next; }); }, [data]); if (err) return
{

{err}

}
; if (!data) return
Loading…
; const setField = (section: string, field: string, value: unknown) => setEdit((e) => ({ ...e, [section]: { ...(e[section] ?? {}), [field]: value } })); const save = async (section: string) => { setMsg(null); // Send ONLY the fields that actually changed. Sending the whole section makes the // backend count every populated field as "touched" — spuriously flagging a restart // and pinning env-sourced values into the DB overlay. const orig = (data.effective[section] ?? {}) as Record; const cur = (edit[section] ?? {}) as Record; const patch: Record = {}; for (const k of Object.keys(cur)) { if (JSON.stringify(cur[k]) !== JSON.stringify(orig[k])) patch[k] = cur[k]; } if (Object.keys(patch).length === 0) { setMsg(`No changes in ${section}.`); return; } try { await api.putConfig({ [section]: patch }, { [section]: data.versions[section] ?? 0 }); setMsg(`Saved ${section}.`); reload(); } catch (e) { setMsg(e instanceof Error ? e.message : String(e)); } }; return (
{data.restart_pending && (
⚠ A saved change needs a restart to take effect (systemctl restart, or auto mode).
)} {msg &&

{msg}

} {Object.keys(edit) .sort() .map((section) => (
{Object.entries(edit[section] ?? {}).map(([field, value]) => { const cls = data.runtime_class[`${section}.${field}`] ?? 'restart-required'; const overridden = data.overlay[section]?.[field] !== undefined; return (
{typeof value === 'boolean' ? ( setField(section, field, (e.target as HTMLInputElement).checked)} /> ) : ( { const v = (e.target as HTMLInputElement).value; if (typeof value === 'number') { // Ignore empty / non-numeric input instead of coercing to 0. if (v === '') return; const n = Number(v); if (!Number.isNaN(n)) setField(section, field, n); } else { setField(section, field, v); } }} /> )}
); })}
))}
); } // ── shell ───────────────────────────────────────────────────────────────────── const TABS = ['Status', 'Keys', 'Invites', 'Config'] as const; type Tab = (typeof TABS)[number]; function Console({ onLogout }: { onLogout: () => void }) { const [tab, setTab] = useState('Status'); return (

🐸 Smirk Operator Console

{tab === 'Status' && } {tab === 'Keys' && } {tab === 'Invites' && } {tab === 'Config' && }
); } function App() { const [authed, setAuthed] = useState(false); return authed ? setAuthed(false)} /> : setAuthed(true)} />; } render(, document.getElementById('app')!);