1import { createMemo, createSignal, onCleanup, onMount } from "solid-js";2import type { BreachScanStatus } from "../../shared/types";3import { sendMessage } from "../App";45function isScanStatus(value: unknown): value is BreachScanStatus {6 if (!value || typeof value !== "object") return false;7 const status = value as Record<string, unknown>;8 const shaped = (status.id === null || typeof status.id === "string") &&9 ["idle", "running", "complete", "cancelled"].includes(String(status.state)) &&10 ["total", "scanned", "unknown", "unapplied", "breached", "cancelled"].every((field) =>11 typeof status[field] === "number" && Number.isSafeInteger(status[field]) && Number(status[field]) >= 0);12 if (!shaped) return false;13 const scan = value as BreachScanStatus;14 const accounted = scan.scanned + scan.unknown + scan.unapplied + scan.cancelled;15 return scan.breached <= scan.scanned && accounted <= scan.total &&16 (scan.state === "running" || accounted === scan.total) &&17 (scan.state !== "idle" || (scan.id === null && scan.total === 0));18}1920function describeScan(status: BreachScanStatus | null): string | null {21 if (!status || status.state === "idle") return null;22 if (!status.total) return status.state === "cancelled" ? "Scan cancelled" : "No passwords to check";23 const details = `${status.scanned} of ${status.total} checked · ${status.breached} found in breaches · ${status.unknown} unavailable · ${status.unapplied} changed or superseded · ${status.cancelled} cancelled`;24 if (status.state === "cancelled") return `Scan cancelled. ${details}`;25 if (status.state === "complete" && status.scanned === status.total && status.breached === 0) {26 return `No breaches found in ${status.scanned} checked password${status.scanned === 1 ? "" : "s"}`;27 }28 return details;29}303132export function createBreachScan(onRefresh: () => void) {33 const [status, setStatus] = createSignal<BreachScanStatus | null>(null);34 const [error, setError] = createSignal<string | null>(null);35 const [starting, setStarting] = createSignal(false);36 let timer: ReturnType<typeof setTimeout> | undefined;37 let disposed = false;38 let request = 0;3940 async function read(start = false): Promise<void> {41 const current = ++request;42 clearTimeout(timer);43 if (start) setStarting(true);44 try {45 const response = await sendMessage({ type: start ? "CHECK_ALL_BREACHES" : "GET_BREACH_SCAN_STATUS" });46 if (disposed || current !== request) return;47 if (!response.ok || !isScanStatus(response.data)) {48 setError(start ? "Could not start breach scan. Try again." : "Could not read breach scan status. Try again.");49 return;50 }51 const previous = status();52 const next = response.data;53 setError(previous?.state === "running" && next.state === "idle" ? "Breach scan interrupted. Run it again." : null);54 setStatus(next);55 if (next.scanned > (previous?.id === next.id ? previous.scanned : 0)) onRefresh();56 if (next.state === "running") timer = setTimeout(() => { void read(); }, 1000);57 } catch {58 if (!disposed && current === request) setError(start ? "Could not start breach scan. Try again." : "Could not read breach scan status. Try again.");59 } finally {60 if (!disposed && current === request) setStarting(false);61 }62 }6364 onMount(() => { void read(); });65 onCleanup(() => { disposed = true; request++; clearTimeout(timer); });66 return {67 checking: createMemo(() => starting() || (status()?.state === "running" && !error())),68 result: createMemo(() => error() ?? describeScan(status())),69 start: () => read(true),70 };71}