diff options
Diffstat (limited to 'src/App.js')
| -rw-r--r-- | src/App.js | 115 |
1 files changed, 97 insertions, 18 deletions
| @@ -1,38 +1,105 @@ | |||
| 1 | import { Button, Heading, Link, Pane, Paragraph } from 'evergreen-ui'; | 1 | import { Button, Heading, Link, Pane, Paragraph } from 'evergreen-ui'; |
| 2 | import React, { useEffect, useRef, useState } from 'react'; | 2 | import React, { useEffect, useRef, useState, useCallback } from 'react'; |
| 3 | import { useTranslation } from 'react-i18next'; | 3 | import { useTranslation } from 'react-i18next'; |
| 4 | import logo from '../src/images/wifi.png'; | 4 | import logo from '../src/images/wifi.png'; |
| 5 | import { Settings } from './components/Settings'; | 5 | import { Settings } from './components/Settings'; |
| 6 | import { WifiCard } from './components/WifiCard'; | 6 | import { WifiCard } from './components/WifiCard'; |
| 7 | import './style.css'; | 7 | import './style.css'; |
| 8 | import { Translations } from './translations'; | 8 | import { Translations } from './translations'; |
| 9 | import useHashParam from './components/useHashParam'; | ||
| 9 | 10 | ||
| 10 | function App() { | 11 | function App() { |
| 12 | const getHashSearchParams = (location) => { | ||
| 13 | const hash = location.hash.slice(1); | ||
| 14 | const [prefix, query] = hash.split('?'); | ||
| 15 | |||
| 16 | return [prefix, new URLSearchParams(query)]; | ||
| 17 | }; | ||
| 18 | const getHashParam = (key, location = window.location) => { | ||
| 19 | const [_, searchParams] = getHashSearchParams(location); | ||
| 20 | return searchParams.get(key); | ||
| 21 | }; | ||
| 22 | const setHashParam = (key, value, location = window.location) => { | ||
| 23 | const [prefix, searchParams] = getHashSearchParams(location); | ||
| 24 | |||
| 25 | if (typeof value === 'undefined') { | ||
| 26 | searchParams.delete(key); | ||
| 27 | } else { | ||
| 28 | searchParams.set(key, value); | ||
| 29 | } | ||
| 30 | |||
| 31 | const search = searchParams.toString(); | ||
| 32 | location.hash = search ? `${prefix}?${search}` : prefix; | ||
| 33 | }; | ||
| 34 | |||
| 35 | let pssid = getHashParam('ssid'); //params.get('ssid') || ''; | ||
| 36 | let ppassword = getHashParam('password') || ''; | ||
| 37 | let pencryptionMode = | ||
| 38 | getHashParam('encryptionMode') !== null | ||
| 39 | ? getHashParam('encryptionMode') | ||
| 40 | : 'WPA'; | ||
| 41 | let peapMethod = getHashParam('eapMethod') || 'PWD'; | ||
| 42 | let peapIdentity = getHashParam('eapIdentity') || ''; | ||
| 43 | let phidePassword = | ||
| 44 | getHashParam('hidePassword') === null | ||
| 45 | ? false | ||
| 46 | : getHashParam('hidePassword').toLowerCase() === 'true' | ||
| 47 | ? true | ||
| 48 | : false; | ||
| 49 | let phiddenSSID = | ||
| 50 | getHashParam('hiddenSSID') === null | ||
| 51 | ? false | ||
| 52 | : getHashParam('hiddenSSID').toLowerCase() === 'true' | ||
| 53 | ? true | ||
| 54 | : false; | ||
| 55 | let pportrait = | ||
| 56 | getHashParam('portrait') === null | ||
| 57 | ? false | ||
| 58 | : getHashParam('portrait').toLowerCase() === 'true' | ||
| 59 | ? true | ||
| 60 | : false || false; | ||
| 61 | let padditionalCards = getHashParam('additionalCards') || 1; | ||
| 62 | let phideTip = | ||
| 63 | getHashParam('hideTip') === null | ||
| 64 | ? false | ||
| 65 | : getHashParam('hideTip').toLowerCase() === 'true' | ||
| 66 | ? true | ||
| 67 | : false; | ||
| 68 | let planguage = | ||
| 69 | getHashParam('lng') === null || getHashParam('lng').toLowerCase() === '' | ||
| 70 | ? 'en-US' | ||
| 71 | : getHashParam('lng'); | ||
| 72 | |||
| 73 | // ######################## | ||
| 11 | const html = document.querySelector('html'); | 74 | const html = document.querySelector('html'); |
| 75 | |||
| 12 | const { t, i18n } = useTranslation(); | 76 | const { t, i18n } = useTranslation(); |
| 13 | const firstLoad = useRef(true); | 77 | const firstLoad = useRef(true); |
| 14 | const [settings, setSettings] = useState({ | 78 | const [settings, setSettings] = useState({ |
| 15 | // Network SSID name | 79 | // Network SSID name |
| 16 | ssid: '', | 80 | ssid: pssid, |
| 17 | // Network password | 81 | // Network password |
| 18 | password: '', | 82 | password: ppassword, |
| 19 | // Settings: Network encryption mode | 83 | // Settings: Network encryption mode |
| 20 | encryptionMode: 'WPA', | 84 | encryptionMode: pencryptionMode, |
| 21 | // Settings: EAP Method | 85 | // Settings: EAP Method |
| 22 | eapMethod: 'PWD', | 86 | eapMethod: peapMethod, |
| 23 | // Settings: EAP identity | 87 | // Settings: EAP identity |
| 24 | eapIdentity: '', | 88 | eapIdentity: peapIdentity, |
| 25 | // Settings: Hide password on the printed card | 89 | // Settings: Hide password on the printed card |
| 26 | hidePassword: false, | 90 | hidePassword: phidePassword, |
| 27 | // Settings: Mark your network as hidden SSID | 91 | // Settings: Mark your network as hidden SSID |
| 28 | hiddenSSID: false, | 92 | hiddenSSID: phiddenSSID, |
| 29 | // Settings: Portrait orientation | 93 | // Settings: Portrait orientation |
| 30 | portrait: false, | 94 | portrait: pportrait, |
| 31 | // Settings: Additional cards | 95 | // Settings: Additional cards |
| 32 | additionalCards: 0, | 96 | additionalCards: padditionalCards, |
| 33 | // Settings: Show tip (legend) on card | 97 | // Settings: Show tip (legend) on card |
| 34 | hideTip: false, | 98 | hideTip: phideTip, |
| 99 | // Display language | ||
| 100 | lng: planguage, | ||
| 35 | }); | 101 | }); |
| 102 | |||
| 36 | const [errors, setErrors] = useState({ | 103 | const [errors, setErrors] = useState({ |
| 37 | ssidError: '', | 104 | ssidError: '', |
| 38 | passwordError: '', | 105 | passwordError: '', |
| @@ -48,6 +115,8 @@ function App() { | |||
| 48 | const onChangeLanguage = (language) => { | 115 | const onChangeLanguage = (language) => { |
| 49 | html.style.direction = htmlDirection(language); | 116 | html.style.direction = htmlDirection(language); |
| 50 | i18n.changeLanguage(language); | 117 | i18n.changeLanguage(language); |
| 118 | |||
| 119 | setSettings({ ...settings, lng: language }); | ||
| 51 | }; | 120 | }; |
| 52 | 121 | ||
| 53 | const onPrint = () => { | 122 | const onPrint = () => { |
| @@ -126,7 +195,7 @@ function App() { | |||
| 126 | }; | 195 | }; |
| 127 | const onAdditionalCardsChange = (additionalCardsStr) => { | 196 | const onAdditionalCardsChange = (additionalCardsStr) => { |
| 128 | const amount = parseInt(additionalCardsStr); | 197 | const amount = parseInt(additionalCardsStr); |
| 129 | amount >= 0 && setSettings({ ...settings, additionalCards: amount }); | 198 | amount >= 1 && setSettings({ ...settings, additionalCards: amount }); |
| 130 | }; | 199 | }; |
| 131 | const onHideTipChange = (hideTip) => { | 200 | const onHideTipChange = (hideTip) => { |
| 132 | setSettings({ ...settings, hideTip }); | 201 | setSettings({ ...settings, hideTip }); |
| @@ -136,22 +205,34 @@ function App() { | |||
| 136 | firstLoad.current = false; | 205 | firstLoad.current = false; |
| 137 | }; | 206 | }; |
| 138 | 207 | ||
| 208 | const generateUrl = () => { | ||
| 209 | Object.entries(settings).map(([key, value]) => { | ||
| 210 | //console.log(key+" = " + value) | ||
| 211 | setHashParam(key, value); | ||
| 212 | if (key === 'ssid') setName(value); | ||
| 213 | return true; | ||
| 214 | }); | ||
| 215 | }; | ||
| 216 | const [name, setName] = useHashParam('name'); | ||
| 217 | |||
| 139 | useEffect(() => { | 218 | useEffect(() => { |
| 140 | // Ensure the page direction is set properly on first load | 219 | // Ensure the page direction is set properly on first load |
| 141 | if (htmlDirection() === 'rtl') { | 220 | if (htmlDirection() === 'rtl') { |
| 142 | html.style.direction = 'rtl'; | 221 | html.style.direction = 'rtl'; |
| 143 | } | 222 | } |
| 223 | generateUrl(); | ||
| 144 | }); | 224 | }); |
| 145 | 225 | ||
| 146 | return ( | 226 | return ( |
| 147 | <Pane> | 227 | <Pane> |
| 228 | Hello{' '} | ||
| 229 | {name ? name + '! You name is stored in hash params #️⃣' : 'visitor!'} | ||
| 148 | <Pane display="flex"> | 230 | <Pane display="flex"> |
| 149 | <img alt="icon" src={logo} width="32" height="32" /> | 231 | <img alt="icon" src={logo} width="32" height="32" /> |
| 150 | <Heading size={900} paddingRight={16} paddingLeft={16}> | 232 | <Heading size={900} paddingRight={16} paddingLeft={16}> |
| 151 | {t('title')} | 233 | {t('title')} |
| 152 | </Heading> | 234 | </Heading> |
| 153 | </Pane> | 235 | </Pane> |
| 154 | |||
| 155 | <Pane> | 236 | <Pane> |
| 156 | <Paragraph marginTop={12}>{t('desc.use')}</Paragraph> | 237 | <Paragraph marginTop={12}>{t('desc.use')}</Paragraph> |
| 157 | 238 | ||
| @@ -163,7 +244,6 @@ function App() { | |||
| 163 | . | 244 | . |
| 164 | </Paragraph> | 245 | </Paragraph> |
| 165 | </Pane> | 246 | </Pane> |
| 166 | |||
| 167 | <Pane> | 247 | <Pane> |
| 168 | <WifiCard | 248 | <WifiCard |
| 169 | settings={settings} | 249 | settings={settings} |
| @@ -175,7 +255,6 @@ function App() { | |||
| 175 | onPasswordChange={onPasswordChange} | 255 | onPasswordChange={onPasswordChange} |
| 176 | /> | 256 | /> |
| 177 | </Pane> | 257 | </Pane> |
| 178 | |||
| 179 | <Settings | 258 | <Settings |
| 180 | settings={settings} | 259 | settings={settings} |
| 181 | firstLoad={firstLoad} | 260 | firstLoad={firstLoad} |
| @@ -189,7 +268,6 @@ function App() { | |||
| 189 | onAdditionalCardsChange={onAdditionalCardsChange} | 268 | onAdditionalCardsChange={onAdditionalCardsChange} |
| 190 | onHideTipChange={onHideTipChange} | 269 | onHideTipChange={onHideTipChange} |
| 191 | /> | 270 | /> |
| 192 | |||
| 193 | <Button | 271 | <Button |
| 194 | id="print" | 272 | id="print" |
| 195 | appearance="primary" | 273 | appearance="primary" |
| @@ -200,9 +278,10 @@ function App() { | |||
| 200 | {t('button.print')} | 278 | {t('button.print')} |
| 201 | </Button> | 279 | </Button> |
| 202 | <Pane id="print-area"> | 280 | <Pane id="print-area"> |
| 203 | {settings.additionalCards >= 0 && | 281 | {settings.additionalCards >= 1 && |
| 204 | [...Array(settings.additionalCards + 1)].map((el, idx) => ( | 282 | [...Array(settings.additionalCards)].map((el, idx) => ( |
| 205 | <WifiCard | 283 | <WifiCard |
| 284 | keyid={idx} | ||
| 206 | key={`card-nr-${idx}`} | 285 | key={`card-nr-${idx}`} |
| 207 | settings={settings} | 286 | settings={settings} |
| 208 | ssidError={errors.ssidError} | 287 | ssidError={errors.ssidError} |
