Branchwell

Become a Pro / website · becomeapro.ai / src / app / account / devices.tsx

Raw 3.1 KB on f63bc385c0fa362af985bfb576a8b5aa2a3df60f
1"use client";
2
3import { useCallback, useEffect, useState } from "react";
4import { listDevices, makeDeviceLink, removeDevice } from "@/lib/biolock/use-biolock";
5import { enrolledHere } from "@/lib/biolock/use-biolock";
6
7type Device = Awaited<ReturnType<typeof listDevices>>[number];
8const when = (seconds: number) => new Date(seconds * 1000).toLocaleDateString(undefined, { month: "short", day: "numeric", year: "numeric" });
9
10// The devices that can sign in to this account with BioLock, and one-time links to add another.
11export function Devices() {
12 const [devices, setDevices] = useState<Device[] | null>(null);
13 const [link, setLink] = useState<string | null>(null);
14 const [message, setMessage] = useState<string | null>(null);
15 const here = typeof window === "undefined" ? [] : enrolledHere();
16
17 const load = useCallback(() => {
18 listDevices().then(setDevices).catch((error: Error) => setMessage(error.message));
19 }, []);
20 useEffect(load, [load]);
21
22 const add = async () => {
23 setMessage(null);
24 try {
25 const { url } = await makeDeviceLink();
26 setLink(url);
27 } catch (error) {
28 setMessage(error instanceof Error ? error.message : "Could not make a link.");
29 }
30 };
31
32 const remove = async (id: string) => {
33 setMessage(null);
34 try {
35 await removeDevice(id);
36 load();
37 } catch (error) {
38 setMessage(error instanceof Error ? error.message : "Could not remove that device.");
39 }
40 };
41
42 return (
43 <section id="devices" className="mt-20 scroll-mt-28">
44 <h2 className="mb-2 font-mono text-xs font-medium uppercase tracking-[0.08em] text-[var(--ash)]">Your devices</h2>
45 <div className="border-t border-border pt-6">
46 <p className="text-[var(--mist)]">These devices sign in with their own fingerprint or face reader.</p>
47 <ul className="mt-6 divide-y divide-border">
48 {(devices ?? []).map((device) => (
49 <li key={device.id} className="flex items-center justify-between gap-4 py-3">
50 <span>
51 <span className="block">{device.name}{here.includes(device.id) ? " (this device)" : ""}</span>
52 <span className="block text-sm text-[var(--ash)]">Added {when(device.created_at)}{device.last_used ? ` · last used ${when(device.last_used)}` : ""}</span>
53 </span>
54 <button type="button" className="text-link text-sm" onClick={() => void remove(device.id)}>Remove</button>
55 </li>
56 ))}
57 </ul>
58 <button type="button" className="pill pill-sm mt-6" onClick={() => void add()}>Add a device</button>
59 {link && (
60 <div className="mt-6 rounded-xl border border-foreground/15 p-4 text-sm">
61 <p className="text-[var(--mist)]">Open this link on the other device within 10 minutes. It works once.</p>
62 <p className="mt-3 break-all font-mono text-[13px]">{link}</p>
63 <button type="button" className="text-link mt-3" onClick={() => void navigator.clipboard?.writeText(link)}>Copy link</button>
64 </div>
65 )}
66 {message && <p role="alert" className="mt-4 text-sm">{message}</p>}
67 </div>
68 </section>
69 );
70}