"use client"; import { useRef, useState } from "react"; import { QRCodeCanvas, QRCodeSVG } from "qrcode.react"; import { Copy, Download, Maximize2, Share2 } from "lucide-react"; import { Button } from "@/app/components/ui/button"; import { Dialog, DialogContent } from "@/app/components/ui/dialog"; import { Switch } from "@/app/components/ui/switch"; import { cn } from "@/app/lib/utils"; import { useT } from "@/app/lib/i18n/MessagesProvider"; const slug = (s: string) => s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""); /** One registration code: QR, link, copy, PNG download and (when onToggle is given) an on/off switch. */ export function QrCodeItem({ label, className, eventName, fileLabel, token, enabled, onToggle, prefix = "r", personal = false, }: { /** Heading above the link; omit when the surrounding card already names the event. */ label?: string; className?: string; eventName: string; /** Member name for the download file name; omit for the event code. */ fileLabel?: string; token: string; enabled: boolean; onToggle?: (enabled: boolean) => void; /** Public path the code opens: r = registration (default), c = check-in. */ prefix?: "r" | "c"; /** A user's own code (My QR codes): adds Share and Show full screen for use at the booth. */ personal?: boolean; }) { const t = useT(); const canvas = useRef(null); const [copied, setCopied] = useState(false); const [copyFailed, setCopyFailed] = useState(false); const [full, setFull] = useState(false); const link = `${window.location.origin}/${prefix}/${token}`; async function copy() { try { await navigator.clipboard.writeText(link); setCopyFailed(false); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch { setCopied(false); setCopyFailed(true); } } async function share() { if (!navigator.share) return copy(); try { await navigator.share({ title: eventName, text: t("QrShareText", { event: eventName }), url: link }); } catch (err) { // Dismissing the share sheet is an AbortError; anything else falls back to copying. if ((err as { name?: string }).name !== "AbortError") return copy(); } } function download() { if (!canvas.current) return; const a = document.createElement("a"); a.href = canvas.current.toDataURL("image/png"); a.download = `${slug(eventName)}-${slug(fileLabel || "event")}-qr.png`; a.click(); } return (
{label &&

{label}

}

{link}

{personal && ( <> )} {onToggle && }
{copyFailed &&

{t("CopyFailed")}

}
{personal && ( {/* A light quiet zone around the QR, so it scans in dark mode too. */}
)}
); }