Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
122 lines
4.6 KiB
TypeScript
122 lines
4.6 KiB
TypeScript
"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<HTMLCanvasElement>(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 (
|
|
<div className={cn("flex flex-col gap-3 rounded-lg border p-4 sm:flex-row sm:items-center", className)}>
|
|
<QRCodeSVG value={link} size={128} className="shrink-0" />
|
|
<QRCodeCanvas ref={canvas} value={link} size={512} className="hidden" />
|
|
<div className="min-w-0 flex-1 space-y-2">
|
|
{label && <p className="text-sm font-medium">{label}</p>}
|
|
<p className="break-all text-xs text-muted-foreground">{link}</p>
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<Button variant="outline" size="sm" onClick={copy}>
|
|
<Copy aria-hidden="true" className="size-4" />
|
|
{copied ? t("QrCopied") : t("QrCopy")}
|
|
</Button>
|
|
<Button variant="outline" size="sm" onClick={download}>
|
|
<Download aria-hidden="true" className="size-4" />
|
|
{t("QrDownload")}
|
|
</Button>
|
|
{personal && (
|
|
<>
|
|
<Button variant="outline" className="min-h-11" onClick={share}>
|
|
<Share2 aria-hidden="true" className="size-4" />
|
|
{t("QrShare")}
|
|
</Button>
|
|
<Button variant="outline" className="min-h-11" onClick={() => setFull(true)}>
|
|
<Maximize2 aria-hidden="true" className="size-4" />
|
|
{t("QrFullScreen")}
|
|
</Button>
|
|
</>
|
|
)}
|
|
{onToggle && <Switch checked={enabled} onCheckedChange={onToggle} aria-label={t("QrEnabled")} />}
|
|
</div>
|
|
{copyFailed && <p role="alert" className="text-sm text-destructive">{t("CopyFailed")}</p>}
|
|
</div>
|
|
{personal && (
|
|
<Dialog open={full} onOpenChange={setFull}>
|
|
<DialogContent aria-label={eventName} aria-describedby={undefined} className="max-w-none place-items-center border-0">
|
|
{/* A light quiet zone around the QR, so it scans in dark mode too. */}
|
|
<div className="bg-white p-4">
|
|
<QRCodeSVG value={link} size={512} style={{ width: "min(80vw, 80vh)", height: "min(80vw, 80vh)" }} />
|
|
</div>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|