feat(devices): pair-a-device card replaces the pending registration list

The Devices page's Pending Device Registrations section (Approve/Reject
cards broadcast to every account) is replaced by an always-visible Pair
a Device field: type the 6-digit code shown on the registering e-reader,
a templ-rendered confirm card shows what will be bound, and confirming
claims the device to the signed-in account.

Rendering ownership (AGENTS.md): the confirm card and every lookup
outcome are templ partials (PairingLookupResult) swapped by htmx from a
new SSR route POST /devices/pairing/lookup — the server decides what UI
looks like, no JSON-built HTML. Errors render as content because htmx
only swaps 2xx bodies (the route is web-only; the JSON API keeps real
status codes), and one delegated htmx:responseError listener in
device-management.ts toasts the non-swap failure path.

The claim confirm button posts straight to /api/devices/claim; success
comes back as HX-Refresh (same pattern as token regeneration), so the
page reloads into the paired state. No new TS module is needed — plain
htmx attributes drive the whole flow.

Ride-alongs: the Add New Device modal loses the device-type select and
the kobo manual form (its post-register UX depended on the broadcast
list and silently produced an unclaimable registration; native Kobo
sync is still server-side-only and will get its own pairing UX when it
lands) — the modal is now a single Connect-a-KOReader-Device guide with
the pairing steps. PendingRegistrationData, convertPending, and the
pending plumbing in the page handler are gone with the section.
ui-controls.md glossary and tests/ui COVERAGE.md updated in the same
commit as the control rename, per the drift gates.
This commit is contained in:
John O'Keefe committed 2026-11-02 16:11:15 -05:00
1 parent 8fbacd9f46
commit fef79b1836
9 files changed
+253 -313

No files matched your search

