"use client"; import { useRouter } from "next/navigation"; import { useState } from "react"; /** * Runs a network scan. The scan writes the server-side registry, so afterwards the * page is refreshed and the server re-renders the camera list. */ export default function CameraScanner() { const router = useRouter(); const [scanning, setScanning] = useState(false); const [result, setResult] = useState(null); const [error, setError] = useState(null); const [timeout, setTimeoutSecs] = useState(5); const [unicastSweep, setUnicastSweep] = useState(true); async function scan() { setScanning(true); setError(null); setResult(null); try { const res = await fetch("/api/discover", { method: "POST", headers: { "Content-Type": "application/json" }, // The server rejects timeouts outside 1–30 s; keep a half-typed value in range. body: JSON.stringify({ timeout: Math.min(Math.max(Math.round(timeout) || 5, 1), 30) * 1000, unicastSweep, }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error ?? `HTTP ${res.status}`); const found = data.cameras.length; setResult(`Scan found ${found} camera${found === 1 ? "" : "s"}`); router.refresh(); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } finally { setScanning(false); } } return ( <> {result &&

{result}

} {error &&

Error: {error}

} ); }