import { createMemo, createSignal, onCleanup, onMount } from "solid-js"; import type { BreachScanStatus } from "../../shared/types"; import { sendMessage } from "../App"; function isScanStatus(value: unknown): value is BreachScanStatus { if (!value || typeof value !== "object") return false; const status = value as Record; const shaped = (status.id === null || typeof status.id === "string") && ["idle", "running", "complete", "cancelled"].includes(String(status.state)) && ["total", "scanned", "unknown", "unapplied", "breached", "cancelled"].every((field) => typeof status[field] === "number" && Number.isSafeInteger(status[field]) && Number(status[field]) >= 0); if (!shaped) return false; const scan = value as BreachScanStatus; const accounted = scan.scanned + scan.unknown + scan.unapplied + scan.cancelled; return scan.breached <= scan.scanned && accounted <= scan.total && (scan.state === "running" || accounted === scan.total) && (scan.state !== "idle" || (scan.id === null && scan.total === 0)); } function describeScan(status: BreachScanStatus | null): string | null { if (!status || status.state === "idle") return null; if (!status.total) return status.state === "cancelled" ? "Scan cancelled" : "No passwords to check"; const details = `${status.scanned} of ${status.total} checked · ${status.breached} found in breaches · ${status.unknown} unavailable · ${status.unapplied} changed or superseded · ${status.cancelled} cancelled`; if (status.state === "cancelled") return `Scan cancelled. ${details}`; if (status.state === "complete" && status.scanned === status.total && status.breached === 0) { return `No breaches found in ${status.scanned} checked password${status.scanned === 1 ? "" : "s"}`; } return details; } /// Keeps an extension view connected to the current background breach scan. export function createBreachScan(onRefresh: () => void) { const [status, setStatus] = createSignal(null); const [error, setError] = createSignal(null); const [starting, setStarting] = createSignal(false); let timer: ReturnType | undefined; let disposed = false; let request = 0; async function read(start = false): Promise { const current = ++request; clearTimeout(timer); if (start) setStarting(true); try { const response = await sendMessage({ type: start ? "CHECK_ALL_BREACHES" : "GET_BREACH_SCAN_STATUS" }); if (disposed || current !== request) return; if (!response.ok || !isScanStatus(response.data)) { setError(start ? "Could not start breach scan. Try again." : "Could not read breach scan status. Try again."); return; } const previous = status(); const next = response.data; setError(previous?.state === "running" && next.state === "idle" ? "Breach scan interrupted. Run it again." : null); setStatus(next); if (next.scanned > (previous?.id === next.id ? previous.scanned : 0)) onRefresh(); if (next.state === "running") timer = setTimeout(() => { void read(); }, 1000); } catch { if (!disposed && current === request) setError(start ? "Could not start breach scan. Try again." : "Could not read breach scan status. Try again."); } finally { if (!disposed && current === request) setStarting(false); } } onMount(() => { void read(); }); onCleanup(() => { disposed = true; request++; clearTimeout(timer); }); return { checking: createMemo(() => starting() || (status()?.state === "running" && !error())), result: createMemo(() => error() ?? describeScan(status())), start: () => read(true), }; }