fix(scanner): camera restarts automatically after a scan (paused-driven lifecycle)

The camera stopped after decoding one code (by design, one scan at a time)
but was only ever started on mount, so it stayed dead until staff navigated
away and back. The parent now owns the lifecycle via a paused prop: Scan
pauses while busy or any sheet is open (camera re-arms when the sheet
closes), Login pauses while busy (re-arms after a failed scan). scannedRef
guards duplicate callbacks between decode and stop.
This commit is contained in:
Lago committed 2026-10-19 11:03:08 +02:00
1 parent 316f1316e6
commit c857204cfe
4 files changed
+59 -6

No files matched your search

+16 -5
View File
@@ -85,19 +85,27 @@ export function QrImage({ value, size = 180 }: { value: string; size?: number })
* decoded text. The camera only works on a secure context (https or localhost);
* the manual field always works.
*/
export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'QR code', placeholder = '…or type the code (e.g. CVV-XXX-NNNN)', submitLabel = 'Go', disabled = false, submitDisabled = false, onEntryChange }: { onResult: (text: string) => void; label?: string; entryLabel?: string; placeholder?: string; submitLabel?: string; disabled?: boolean; submitDisabled?: boolean; onEntryChange?: (text: string) => void }) {
export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'QR code', placeholder = '…or type the code (e.g. CVV-XXX-NNNN)', submitLabel = 'Go', disabled = false, submitDisabled = false, onEntryChange, paused = false }: { onResult: (text: string) => void; label?: string; entryLabel?: string; placeholder?: string; submitLabel?: string; disabled?: boolean; submitDisabled?: boolean; onEntryChange?: (text: string) => void; paused?: boolean }) {
const [manual, setManual] = useState('');
const [camError, setCamError] = useState<string | null>(null);
const scannerRef = useRef<Html5Qrcode | null>(null);
const [camOn, setCamOn] = useState(false);
// The camera callback below is registered once on mount; without a ref it
// The camera runs only while `paused` is false. The parent sets `paused`
// while it is busy or a result sheet is open, so a scan stops the camera
// (one code at a time) and closing the sheet restarts it automatically —
// no need to navigate away to re-arm it.
// The camera callback below is registered once per start; without a ref it
// would keep calling the FIRST render's onResult, whose closures over
// parent state (welcome mode, selected session) are stale. Always invoke
// the latest handler through this ref.
const onResultRef = useRef(onResult);
onResultRef.current = onResult;
// Guards against the callback firing again for the same lanyard in the
// frames between the decode and the camera actually stopping.
const scannedRef = useRef(false);
useEffect(() => {
if (paused) return;
let cancelled = false;
const el = document.getElementById('qr-reader');
if (!el) return;
@@ -114,6 +122,7 @@ export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'Q
}
let scanner: Html5Qrcode | null = null;
scannedRef.current = false;
try {
scanner = new Html5Qrcode('qr-reader');
scannerRef.current = scanner;
@@ -122,8 +131,11 @@ export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'Q
{ facingMode: 'environment' },
{ fps: 10, qrbox: { width: 220, height: 220 } },
(text) => {
if (cancelled) return;
if (cancelled || scannedRef.current) return;
scannedRef.current = true;
setCamOn(false);
// One code at a time: stop the camera now; the parent's `paused`
// (busy / sheet open) keeps it off, and clearing it restarts it.
scanner?.stop().catch(() => {});
onResultRef.current(text);
},
@@ -147,8 +159,7 @@ export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'Q
/* ignore */
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [paused]);
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>