"use client"; import { useState, useEffect } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { QrCode, Copy, Check } from "lucide-react"; import { toast } from "sonner"; import QRCode from "qrcode"; export function NetworkQRCode() { const [open, setOpen] = useState(false); const [networkUrl, setNetworkUrl] = useState(""); const [qrCodeUrl, setQrCodeUrl] = useState(""); const [copied, setCopied] = useState(false); useEffect(() => { async function fetchNetworkUrl() { try { const response = await fetch("/api/network"); const data = await response.json(); setNetworkUrl(data.url); const qr = await QRCode.toDataURL(data.url, { width: 300, margin: 2, color: { dark: "#FCD66F", light: "#0a0a0a", }, }); setQrCodeUrl(qr); } catch (error) { console.error("Error fetching network URL:", error); } } if (open) { fetchNetworkUrl(); } }, [open]); const copyToClipboard = async () => { try { await navigator.clipboard.writeText(networkUrl); setCopied(true); toast.success("URL copied to clipboard"); setTimeout(() => setCopied(false), 2000); } catch { toast.error("Failed to copy URL"); } }; return ( <> setOpen(true)} size="icon" className="fixed bottom-6 right-6 h-14 w-14 rounded-full shadow-lg shadow-primary/20 z-50" title="Show Network QR Code" > Share on Network {qrCodeUrl && ( )} Scan this QR code or use the URL below to access from other devices on your network {copied ? ( ) : ( )} > ); }
Scan this QR code or use the URL below to access from other devices on your network