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. Co-authored-by: Stephan Charles <[email protected]> Co-authored-by: Amelia Hernandez <[email protected]> Co-authored-by: Markus Franz <[email protected]> Co-authored-by: Sabrina Hota <[email protected]> Co-authored-by: Nilüfer Doğan <[email protected]>
This commit is contained in:
1 parent
383a80f5ec
commit
d3531e1fac
4 files changed
+59
-6
No files matched your search
@@ -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);
|
* decoded text. The camera only works on a secure context (https or localhost);
|
||||||
* the manual field always works.
|
* 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 [manual, setManual] = useState('');
|
||||||
const [camError, setCamError] = useState<string | null>(null);
|
const [camError, setCamError] = useState<string | null>(null);
|
||||||
const scannerRef = useRef<Html5Qrcode | null>(null);
|
const scannerRef = useRef<Html5Qrcode | null>(null);
|
||||||
const [camOn, setCamOn] = useState(false);
|
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
|
// would keep calling the FIRST render's onResult, whose closures over
|
||||||
// parent state (welcome mode, selected session) are stale. Always invoke
|
// parent state (welcome mode, selected session) are stale. Always invoke
|
||||||
// the latest handler through this ref.
|
// the latest handler through this ref.
|
||||||
const onResultRef = useRef(onResult);
|
const onResultRef = useRef(onResult);
|
||||||
onResultRef.current = 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(() => {
|
useEffect(() => {
|
||||||
|
if (paused) return;
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
const el = document.getElementById('qr-reader');
|
const el = document.getElementById('qr-reader');
|
||||||
if (!el) return;
|
if (!el) return;
|
||||||
@@ -114,6 +122,7 @@ export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'Q
|
|||||||
}
|
}
|
||||||
|
|
||||||
let scanner: Html5Qrcode | null = null;
|
let scanner: Html5Qrcode | null = null;
|
||||||
|
scannedRef.current = false;
|
||||||
try {
|
try {
|
||||||
scanner = new Html5Qrcode('qr-reader');
|
scanner = new Html5Qrcode('qr-reader');
|
||||||
scannerRef.current = scanner;
|
scannerRef.current = scanner;
|
||||||
@@ -122,8 +131,11 @@ export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'Q
|
|||||||
{ facingMode: 'environment' },
|
{ facingMode: 'environment' },
|
||||||
{ fps: 10, qrbox: { width: 220, height: 220 } },
|
{ fps: 10, qrbox: { width: 220, height: 220 } },
|
||||||
(text) => {
|
(text) => {
|
||||||
if (cancelled) return;
|
if (cancelled || scannedRef.current) return;
|
||||||
|
scannedRef.current = true;
|
||||||
setCamOn(false);
|
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(() => {});
|
scanner?.stop().catch(() => {});
|
||||||
onResultRef.current(text);
|
onResultRef.current(text);
|
||||||
},
|
},
|
||||||
@@ -147,8 +159,7 @@ export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'Q
|
|||||||
/* ignore */
|
/* ignore */
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
}, [paused]);
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
|
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ export function Login() {
|
|||||||
<div className="h1">Welcome back 👋</div>
|
<div className="h1">Welcome back 👋</div>
|
||||||
<Scanner
|
<Scanner
|
||||||
onResult={handleScan}
|
onResult={handleScan}
|
||||||
|
paused={busy}
|
||||||
label="Point your camera at your lanyard QR"
|
label="Point your camera at your lanyard QR"
|
||||||
entryLabel="QR Code"
|
entryLabel="QR Code"
|
||||||
placeholder="QR Code"
|
placeholder="QR Code"
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ export function Scan() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Scanner onResult={handleScan} label={activeStaff ? 'Point at an attendee’s lanyard' : 'Point at a peer’s lanyard'} />
|
<Scanner onResult={handleScan} paused={busy || !!connectTarget || !!checkin || !!pendingWelcome || !!welcome} label={activeStaff ? 'Point at an attendee’s lanyard' : 'Point at a peer’s lanyard'} />
|
||||||
{busy && <div className="center"><Spinner /></div>}
|
{busy && <div className="center"><Spinner /></div>}
|
||||||
{activeStaff && !welcomeMode && selSession && (
|
{activeStaff && !welcomeMode && selSession && (
|
||||||
<div className="muted small" style={{ textAlign: 'center', opacity: 0.6 }}>
|
<div className="muted small" style={{ textAlign: 'center', opacity: 0.6 }}>
|
||||||
|
|||||||
@@ -118,3 +118,44 @@ the toggle:
|
|||||||
**Deployed to production 2026-10-19**: migration applied + ledger (40 → 41);
|
**Deployed to production 2026-10-19**: migration applied + ledger (40 → 41);
|
||||||
`convivo-app` and `convivo-admin` rebuilt from HEAD behind the drift guard (all
|
`convivo-app` and `convivo-admin` rebuilt from HEAD behind the drift guard (all
|
||||||
three changed files byte-identical to the deployed baseline).
|
three changed files byte-identical to the deployed baseline).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Follow-up: camera stays dead after a scan (2026-10-19)
|
||||||
|
|
||||||
|
## Symptom (reported from production)
|
||||||
|
|
||||||
|
After a successful scan the camera stopped working — staff had to navigate to
|
||||||
|
another tab and back to re-activate it before scanning the next lanyard.
|
||||||
|
|
||||||
|
## Cause
|
||||||
|
|
||||||
|
`Scanner` deliberately stops the camera after decoding one code (so the same
|
||||||
|
lanyard cannot fire twice), but it only ever started the camera once, on mount.
|
||||||
|
Nothing restarted it afterwards.
|
||||||
|
|
||||||
|
## Fix
|
||||||
|
|
||||||
|
The camera lifecycle is now driven by a `paused` prop owned by the parent:
|
||||||
|
|
||||||
|
- `Scan.tsx` passes `paused = busy || a sheet is open` (connect, session
|
||||||
|
result, welcome confirm, welcome result). The camera stops while a sheet is
|
||||||
|
up and **restarts automatically the moment the sheet closes** — "Scan next"
|
||||||
|
or ✕ is all it takes now.
|
||||||
|
- `Login.tsx` passes `paused = busy` so a failed scan (e.g. unknown code)
|
||||||
|
re-arms the camera immediately instead of leaving it dead.
|
||||||
|
- A `scannedRef` guard ignores duplicate callbacks in the frames between the
|
||||||
|
decode and the camera actually stopping.
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
Real-browser smoke against a local build: staff login → Welcome mode → scan →
|
||||||
|
confirm popup → **Yes** → result sheet → **Scan next** → sheet closes and the
|
||||||
|
camera effect re-runs (fresh `Html5Qrcode` instance starts; the library clears
|
||||||
|
the element in its constructor, so re-attaching to the same div is safe).
|
||||||
|
Badge queued correctly; test data cleaned afterwards.
|
||||||
|
|
||||||
|
## Deployment
|
||||||
|
|
||||||
|
**Deployed to production 2026-10-19**: attendee app only (`convivo-app`
|
||||||
|
rebuilt; no migration, admin/sidecar untouched).
|
||||||
Reference in new issue
Block a user