import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { Capacitor } from "@capacitor/core"; import { useAnnouncer } from "@/components/A11yAnnouncer"; import { ReaderActivityControl } from "@/components/ReaderActivityControl"; import { NfcIcon } from "@/lib/images"; import { cancelSession, readTag, Status } from "@/lib/nfc"; import { nfcErrorMessageKey } from "@/lib/nfcErrorMessages"; import { launchFeedback } from "@/lib/launchFeedback"; import { getAntennaSpot, type AntennaSpot } from "@/lib/nfcAntenna"; // The iPhone outline; Android reports its own dimensions. const IPHONE_ASPECT_RATIO = 0.47; type DrawnSpot = Exclude; /** A phone outline with the antenna marked. Not drawn when the spot is unknown. */ function AntennaDiagram({ spot }: { spot: DrawnSpot }) { const aspectRatio = spot.kind === "placed" ? spot.aspectRatio : IPHONE_ASPECT_RATIO; return ( ); } const hintKey: Record = { placed: "proGuide.antennaPlaced", topEdge: "proGuide.antennaTopEdge", broad: "proGuide.antennaBroad", }; type ReadResult = { kind: "read" } | { kind: "error"; message: string } | null; /** * Shows where to hold a tag and runs a live read that launches nothing. The * read session is stopped when the step is left, the guide closes or the * component unmounts. */ export function ProGuideAntennaStep({ active, previewAntenna, }: { active: boolean; previewAntenna: AntennaSpot | null; }) { const { t } = useTranslation(); const { announce } = useAnnouncer(); const [detectedSpot, setDetectedSpot] = useState(null); const [reading, setReading] = useState(false); const [result, setResult] = useState(null); const readingRef = useRef(false); const mountedRef = useRef(true); const spot = previewAntenna ?? detectedSpot; useEffect(() => { if (previewAntenna) return; let cancelled = false; void getAntennaSpot().then((found) => { if (!cancelled) setDetectedSpot(found); }); return () => { cancelled = true; }; }, [previewAntenna]); useEffect(() => { mountedRef.current = true; return () => { mountedRef.current = false; if (readingRef.current) void cancelSession(); }; }, []); useEffect(() => { if (!active && readingRef.current) void cancelSession(); }, [active]); const startRead = async () => { if (readingRef.current) return; readingRef.current = true; setReading(true); setResult(null); try { const outcome = await readTag(); if (!mountedRef.current) return; if (outcome.status === Status.Success) { // Android's reader session already buzzes on a read. launchFeedback.read("nfc", Capacitor.getPlatform() === "android"); setResult({ kind: "read" }); announce(t("proGuide.tagRead")); } } catch (error) { if (!mountedRef.current) return; const key = nfcErrorMessageKey(error); const message = key ? t(key) : t("scan.scanError"); setResult({ kind: "error", message }); announce(message, "assertive"); } finally { readingRef.current = false; if (mountedRef.current) setReading(false); } }; return (

{spot ? t(hintKey[spot.kind]) : t("proGuide.antennaLoading")}

{spot && spot.kind !== "broad" && } {result?.kind === "read" && (

{t("proGuide.tagRead")}

)} {result?.kind === "error" && (

{result.message}

)} } onStart={() => void startRead()} onCancel={() => void cancelSession()} variant="outline" intent="default" buttonClassName="w-full" />
); }