import { useState } from "react"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import QRCode from "qrcode.react"; export default function WifiQRGenerator() { // State hooks for network name, password, and encryption type const [ssid, setSsid] = useState(""); const [password, setPassword] = useState(""); const [encryption, setEncryption] = useState("WPA"); // Function to generate the properly formatted Wi-Fi string for QR code const generateWifiString = () => { // Escape special characters in input strings const esc = (str) => str.replace(/([\\;,:"])/g, '\\$1'); // Use "nopass" for no encryption, otherwise use the selected encryption type const enc = encryption === "nopass" ? "nopass" : encryption; // Only include password if encryption is not "nopass" const pwd = encryption === "nopass" ? "" : esc(password); // Return the encoded Wi-Fi config string return `WIFI:T:${enc};S:${esc(ssid)};P:${pwd};;`; }; // Check if enough information is provided to generate the QR code const canGenerate = ssid && (encryption === "nopass" || password); return (

Wi-Fi QR Code Generator

{/* Input for SSID */}
setSsid(e.target.value)} placeholder="Enter your Wi-Fi network name" />
{/* Input for password, disabled when encryption is "nopass" */}
setPassword(e.target.value)} placeholder="Enter your Wi-Fi password" disabled={encryption === "nopass"} />
{/* Dropdown for selecting encryption type */}
{/* Conditionally render QR code when data is valid */} {canGenerate && (
)} {/* Button to copy Wi-Fi string to clipboard */}
); }