+76 -75
View File
@@ -2,7 +2,7 @@ package templates
import "bookhoard/internal/handlers"
templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []PendingRegistrationData, errorMessage string, baseURL string) {
templ Devices(user User, devices []handlers.DeviceInfo, errorMessage string, baseURL string) {
<!DOCTYPE html>
<html lang="en">
<head>
@@ -157,56 +157,43 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
</div>
}
</div>
if len(pendingRegistrations) > 0 {
<div id="pending-section" class="mt-12">
<h2 class="text-lg font-bold tracking-tight mb-4" style="color: var(--text-primary)">Pending Device Registrations</h2>
<div class="space-y-4">
for _, reg := range pendingRegistrations {
<div class="card p-4 flex items-center justify-between gap-4 flex-wrap">
<div>
<h3 class="font-semibold" style="color: var(--text-primary)">{ reg.DeviceName }</h3>
<p class="text-sm" style="color: var(--text-secondary)">
{ reg.DeviceType } - Expires in { reg.ExpiresAt }
</p>
</div>
<div class="flex space-x-2">
<button @click={ "approveDevice('" + reg.RegistrationID + "')" } class="btn btn-primary text-sm">
Approve
</button>
<button @click={ "rejectDevice('" + reg.RegistrationID + "')" } class="btn btn-danger text-sm">
Reject
</button>
</div>
</div>
}
</div>
</div>
}
<div id="pairing-section" class="mt-12">
<h2 class="text-lg font-bold tracking-tight mb-1" style="color: var(--text-primary)">Pair a Device</h2>
<p class="text-sm mb-3" style="color: var(--text-secondary)">
On your e-reader, open the Bookhoard sync menu and tap <strong>Register device</strong> — then enter the
6-digit code it shows below. Only someone holding the device can see the code, so nobody
else can claim (or interfere with) your registration.
</p>
<form hx-post="/devices/pairing/lookup" hx-target="#pairing-lookup-result" hx-swap="innerHTML" class="flex items-center gap-2 flex-wrap">
<input
type="text"
name="pairing_code"
inputmode="numeric"
maxlength="7"
pattern="[0-9 ]*"
autocomplete="off"
placeholder="e.g. 583 912"
required
class="input max-w-44 font-mono text-base tracking-widest"
aria-label="Pairing code"
/>
<button type="submit" class="btn btn-primary text-sm">
@Icon("search", "h-4 w-4")
Find device
</button>
</form>
<div id="pairing-lookup-result" class="mt-2" aria-live="polite"></div>
</div>
<div id="add-device-modal" class="hidden fixed inset-0 z-50 flex items-center justify-center p-4" x-data="{ selectedType: '' }" style="background-color: var(--surface-overlay);">
</div>
<div id="add-device-modal" class="hidden fixed inset-0 z-50 flex items-center justify-center p-4" style="background-color: var(--surface-overlay);">
<div class="card p-6 w-full max-w-md" style="box-shadow: var(--shadow-pop);">
<div class="flex justify-between items-center mb-6">
<h2 class="text-xl font-bold" style="color: var(--text-primary)">Add New Device</h2>
<h2 class="text-xl font-bold" style="color: var(--text-primary)">Connect a KOReader Device</h2>
<button @click="hideAddDeviceModal()" class="icon-btn" aria-label="Close">
@Icon("close", "h-5 w-5")
</button>
</div>
<div class="mb-4">
<label class="block text-xs font-semibold uppercase tracking-wide mb-2" style="color: var(--text-secondary)">Device Type</label>
<select
id="device-type"
x-model="selectedType"
required
class="input"
>
<option value="">Select device type...</option>
<option value="koreader">KOReader (Kindle, Kobo, PocketBook)</option>
<option value="kobo">Kobo E-Reader</option>
<!-- <option value="web">Web Browser</option> -->
<!-- <option value="mobile">Mobile App</option> -->
</select>
</div>
<div x-show="selectedType === 'koreader'" x-cloak class="space-y-4">
<div class="space-y-4">
<div class="rounded-lg p-4 space-y-4" style="background-color: var(--surface-secondary);">
<h3 class="text-sm font-semibold flex items-center gap-2" style="color: var(--text-primary)">
@Icon("book-open", "h-4 w-4")
@@ -242,47 +229,22 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
</li>
<li class="flex gap-3">
<span class="flex-shrink-0 grid place-items-center h-5 w-5 rounded-full text-xs font-bold" style="background-color: var(--accent-muted); color: var(--accent);">5</span>
<span>Return to this page and refresh — you'll see a <strong>pending registration</strong>. Click <strong>Approve</strong> to connect the device.</span>
<span>Tap <strong>Register device</strong> — your e-reader shows a <strong>6-digit pairing code</strong>.</span>
</li>
<li class="flex gap-3">
<span class="flex-shrink-0 grid place-items-center h-5 w-5 rounded-full text-xs font-bold" style="background-color: var(--accent-muted); color: var(--accent);">6</span>
<span>Type that code into the <strong>Pair a Device</strong> field on this page and confirm — the device finishes connecting by itself.</span>
</li>
</ol>
<div class="rounded-md p-3 text-xs flex items-start gap-2" style="background-color: var(--accent-muted); color: var(--text-secondary);">
@Icon("info", "h-4 w-4 flex-shrink-0 mt-0.5")
<span>Once approved, reading progress sync and OPDS catalog access are set up automatically.</span>
<span>Only someone holding the device can read the code, so only the right account can claim it. Codes expire after 5 minutes.</span>
</div>
</div>
<div class="flex justify-end">
<button type="button" @click="hideAddDeviceModal()" class="btn btn-primary">Got it</button>
</div>
</div>
<div x-show="selectedType === 'kobo'" x-cloak>
<form id="add-device-form" @submit="handleAddDevice($event)">
<div class="mb-4">
<label class="block text-xs font-semibold uppercase tracking-wide mb-2" style="color: var(--text-secondary)">Device Name</label>
<input
type="text"
id="device-name"
required
class="input"
placeholder="My Kobo"
/>
</div>
<div class="mb-6">
<label class="block text-xs font-semibold uppercase tracking-wide mb-2" style="color: var(--text-secondary)">Device Identifier</label>
<input
type="text"
id="device-identifier"
required
class="input"
placeholder="Hardware ID or Serial Number"
/>
<p class="text-xs mt-1" style="color: var(--text-secondary)">Enter your device's unique identifier</p>
</div>
<div class="flex justify-end space-x-3">
<button type="button" @click="hideAddDeviceModal()" class="btn btn-secondary">Cancel</button>
<button type="submit" class="btn btn-primary">Register Device</button>
</div>
</form>
</div>
</div>
</div>
<div id="device-settings-modal" class="hidden fixed inset-0 z-50 flex items-center justify-center p-4" style="background-color: var(--surface-overlay);">
@@ -489,3 +451,42 @@ templ Devices(user User, devices []handlers.DeviceInfo, pendingRegistrations []P
</body>
</html>
}
// PairingLookupResult renders inside #pairing-lookup-result after a
// pairing-code lookup — the confirm card on a hit, otherwise why not.
// Swapped by htmx from POST /devices/pairing/lookup.
templ PairingLookupResult(deviceName string, deviceType string, pairingCode string, status string) {
if status == "found" {
<div class="card p-4 flex items-center justify-between gap-4 flex-wrap">
<div>
<h3 class="font-semibold" style="color: var(--text-primary)">Found: { deviceName }</h3>
<p class="text-sm" style="color: var(--text-secondary)">
{ deviceType } device · Pair it with your account? Its reading progress will sync here.
</p>
</div>
<form hx-post="/api/devices/claim" hx-target="this" hx-swap="outerHTML">
<input type="hidden" name="pairing_code" value={ pairingCode }/>
<button type="submit" class="btn btn-primary text-sm">
@Icon("device", "h-4 w-4")
Pair device
</button>
</form>
</div>
} else if status == "invalid" {
<p class="text-sm" style="color: var(--error)">
No waiting device matches that code — check the digits on your e-reader's screen.
</p>
} else if status == "expired" {
<p class="text-sm" style="color: var(--status-warning)">
That code expired. Tap <strong>Register device</strong> on the device again for a fresh one.
</p>
} else if status == "claimed" {
<p class="text-sm" style="color: var(--status-warning)">
That code was already used. If the device isn't listed above, register it again from the device.
</p>
} else if status == "rate_limited" {
<p class="text-sm" style="color: var(--status-warning)">
Too many pairing attempts — wait a minute and try again.
</p>
}
}