2026-08-25 07:51:38 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2026-08-30 20:46:53 +02:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2026-08-25 07:51:38 +02:00
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
2026-08-30 20:46:53 +02:00
|
|
|
import { Check, Loader2 } from "lucide-react";
|
2026-08-25 07:51:38 +02:00
|
|
|
import { toast } from "sonner";
|
2026-08-30 20:46:53 +02:00
|
|
|
import { cn } from "@/lib/utils";
|
2026-08-25 07:51:38 +02:00
|
|
|
|
|
|
|
|
export interface DeviceRow {
|
|
|
|
|
id: number;
|
|
|
|
|
displayName: string | null;
|
|
|
|
|
bleName: string;
|
|
|
|
|
lastConnectedAt: number | null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-30 20:46:53 +02:00
|
|
|
const AUTOSAVE_DELAY_MS = 600;
|
|
|
|
|
|
2026-08-25 07:51:38 +02:00
|
|
|
function DeviceNameCell({ device }: { device: DeviceRow }) {
|
|
|
|
|
const router = useRouter();
|
2026-08-30 20:46:53 +02:00
|
|
|
const initial = device.displayName ?? device.bleName;
|
|
|
|
|
const [value, setValue] = useState(initial);
|
|
|
|
|
const [status, setStatus] = useState<"idle" | "saving" | "saved">("idle");
|
|
|
|
|
const lastSaved = useRef(initial);
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-30 20:46:53 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
const trimmed = value.trim();
|
|
|
|
|
if (trimmed.length === 0 || trimmed === lastSaved.current) return;
|
|
|
|
|
|
|
|
|
|
setStatus("saving");
|
|
|
|
|
const timer = setTimeout(async () => {
|
|
|
|
|
const res = await fetch(`/api/devices/${device.id}`, {
|
|
|
|
|
method: "PATCH",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({ displayName: trimmed }),
|
|
|
|
|
});
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
lastSaved.current = trimmed;
|
|
|
|
|
setStatus("saved");
|
|
|
|
|
router.refresh();
|
|
|
|
|
} else {
|
|
|
|
|
setStatus("idle");
|
|
|
|
|
toast.error("Could not rename device");
|
|
|
|
|
}
|
|
|
|
|
}, AUTOSAVE_DELAY_MS);
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
}, [value, device.id, router]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (status !== "saved") return;
|
|
|
|
|
const timer = setTimeout(() => setStatus("idle"), 1500);
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
}, [status]);
|
2026-08-25 07:51:38 +02:00
|
|
|
|
|
|
|
|
return (
|
2026-08-31 19:41:13 +02:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<input
|
2026-08-30 20:46:53 +02:00
|
|
|
value={value}
|
|
|
|
|
onChange={(e) => setValue(e.target.value)}
|
|
|
|
|
aria-label="Device display name"
|
2026-08-31 19:41:13 +02:00
|
|
|
className="max-w-48 min-w-0 border-b-2 border-transparent bg-transparent text-sm font-medium text-foreground caret-primary outline-none transition-colors placeholder:text-muted-foreground/70 hover:border-b-primary focus:border-b-primary [field-sizing:content]"
|
2026-08-30 20:46:53 +02:00
|
|
|
/>
|
2026-08-31 19:41:13 +02:00
|
|
|
<div className="relative flex size-4 shrink-0 items-center justify-center">
|
2026-08-30 20:46:53 +02:00
|
|
|
<Loader2
|
|
|
|
|
className={cn(
|
2026-08-31 19:41:13 +02:00
|
|
|
"absolute size-4 animate-spin text-muted-foreground transition-opacity",
|
2026-08-30 20:46:53 +02:00
|
|
|
status === "saving" ? "opacity-100" : "opacity-0",
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<Check
|
|
|
|
|
className={cn(
|
2026-08-31 19:41:13 +02:00
|
|
|
"absolute size-4 text-primary transition-opacity",
|
2026-08-30 20:46:53 +02:00
|
|
|
status === "saved" ? "opacity-100" : "opacity-0",
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2026-08-25 07:51:38 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function DevicesTable({ devices }: { devices: DeviceRow[] }) {
|
|
|
|
|
if (devices.length === 0) {
|
|
|
|
|
return <p className="text-sm text-muted-foreground">No devices seen yet - connect one from the Control page.</p>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableHead>Display name</TableHead>
|
|
|
|
|
<TableHead>Advertised name</TableHead>
|
|
|
|
|
<TableHead>Last connected</TableHead>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{devices.map((d) => (
|
|
|
|
|
<TableRow key={d.id}>
|
|
|
|
|
<TableCell>
|
|
|
|
|
<DeviceNameCell device={d} />
|
|
|
|
|
</TableCell>
|
2026-08-25 16:43:28 +02:00
|
|
|
<TableCell className="bp-readout text-muted-foreground">{d.bleName}</TableCell>
|
|
|
|
|
<TableCell className="bp-readout text-muted-foreground">
|
2026-08-25 07:51:38 +02:00
|
|
|
{d.lastConnectedAt ? new Date(d.lastConnectedAt).toLocaleString() : "Never"}
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
);
|
|
|
|
|
}
|