import QRCode from 'qrcode.react'; import { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import './style.css'; export const Card = ({ direction = 'ltr' }) => { const firstLoad = useRef(true); const [qrvalue, setQrvalue] = useState(''); const [network, setNetwork] = useState({ ssid: '', encryptionMode: 'WPA', password: '', hidePassword: false, }); const [portrait, setPortrait] = useState(false); const { t } = useTranslation(); const escape = (v) => { const needsEscape = ['"', ';', ',', ':', '\\']; let escaped = ''; for (const c of v) { if (needsEscape.includes(c)) { escaped += `\\${c}`; } else { escaped += c; } } return escaped; }; const onPrint = () => { if (network.ssid.length > 0) { if (network.password.length < 8 && network.encryptionMode === 'WPA') { alert(t('wifi.alert.password.length.8')); } else if ( network.password.length < 5 && network.encryptionMode === 'WEP' ) { alert(t('wifi.alert.password.length.5')); } else { document.title = 'WiFi Card - ' + network.ssid; window.print(); } } else { alert(t('wifi.alert.name')); } }; const disableHidePassword = () => { const isWEPWithPasswordLengthShorterThat5Characters = () => { return network.encryptionMode === 'WEP' && network.password.length < 5 ? true : false; }; return network.encryptionMode === 'WPA' && network.password.length < 8 ? true : isWEPWithPasswordLengthShorterThat5Characters(); }; useEffect(() => { if (firstLoad.current && window.innerWidth < 500) { firstLoad.current = false; setPortrait(true); } const ssid = escape(network.ssid); const password = escape(network.password); setQrvalue(`WIFI:T:${network.encryptionMode};S:${ssid};P:${password};;`); }, [network]); return (