Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
750 lines
35 KiB
TypeScript
750 lines
35 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useRef, useEffect, useCallback } from "react";
|
|
import { motion, AnimatePresence } from "framer-motion";
|
|
import {
|
|
ScanLine, CheckCircle2, Copy, RefreshCw,
|
|
Coins, AlertCircle,
|
|
} from "lucide-react";
|
|
import { useTheme } from "@/app/lib/ThemeContext";
|
|
import { useMember } from "@/app/lib/MemberContext";
|
|
import { apiGet, apiPost, ApiError } from "@/app/lib/clientApi";
|
|
import type { CheckInResult } from "@/app/lib/types";
|
|
import MemberQR from "@/app/components/ui/MemberQR";
|
|
|
|
/* ── Constants ───────────────────────────────────────────────────── */
|
|
type ViewMode = "scan" | "badge";
|
|
|
|
/** All possible states the Scan tab can be in. */
|
|
type ScanState =
|
|
| "idle" // waiting for user to tap "Tap to Scan"
|
|
| "starting" // camera is spinning up (dynamic import + Html5Qrcode.start)
|
|
| "scanning" // camera is live, waiting for a QR decode
|
|
| "verifying" // QR decoded, calling BFF
|
|
| "success" // CHECKED_IN
|
|
| "already" // ALREADY_CHECKED_IN — non-error, member already checked in today
|
|
| "failure" // any non-success status or thrown error
|
|
| "camera_error" // permission denied / no camera
|
|
| "insecure"; // window.isSecureContext === false
|
|
|
|
/** Map CheckInResult.status → user-facing message. */
|
|
function statusMessage(status: CheckInResult["status"]): string {
|
|
switch (status) {
|
|
case "NO_ACTIVE_EVENT": return "There's no active event right now.";
|
|
case "WRONG_QR_TYPE": return "That's not a check-in code.";
|
|
case "INVALID_QR": return "This QR code isn't valid.";
|
|
case "CHECK_IN_FAILED": return "Check-in couldn't be completed. Please try again.";
|
|
default: return "Check-in couldn't be completed. Please try again.";
|
|
}
|
|
}
|
|
|
|
/* ── Animations ──────────────────────────────────────────────────── */
|
|
const stagger = { hidden: {}, visible: { transition: { staggerChildren: 0.07 } } };
|
|
const fadeUp = {
|
|
hidden: { opacity: 0, y: 14 },
|
|
visible: { opacity: 1, y: 0, transition: { type: "spring" as const, stiffness: 340, damping: 28 } },
|
|
};
|
|
|
|
export default function CheckInPage() {
|
|
const { isDark } = useTheme();
|
|
const { member } = useMember();
|
|
const memberCode = member?.memberCode ?? "";
|
|
|
|
const [view, setView] = useState<ViewMode>("scan");
|
|
const [checkedIn, setCheckedIn] = useState(false);
|
|
const [copied, setCopied] = useState(false);
|
|
const [scanState, setScanState] = useState<ScanState>("idle");
|
|
const [failureMsg, setFailureMsg] = useState<string>("");
|
|
const [eventName, setEventName] = useState<string | undefined>();
|
|
const [balance, setBalance] = useState(0);
|
|
const [earnedToday, setEarnedToday] = useState(0);
|
|
|
|
// Live loyalty figures — same endpoints the home page uses.
|
|
useEffect(() => {
|
|
apiGet<{ balance: number }>("/api/points/balance")
|
|
.then((d) => setBalance(d.balance ?? 0))
|
|
.catch(() => {});
|
|
apiGet<{ earnedToday: number }>("/api/points/today")
|
|
.then((d) => setEarnedToday(d.earnedToday ?? 0))
|
|
.catch(() => {});
|
|
}, []);
|
|
|
|
// Holds the live Html5Qrcode instance so we can stop it from anywhere.
|
|
const scannerRef = useRef<any>(null);
|
|
const stoppingRef = useRef(false); // guard against double-stop
|
|
const isMountedRef = useRef(true); // guard against post-unmount state writes
|
|
// Bumped whenever an in-flight start() sequence should be treated as stale:
|
|
// on unmount, and when the user leaves the Scan tab while starting/scanning.
|
|
// Each handleScan invocation captures the counter's value at call time and
|
|
// compares it at every async checkpoint — if it no longer matches (because
|
|
// this attempt was cancelled, or a newer handleScan call has since bumped
|
|
// it), the attempt bails out instead of advancing to "scanning". This is
|
|
// safer than a shared cancelled/not-cancelled boolean, which a newer
|
|
// invocation could reset out from under a still in-flight older one.
|
|
const startGenerationRef = useRef(0);
|
|
// Every camera MediaStream html5-qrcode opens during our start() attempts.
|
|
// The library leaks the track if its render step is interrupted (e.g. the
|
|
// reader element unmounts mid-start on navigation) — it rejects start()
|
|
// without ever calling track.stop(), and its own stop() refuses to run when
|
|
// the scanner never reached the SCANNING state. We capture the stream by
|
|
// wrapping getUserMedia around start() and force-stop it on every teardown so
|
|
// the camera is always released, even when the library abandons it.
|
|
const openedStreamsRef = useRef<MediaStream[]>([]);
|
|
|
|
/** Force-stop every camera track we captured. Idempotent. */
|
|
const releaseOpenedStreams = useCallback(() => {
|
|
for (const stream of openedStreamsRef.current) {
|
|
try {
|
|
stream.getTracks().forEach((track) => track.stop());
|
|
} catch {
|
|
// A track may already be stopped — ignore.
|
|
}
|
|
}
|
|
openedStreamsRef.current = [];
|
|
}, []);
|
|
|
|
/** Tear down the camera. Safe to call multiple times. */
|
|
const stopCamera = useCallback(async () => {
|
|
if (!stoppingRef.current && scannerRef.current) {
|
|
stoppingRef.current = true;
|
|
try {
|
|
await scannerRef.current.stop();
|
|
} catch {
|
|
// Ignore stop errors (e.g. already stopped, or never reached SCANNING).
|
|
} finally {
|
|
scannerRef.current = null;
|
|
stoppingRef.current = false;
|
|
}
|
|
}
|
|
// Always release captured streams — covers an orphaned start() whose track
|
|
// the library opened but never attached to a (now-unmounted) reader element,
|
|
// where scannerRef was never set and scanner.stop() would not help.
|
|
releaseOpenedStreams();
|
|
}, [releaseOpenedStreams]);
|
|
|
|
// Tear down on unmount
|
|
useEffect(() => {
|
|
isMountedRef.current = true;
|
|
return () => {
|
|
isMountedRef.current = false;
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- generation counter: we intentionally bump its live value at unmount to invalidate in-flight start() sequences, not a mount-time snapshot.
|
|
startGenerationRef.current++;
|
|
void stopCamera();
|
|
};
|
|
}, [stopCamera]);
|
|
|
|
/** Called when the user taps "Tap to Scan". */
|
|
const handleScan = useCallback(async () => {
|
|
// Capture this invocation's generation up front. Any later bump of
|
|
// startGenerationRef.current (unmount, or leaving the Scan tab) means
|
|
// this specific attempt has been superseded, even if a newer handleScan
|
|
// call has since started and is running concurrently.
|
|
const myGeneration = ++startGenerationRef.current;
|
|
|
|
if (!window.isSecureContext) {
|
|
setScanState("insecure");
|
|
return;
|
|
}
|
|
|
|
setScanState("starting");
|
|
|
|
let scannerInstance: InstanceType<typeof import("html5-qrcode")["Html5Qrcode"]>;
|
|
|
|
try {
|
|
const { Html5Qrcode } = await import("html5-qrcode");
|
|
scannerInstance = new Html5Qrcode("checkin-reader");
|
|
} catch {
|
|
if (!isMountedRef.current) return;
|
|
setScanState("camera_error");
|
|
setFailureMsg("Could not load the camera module.");
|
|
return;
|
|
}
|
|
|
|
// The user may have left the Scan tab (or the page unmounted), or a newer
|
|
// scan attempt may have since started, while the html5-qrcode module was
|
|
// loading. Abort before wiring up any camera state — nothing has been
|
|
// started yet, so there is nothing to stop.
|
|
if (!isMountedRef.current || startGenerationRef.current !== myGeneration) {
|
|
return;
|
|
}
|
|
|
|
const scanner = scannerInstance;
|
|
|
|
/** Called by html5-qrcode when it decodes a QR. Runs once. */
|
|
const onDecode = async (decodedText: string) => {
|
|
// Stop the camera first, then verify
|
|
await stopCamera();
|
|
// Bail out if unmounted while camera was stopping
|
|
if (!isMountedRef.current) return;
|
|
setScanState("verifying");
|
|
try {
|
|
const result = await apiPost<CheckInResult>("/api/event/check-in", { qrText: decodedText });
|
|
// Bail out if unmounted while waiting for the API response
|
|
if (!isMountedRef.current) return;
|
|
if (result.status === "CHECKED_IN") {
|
|
setCheckedIn(true);
|
|
setEventName(result.eventName);
|
|
setScanState("success");
|
|
} else if (result.status === "ALREADY_CHECKED_IN") {
|
|
// Not an error — the member is already checked in today (at this or another booth).
|
|
setCheckedIn(true);
|
|
setEventName(result.eventName);
|
|
setScanState("already");
|
|
} else {
|
|
setFailureMsg(statusMessage(result.status));
|
|
setScanState("failure");
|
|
}
|
|
} catch (err) {
|
|
if (!isMountedRef.current) return;
|
|
if (err instanceof ApiError) {
|
|
setFailureMsg(err.message);
|
|
} else {
|
|
setFailureMsg("Check-in couldn't be completed. Please try again.");
|
|
}
|
|
setScanState("failure");
|
|
}
|
|
};
|
|
|
|
// Wrap getUserMedia for the duration of start() so every camera stream the
|
|
// library opens is captured (and can be force-released even if the library
|
|
// leaks it). Only this page uses the camera and starts are serialized, so
|
|
// the patch window is safe; it is restored in `finally`.
|
|
const md = typeof navigator !== "undefined" ? navigator.mediaDevices : undefined;
|
|
const originalGetUserMedia = md?.getUserMedia;
|
|
if (md && originalGetUserMedia) {
|
|
try {
|
|
md.getUserMedia = (constraints?: MediaStreamConstraints) =>
|
|
originalGetUserMedia.call(md, constraints).then((stream) => {
|
|
openedStreamsRef.current.push(stream);
|
|
return stream;
|
|
});
|
|
} catch {
|
|
// getUserMedia not assignable here — capture is best-effort.
|
|
}
|
|
}
|
|
|
|
try {
|
|
await scanner.start(
|
|
{ facingMode: "environment" },
|
|
{ fps: 10 },
|
|
onDecode,
|
|
() => { /* per-frame decode errors — ignore */ }
|
|
);
|
|
} catch (err) {
|
|
console.warn("[checkin] scanner.start() failed:", err instanceof Error ? err.name : err);
|
|
// The library rejects without stopping a track it already opened — release it.
|
|
releaseOpenedStreams();
|
|
if (isMountedRef.current) {
|
|
setScanState("camera_error");
|
|
setFailureMsg("Camera unavailable. Please allow camera access and try again.");
|
|
}
|
|
return;
|
|
} finally {
|
|
if (md && originalGetUserMedia) {
|
|
try { md.getUserMedia = originalGetUserMedia; } catch { /* ignore */ }
|
|
}
|
|
}
|
|
|
|
// The camera is now live, but the user may have left the Scan tab, the page
|
|
// may have unmounted, or a newer scan attempt may have started while
|
|
// start() was resolving. Stop the stream we just opened instead of
|
|
// advancing to "scanning" on an attempt nobody is watching anymore.
|
|
if (!isMountedRef.current || startGenerationRef.current !== myGeneration) {
|
|
await scanner.stop().catch(() => {});
|
|
releaseOpenedStreams();
|
|
return;
|
|
}
|
|
|
|
scannerRef.current = scanner;
|
|
stoppingRef.current = false;
|
|
setScanState("scanning");
|
|
}, [stopCamera, releaseOpenedStreams]);
|
|
|
|
// Auto-start: whenever the Scan tab is active and idle (initial mount with the
|
|
// Scan tab selected, or returning to it after a reset), begin capture without
|
|
// requiring an explicit "Tap to Scan". handleScan moves state off "idle"
|
|
// immediately, so this fires once per idle→active transition rather than
|
|
// looping; its own guards (insecure context, permission failure, generation
|
|
// counter) still apply, landing on camera_error/insecure when capture can't start.
|
|
useEffect(() => {
|
|
if (view === "scan" && scanState === "idle") {
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- handleScan synchronizes with an external system (the device camera); its setState("starting") is the intended "subscribe to an external source" effect use, and the idle-only guard fires it once per transition, not in a cascade.
|
|
void handleScan();
|
|
}
|
|
}, [view, scanState, handleScan]);
|
|
|
|
/** Switch tabs — stop camera if leaving Scan while it is live or verifying. */
|
|
const handleTabSwitch = useCallback(async (next: ViewMode) => {
|
|
if (next !== "scan") {
|
|
if (scanState === "scanning" || scanState === "starting") {
|
|
startGenerationRef.current++;
|
|
await stopCamera();
|
|
setScanState("idle");
|
|
setFailureMsg("");
|
|
setEventName(undefined);
|
|
} else if (scanState === "verifying") {
|
|
// Camera is already stopped during verifying — only reset state
|
|
setScanState("idle");
|
|
setFailureMsg("");
|
|
setEventName(undefined);
|
|
}
|
|
}
|
|
setView(next);
|
|
}, [scanState, stopCamera]);
|
|
|
|
/** Return to idle so the member can retry. */
|
|
const handleScanAgain = useCallback(async () => {
|
|
await stopCamera();
|
|
setScanState("idle");
|
|
setFailureMsg("");
|
|
}, [stopCamera]);
|
|
|
|
const handleCopy = () => {
|
|
navigator.clipboard.writeText(memberCode).catch(() => {});
|
|
setCopied(true);
|
|
setTimeout(() => setCopied(false), 2000);
|
|
};
|
|
|
|
const t = {
|
|
text: isDark ? "#FFFFFF" : "#1a1a1a",
|
|
textSec: isDark ? "rgba(255,255,255,0.8)" : "#5C5C5C",
|
|
textMuted: isDark ? "rgba(255,255,255,0.6)" : "#6B6B6B",
|
|
card: isDark ? "rgba(255,255,255,0.06)" : "#FFFFFF",
|
|
border: isDark ? "rgba(255,255,255,0.08)" : "rgba(0,0,0,0.07)",
|
|
shadow: isDark ? "0 4px 24px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.06)"
|
|
: "0 2px 16px rgba(0,0,0,0.07), 0 1px 3px rgba(0,0,0,0.04)",
|
|
divider: isDark ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.06)",
|
|
scanBg: isDark ? "rgba(0,0,0,0.85)" : "rgba(10,10,10,0.9)",
|
|
};
|
|
|
|
const TABS: { id: ViewMode; label: string }[] = [
|
|
{ id: "scan", label: "Scan QR" },
|
|
{ id: "badge", label: "My Badge" },
|
|
];
|
|
|
|
const isLiveCamera = scanState === "scanning" || scanState === "starting";
|
|
// A retry (manual restart) control is shown whenever an attempt has ended and
|
|
// won't auto-resume: an outright failure, or a camera/context problem.
|
|
const showRetry =
|
|
scanState === "failure" || scanState === "camera_error" || scanState === "insecure";
|
|
|
|
return (
|
|
<div className="min-h-full" style={{ background: "var(--bg-primary)" }}>
|
|
<motion.div
|
|
variants={stagger}
|
|
initial="hidden"
|
|
animate="visible"
|
|
className="px-4 pt-5 pb-6 space-y-4"
|
|
>
|
|
|
|
{/* ── 1. Title ───────────────────────────────────────────── */}
|
|
<motion.div variants={fadeUp}>
|
|
<h1 className="text-xl font-extrabold tracking-tight" style={{ color: t.text }}>
|
|
Event <span style={{ color: "var(--orange-text)" }}>Check-In</span>
|
|
</h1>
|
|
<p className="text-xs mt-0.5" style={{ color: t.textMuted }}>
|
|
Show your badge or scan a QR code at the entrance
|
|
</p>
|
|
</motion.div>
|
|
|
|
{/* ── 2. Tabs ────────────────────────────────────────────── */}
|
|
<motion.div
|
|
variants={fadeUp}
|
|
className="flex gap-1.5 p-1 rounded-2xl"
|
|
style={{ background: t.card, border: `1px solid ${t.border}`, boxShadow: t.shadow }}
|
|
>
|
|
{TABS.map(({ id, label }) => (
|
|
<motion.button
|
|
key={id}
|
|
onClick={() => void handleTabSwitch(id)}
|
|
whileTap={{ scale: 0.96 }}
|
|
transition={{ type: "spring", stiffness: 450, damping: 28 }}
|
|
className="relative flex-1 py-2.5 rounded-xl text-xs font-bold tracking-wide"
|
|
>
|
|
{view === id && (
|
|
<motion.div
|
|
layoutId="tab-pill"
|
|
className="absolute inset-0 rounded-xl"
|
|
style={{
|
|
background: "linear-gradient(135deg, #C94D00 0%, #A83F00 100%)",
|
|
boxShadow: "0 4px 12px rgba(255,107,0,0.35)",
|
|
}}
|
|
transition={{ type: "spring", stiffness: 420, damping: 30 }}
|
|
/>
|
|
)}
|
|
<span className="relative" style={{ color: view === id ? "#fff" : t.textMuted }}>
|
|
{label}
|
|
</span>
|
|
</motion.button>
|
|
))}
|
|
</motion.div>
|
|
|
|
{/* ── 3. Tab content ─────────────────────────────────────── */}
|
|
<AnimatePresence mode="wait">
|
|
|
|
{/* ══ SCAN TAB ══════════════════════════════════════════ */}
|
|
{view === "scan" && (
|
|
<motion.div
|
|
key="scan"
|
|
initial={{ opacity: 0, x: -16 }}
|
|
animate={{ opacity: 1, x: 0 }}
|
|
exit={{ opacity: 0, x: 16 }}
|
|
transition={{ type: "spring", stiffness: 320, damping: 28 }}
|
|
className="space-y-4"
|
|
>
|
|
{/* Scanner viewport */}
|
|
<div
|
|
className="relative w-full aspect-square rounded-3xl overflow-hidden"
|
|
style={{ background: t.scanBg, border: `1px solid rgba(255,255,255,0.08)` }}
|
|
>
|
|
{/* Ambient glow */}
|
|
<div className="absolute inset-0 pointer-events-none"
|
|
style={{ background: "radial-gradient(ellipse at center, rgba(255,107,0,0.06) 0%, transparent 70%)" }} />
|
|
|
|
{/* Corner brackets */}
|
|
{[
|
|
"top-5 left-5 border-t-2 border-l-2",
|
|
"top-5 right-5 border-t-2 border-r-2",
|
|
"bottom-5 left-5 border-b-2 border-l-2",
|
|
"bottom-5 right-5 border-b-2 border-r-2",
|
|
].map((cls, i) => (
|
|
<motion.div
|
|
key={i}
|
|
initial={{ opacity: 0, scale: 0.85 }}
|
|
animate={{ opacity: 1, scale: 1 }}
|
|
transition={{ delay: 0.1 + i * 0.05 }}
|
|
className={`absolute w-9 h-9 border-tastefullyOrange rounded-sm ${cls}`}
|
|
/>
|
|
))}
|
|
|
|
{/* Scan line (shown while camera is live) */}
|
|
{isLiveCamera && (
|
|
<motion.div
|
|
initial={{ y: "0%" }}
|
|
animate={{ y: "100%" }}
|
|
transition={{ duration: 1.3, repeat: Infinity, ease: "linear" }}
|
|
className="absolute left-5 right-5 h-0.5 rounded-full"
|
|
style={{
|
|
background: "linear-gradient(90deg, transparent, #FF6B00, transparent)",
|
|
boxShadow: "0 0 16px rgba(255,107,0,0.9)",
|
|
}}
|
|
/>
|
|
)}
|
|
|
|
{/* Live camera feed container — html5-qrcode mounts video here */}
|
|
{isLiveCamera && (
|
|
<div
|
|
id="checkin-reader"
|
|
className="absolute inset-0 w-full h-full"
|
|
style={{ zIndex: 1 }}
|
|
/>
|
|
)}
|
|
|
|
{/* Center overlay */}
|
|
<div className="absolute inset-0 flex flex-col items-center justify-center gap-3" style={{ zIndex: 2 }}>
|
|
{/* ── Success ── */}
|
|
{scanState === "success" && (
|
|
<motion.div
|
|
initial={{ scale: 0 }}
|
|
animate={{ scale: 1 }}
|
|
transition={{ type: "spring", stiffness: 380, damping: 22 }}
|
|
className="flex flex-col items-center gap-2"
|
|
>
|
|
<div className="w-20 h-20 rounded-full flex items-center justify-center"
|
|
style={{ background: "rgba(34,197,94,0.15)", border: "2px solid rgba(34,197,94,0.4)" }}>
|
|
<CheckCircle2 strokeWidth={2} className="w-10 h-10 stroke-emerald-400" />
|
|
</div>
|
|
<span className="text-sm font-bold text-emerald-400">Checked In!</span>
|
|
{eventName && (
|
|
<span className="text-xs text-center px-4" style={{ color: "rgba(255,255,255,0.6)" }}>
|
|
{eventName}
|
|
</span>
|
|
)}
|
|
</motion.div>
|
|
)}
|
|
|
|
{/* ── Already checked in (non-error) ── */}
|
|
{scanState === "already" && (
|
|
<motion.div
|
|
initial={{ scale: 0.9, opacity: 0 }}
|
|
animate={{ scale: 1, opacity: 1 }}
|
|
transition={{ type: "spring", stiffness: 340, damping: 24 }}
|
|
className="flex flex-col items-center gap-2 px-6 text-center"
|
|
>
|
|
<div className="w-20 h-20 rounded-full flex items-center justify-center"
|
|
style={{ background: "rgba(255,159,10,0.15)", border: "2px solid rgba(255,159,10,0.4)" }}>
|
|
<CheckCircle2 strokeWidth={2} className="w-10 h-10" style={{ stroke: "#ffb03a" }} />
|
|
</div>
|
|
<span className="text-sm font-bold" style={{ color: "#ffb03a" }}>Already Checked In</span>
|
|
{eventName && (
|
|
<span className="text-xs" style={{ color: "rgba(255,255,255,0.6)" }}>{eventName}</span>
|
|
)}
|
|
<span className="text-xs" style={{ color: "rgba(255,255,255,0.5)" }}>
|
|
You've already checked in today
|
|
</span>
|
|
</motion.div>
|
|
)}
|
|
|
|
{/* ── Failure / error ── */}
|
|
{(scanState === "failure" || scanState === "camera_error" || scanState === "insecure") && (
|
|
<motion.div
|
|
initial={{ scale: 0.9, opacity: 0 }}
|
|
animate={{ scale: 1, opacity: 1 }}
|
|
transition={{ type: "spring", stiffness: 320, damping: 26 }}
|
|
className="flex flex-col items-center gap-3 px-6 text-center"
|
|
>
|
|
<div className="w-16 h-16 rounded-full flex items-center justify-center"
|
|
style={{ background: "rgba(239,68,68,0.15)", border: "2px solid rgba(239,68,68,0.35)" }}>
|
|
<AlertCircle strokeWidth={2} className="w-8 h-8" style={{ stroke: "#f87171" }} />
|
|
</div>
|
|
<span className="text-sm font-semibold" style={{ color: "#f87171" }}>
|
|
{scanState === "insecure"
|
|
? "Scanning needs a secure (https) connection"
|
|
: failureMsg}
|
|
</span>
|
|
{(scanState === "camera_error" || scanState === "insecure") && (
|
|
<span className="text-xs" style={{ color: "rgba(255,255,255,0.5)" }}>
|
|
Use your <strong style={{ color: "rgba(255,255,255,0.75)" }}>My Badge</strong> tab instead
|
|
</span>
|
|
)}
|
|
</motion.div>
|
|
)}
|
|
|
|
{/* ── Verifying ── */}
|
|
{scanState === "verifying" && (
|
|
<motion.div
|
|
animate={{ opacity: [1, 0.4, 1] }}
|
|
transition={{ repeat: Infinity, duration: 0.8 }}
|
|
className="flex flex-col items-center gap-2"
|
|
>
|
|
<motion.div animate={{ rotate: 360 }} transition={{ repeat: Infinity, duration: 0.9, ease: "linear" }}>
|
|
<RefreshCw strokeWidth={1.4} className="w-12 h-12" style={{ stroke: "rgba(255,255,255,0.5)" }} />
|
|
</motion.div>
|
|
<span className="text-xs font-medium" style={{ color: "rgba(255,255,255,0.5)" }}>
|
|
Verifying…
|
|
</span>
|
|
</motion.div>
|
|
)}
|
|
|
|
{/* ── Idle / Starting ── */}
|
|
{(scanState === "idle" || scanState === "starting") && (
|
|
<motion.div
|
|
animate={scanState === "starting" ? { opacity: [1, 0.4, 1] } : {}}
|
|
transition={{ repeat: Infinity, duration: 0.8 }}
|
|
className="flex flex-col items-center gap-2"
|
|
>
|
|
<ScanLine strokeWidth={1.4} className="w-20 h-20" style={{ stroke: "rgba(255,255,255,0.18)" }} />
|
|
<span className="text-xs font-medium" style={{ color: "rgba(255,255,255,0.6)" }}>
|
|
{scanState === "starting" ? "Starting camera…" : "Align QR code within frame"}
|
|
</span>
|
|
</motion.div>
|
|
)}
|
|
|
|
{/* ── Scanning (camera live — overlay is mostly transparent) ── */}
|
|
{scanState === "scanning" && (
|
|
<span className="text-xs font-medium" style={{ color: "rgba(255,255,255,0.6)" }}>
|
|
Scanning…
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Retry control — camera restarts automatically otherwise */}
|
|
{showRetry && (
|
|
<motion.button
|
|
onClick={() => void handleScanAgain()}
|
|
whileTap={{ scale: 0.97 }}
|
|
transition={{ type: "spring", stiffness: 420, damping: 26 }}
|
|
className="w-full py-4 rounded-2xl font-bold text-sm flex items-center justify-center gap-2.5"
|
|
style={{
|
|
background: "linear-gradient(135deg, #C94D00 0%, #A83F00 100%)",
|
|
color: "#fff",
|
|
boxShadow: "0 8px 24px rgba(255,107,0,0.4)",
|
|
}}
|
|
>
|
|
<RefreshCw strokeWidth={2} className="w-4 h-4" />
|
|
Scan again
|
|
</motion.button>
|
|
)}
|
|
|
|
{/* Status indicator — non-interactive; capture is automatic */}
|
|
{!showRetry && (
|
|
<motion.button
|
|
disabled
|
|
className="w-full py-4 rounded-2xl font-bold text-sm flex items-center justify-center gap-2.5 disabled:opacity-60"
|
|
style={{
|
|
background: scanState === "success" ? "rgba(34,197,94,0.12)"
|
|
: scanState === "already" ? "rgba(255,159,10,0.12)"
|
|
: "linear-gradient(135deg, #C94D00 0%, #A83F00 100%)",
|
|
border: scanState === "success" ? "1px solid rgba(34,197,94,0.3)"
|
|
: scanState === "already" ? "1px solid rgba(255,159,10,0.35)"
|
|
: "none",
|
|
color: scanState === "success" ? "#34d399"
|
|
: scanState === "already" ? "#ffb03a"
|
|
: "#fff",
|
|
boxShadow: (scanState === "success" || scanState === "already") ? "none" : "0 8px 24px rgba(255,107,0,0.4)",
|
|
}}
|
|
>
|
|
{scanState === "success" ? (
|
|
<><CheckCircle2 strokeWidth={2} className="w-4 h-4" /> Check-In Confirmed</>
|
|
) : scanState === "already" ? (
|
|
<><CheckCircle2 strokeWidth={2} className="w-4 h-4" /> Already Checked In</>
|
|
) : (
|
|
<><motion.div animate={{ rotate: 360 }} transition={{ repeat: Infinity, duration: 0.9, ease: "linear" }}>
|
|
<RefreshCw strokeWidth={2} className="w-4 h-4" />
|
|
</motion.div> Scanning…</>
|
|
)}
|
|
</motion.button>
|
|
)}
|
|
</motion.div>
|
|
)}
|
|
|
|
{/* ══ BADGE TAB ══════════════════════════════════════════ */}
|
|
{view === "badge" && (
|
|
<motion.div
|
|
key="badge"
|
|
initial={{ opacity: 0, x: 16 }}
|
|
animate={{ opacity: 1, x: 0 }}
|
|
exit={{ opacity: 0, x: -16 }}
|
|
transition={{ type: "spring", stiffness: 320, damping: 28 }}
|
|
className="space-y-4"
|
|
>
|
|
|
|
{/* ── Membership Card ────────────────────────────────── */}
|
|
<div
|
|
className="relative rounded-3xl overflow-hidden"
|
|
style={{
|
|
background: "linear-gradient(135deg, #FF6B00 0%, #c94d00 45%, #e86000 70%, #ff9e4d 100%)",
|
|
boxShadow: "0 16px 48px rgba(255,107,0,0.45), 0 4px 12px rgba(0,0,0,0.2)",
|
|
}}
|
|
>
|
|
{/* Light shimmer overlay */}
|
|
<div className="absolute inset-0 pointer-events-none"
|
|
style={{ background: "radial-gradient(ellipse at 20% 20%, rgba(255,255,255,0.22) 0%, transparent 55%)" }} />
|
|
{/* Decorative circles */}
|
|
<div className="absolute -right-8 -top-8 w-32 h-32 rounded-full opacity-20"
|
|
style={{ background: "rgba(255,255,255,0.3)" }} />
|
|
<div className="absolute -right-4 -bottom-4 w-24 h-24 rounded-full opacity-15"
|
|
style={{ background: "rgba(255,255,255,0.2)" }} />
|
|
|
|
<div className="relative p-5">
|
|
{/* Top row: brand + status */}
|
|
<div className="flex items-start justify-between mb-5">
|
|
<div>
|
|
<p className="text-xs font-black tracking-[0.2em] uppercase text-white/70 mb-0.5">
|
|
Tastefully Events
|
|
</p>
|
|
</div>
|
|
<motion.div
|
|
animate={checkedIn ? { scale: [1, 1.08, 1] } : {}}
|
|
transition={{ type: "spring" }}
|
|
className="px-3 py-1.5 rounded-xl text-xs font-bold"
|
|
style={{
|
|
background: checkedIn ? "rgba(34,197,94,0.2)" : "rgba(255,255,255,0.2)",
|
|
border: checkedIn ? "1px solid rgba(34,197,94,0.5)" : "1px solid rgba(255,255,255,0.35)",
|
|
color: checkedIn ? "#a3e635" : "rgba(255,255,255,0.95)",
|
|
backdropFilter: "blur(8px)",
|
|
}}
|
|
>
|
|
{checkedIn ? "✓ Checked In" : "Pending"}
|
|
</motion.div>
|
|
</div>
|
|
|
|
{/* QR code */}
|
|
<div className="flex justify-center mb-5">
|
|
<motion.div
|
|
animate={checkedIn ? { scale: [1, 1.04, 1] } : {}}
|
|
transition={{ type: "spring", stiffness: 300 }}
|
|
className="relative p-3 rounded-2xl bg-white"
|
|
style={{
|
|
boxShadow: checkedIn
|
|
? "0 0 0 3px rgba(163,230,53,0.5), 0 8px 24px rgba(0,0,0,0.2)"
|
|
: "0 8px 24px rgba(0,0,0,0.2)",
|
|
}}
|
|
>
|
|
<MemberQR value={memberCode} />
|
|
{checkedIn && (
|
|
<motion.div
|
|
initial={{ opacity: 0, scale: 0 }}
|
|
animate={{ opacity: 1, scale: 1 }}
|
|
transition={{ type: "spring", stiffness: 380, damping: 22 }}
|
|
className="absolute inset-0 flex items-center justify-center rounded-2xl"
|
|
style={{ background: "rgba(34,197,94,0.85)", backdropFilter: "blur(4px)" }}
|
|
>
|
|
<CheckCircle2 strokeWidth={2} className="w-10 h-10 stroke-white" />
|
|
</motion.div>
|
|
)}
|
|
</motion.div>
|
|
</div>
|
|
|
|
{/* Member name */}
|
|
<div className="text-center mb-1">
|
|
<h2 className="text-2xl font-extrabold text-white tracking-tight leading-tight uppercase">
|
|
{member?.name ?? "—"}
|
|
</h2>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Footer */}
|
|
<div className="relative px-5 py-3 flex items-center justify-between"
|
|
style={{ borderTop: "1px solid rgba(255,255,255,0.15)", background: "rgba(0,0,0,0.12)" }}>
|
|
<div>
|
|
<p className="text-xs font-bold tracking-widest uppercase text-white/50 mb-0.5">Member ID</p>
|
|
<p className="text-xs font-mono font-bold text-white/90">{memberCode}</p>
|
|
</div>
|
|
<motion.button
|
|
onClick={handleCopy}
|
|
whileTap={{ scale: 0.9 }}
|
|
className="flex items-center gap-1.5 px-3 py-1.5 rounded-xl text-xs font-bold"
|
|
style={{ background: "rgba(255,255,255,0.15)", color: "rgba(255,255,255,0.85)", backdropFilter: "blur(8px)" }}
|
|
>
|
|
<Copy strokeWidth={2} className="w-3 h-3" />
|
|
{copied ? "Copied!" : "Copy"}
|
|
</motion.button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* ── Loyalty Points ─────────────────────────────────── */}
|
|
<motion.div
|
|
initial={{ opacity: 0, y: 12 }}
|
|
animate={{ opacity: 1, y: 0 }}
|
|
transition={{ delay: 0.1, type: "spring", stiffness: 320, damping: 26 }}
|
|
className="rounded-2xl p-4"
|
|
style={{ background: t.card, border: `1px solid ${t.border}`, boxShadow: t.shadow }}
|
|
>
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2">
|
|
<div className="w-8 h-8 rounded-xl flex items-center justify-center"
|
|
style={{ background: "rgba(255,107,0,0.12)", border: "1px solid rgba(255,107,0,0.2)" }}>
|
|
<Coins strokeWidth={2} className="w-4 h-4 stroke-tastefullyOrange" />
|
|
</div>
|
|
<div>
|
|
<p className="text-xs font-bold tracking-widest uppercase" style={{ color: t.textMuted }}>
|
|
Loyalty Points
|
|
</p>
|
|
<p className="text-xl font-extrabold leading-none" style={{ color: "var(--orange-text)" }}>
|
|
{balance.toLocaleString()}
|
|
<span className="text-xs font-semibold ml-1" style={{ color: t.textMuted }}>pts</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div className="text-right">
|
|
<p className="text-xs font-bold tracking-widest uppercase" style={{ color: t.textMuted }}>
|
|
Earned Today
|
|
</p>
|
|
<p className="text-xl font-extrabold leading-none" style={{ color: t.text }}>
|
|
+{earnedToday.toLocaleString()}
|
|
<span className="text-xs font-semibold ml-1" style={{ color: t.textMuted }}>pts</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</motion.div>
|
|
|
|
</motion.div>
|
|
)}
|
|
</AnimatePresence>
|
|
</motion.div>
|
|
</div>
|
|
);
|
|
}
|