refactor(qr): shared useQrScanner hook dedupes Verify + ScanUpdate - #27
Merged
Conversation
Both pages carried their own ~80-line html5-qrcode setup/stop/cleanup copy with drifting configs. Extract useQrScanner: instance lifecycle, optimistic start, awaited stop+clear, per-session single-fire guard, caller-rejectable decodes (return false = keep scanning), responsive qrbox support and unmount cleanup (R8). Verify keeps its fps=30/BarcodeDetector/responsive-qrbox config and friendly error mapping; ScanUpdate shrinks by ~50 lines.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The QR scanner logic was duplicated across two pages (audit Code Quality + the report's #10 priority):
Verify.tsxandScanUpdate.tsxeach grew their own ~80-line html5-qrcode setup/stop/cleanup copy, with different configs (fps 30 vs 12, responsive vs fixed qrbox, BarcodeDetector on vs off), duplicated single-fire guards, and independently hand-rolled stop/clear error handling. Any camera fix had to be applied twice — and drift was already visible.Fix
New shared hook —
src/hooks/use-qr-scanner.ts(useQrScanner):onDecodemay returnfalseto keep scanning (Verify uses this when a decoded payload doesn't extract to a product code — preserving its exact old semantics).fps, fixed-or-functionalqrbox(Verify passes a responsive one computed from container width), anduseBarCodeDetectorIfSupported.start/stopstay referentially stable and the cleanup can never capture a stale closure.src/pages/Verify.tsx:onError, unchanged.activeRef; geo-after-camera behavior untouched.src/pages/ScanUpdate.tsx:onDecode. Net: the page shrank by ~50 lines.Files Changed
src/hooks/use-qr-scanner.ts— new shared scanner hook.src/pages/Verify.tsx,src/pages/ScanUpdate.tsx— consumers; duplicate lifecycle code deleted.Verification
npx tsc --noEmit -p tsconfig.app.json→ exit 0 (strict mode).npm run build→ ✓ built;npm test→ 35/35 passing.Manual Test (post-deploy)