Files
co-lite/solvway-admin-portal/app/components/registration/QrCodeItem.tsx
T

73 lines
2.5 KiB
TypeScript

"use client";
import { useRef, useState } from "react";
import { QRCodeCanvas, QRCodeSVG } from "qrcode.react";
import { Copy, Download } from "lucide-react";
import { Button } from "@/app/components/ui/button";
import { Switch } from "@/app/components/ui/switch";
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,
eventName,
fileLabel,
token,
enabled,
onToggle,
prefix = "r",
}: {
label: 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";
}) {
const t = useT();
const canvas = useRef<HTMLCanvasElement>(null);
const [copied, setCopied] = useState(false);
const link = `${window.location.origin}/${prefix}/${token}`;
async function copy() {
await navigator.clipboard.writeText(link);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
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="flex flex-col gap-3 rounded-lg border p-4 sm:flex-row sm:items-center">
<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">
<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>
{onToggle && <Switch checked={enabled} onCheckedChange={onToggle} aria-label={t("QrEnabled")} />}
</div>
</div>
</div>
);
}