"use client"; import { useState, useEffect } from "react"; import { Wifi, Copy, Check, AlertCircle } from "lucide-react"; import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { useClientData } from "@/lib/hooks/useClientData"; export default function WifiCard() { const { wifiName, wifiPassword, loading } = useClientData(); const [copied, setCopied] = useState(false); const [error, setError] = useState(null); // Fonction de copie avec fallback pour les navigateurs qui ne supportent pas l'API Clipboard const copyToClipboard = async (text: string): Promise => { // Méthode moderne (nécessite HTTPS ou localhost) if (navigator.clipboard && window.isSecureContext) { try { await navigator.clipboard.writeText(text); return true; } catch (err) { console.error("Erreur avec l'API Clipboard:", err); } } // Fallback pour les navigateurs plus anciens ou contextes non sécurisés try { const textArea = document.createElement("textarea"); textArea.value = text; textArea.style.position = "fixed"; textArea.style.left = "-999999px"; textArea.style.top = "-999999px"; document.body.appendChild(textArea); textArea.focus(); textArea.select(); const successful = document.execCommand("copy"); document.body.removeChild(textArea); if (successful) { return true; } else { throw new Error("La commande copy a échoué"); } } catch (err) { console.error("Erreur avec la méthode fallback:", err); return false; } }; const handleCopyPassword = async () => { if (!wifiPassword || wifiPassword.trim() === "") { setError("Le mot de passe WiFi n'est pas disponible"); setTimeout(() => setError(null), 3000); return; } setError(null); const success = await copyToClipboard(wifiPassword); if (success) { setCopied(true); setTimeout(() => setCopied(false), 2000); } else { setError("Impossible de copier. Veuillez sélectionner manuellement le mot de passe ci-dessous."); setTimeout(() => setError(null), 5000); } }; // Afficher le mot de passe en cas d'échec de la copie const showPasswordFallback = error && error.includes("sélectionner manuellement"); return ( Connexion WiFi

Nom du réseau

{wifiName || "Chargement..."}

{showPasswordFallback && wifiPassword && (

{wifiPassword}

)} {error && !showPasswordFallback && (

{error}

)}
); }