Initial commit

Next.js + Express event management app for Hope Family Church.
This commit is contained in:
2026-07-23 15:26:47 +02:00
commit fdd2d241b4
246 changed files with 57565 additions and 0 deletions
@@ -0,0 +1,78 @@
"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import { BrowserQRCodeReader } from "@zxing/browser";
export type UseQRScannerOptions = {
onResult?: (text: string) => void;
onError?: (error: unknown) => void;
};
// Hook to decode QR codes from a HTMLVideoElement using @zxing/browser
export function useQRScanner(videoEl: HTMLVideoElement | null, opts: UseQRScannerOptions = {}) {
const { onResult, onError } = opts;
const [active, setActive] = useState(false);
const [permissionError, setPermissionError] = useState<string | null>(null);
const readerRef = useRef<BrowserQRCodeReader | null>(null);
const cancelRef = useRef<(() => void) | undefined>(undefined);
const stop = useCallback(() => {
cancelRef.current?.();
cancelRef.current = undefined;
}, []);
const toggle = useCallback(() => setActive((p) => !p), []);
useEffect(() => {
readerRef.current = new BrowserQRCodeReader();
return () => {
stop();
readerRef.current = null;
};
}, [stop]);
useEffect(() => {
let cancelled = false;
async function run() {
setPermissionError(null);
try {
// Start decode loop
const reader = readerRef.current!;
const scan = async () => {
if (cancelled) return;
try {
if (!videoEl) return;
const result = await reader.decodeOnceFromVideoElement(videoEl);
const text = (result as any)?.getText ? (result as any).getText() : (result as any)?.text ?? String(result ?? "");
if (text) onResult?.(text);
} catch (err) {
// Ignore transient decode errors and retry
} finally {
if (!cancelled && active) setTimeout(scan, 200);
}
};
scan();
cancelRef.current = () => {
cancelled = true;
};
} catch (err: any) {
if (err?.name === "NotAllowedError") {
setPermissionError("Camera permission denied. Please enable camera access.");
} else if (err?.name === "NotFoundError") {
setPermissionError("No suitable camera found. Try a device with a back camera.");
} else {
setPermissionError("Unable to access camera.");
}
onError?.(err);
setActive(false);
}
}
if (active && videoEl) run();
else stop();
}, [active, onError, onResult, stop, videoEl]);
return { active, toggle, stop, permissionError };
}