From de18fb0d4c8088711179ea8655e67683ddd848c2 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Tue, 7 Mar 2023 04:13:56 -0800 Subject: [PATCH] Fix unsafe/selfhosted alert --- frontend/src/components/UnsafeAlert.tsx | 34 ++++++++++++------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/frontend/src/components/UnsafeAlert.tsx b/frontend/src/components/UnsafeAlert.tsx index 81e93b9b..b682a3b2 100644 --- a/frontend/src/components/UnsafeAlert.tsx +++ b/frontend/src/components/UnsafeAlert.tsx @@ -5,10 +5,13 @@ import { Paper, Alert, AlertTitle, Button, Link } from '@mui/material'; import { getHost } from '../utils'; const UnsafeAlert = (): JSX.Element => { - const { settings, setSettings, windowSize } = useContext(AppContext); + const { windowSize } = useContext(AppContext); const { t } = useTranslation(); const [show, setShow] = useState(true); + const [unsafeClient, setUnsafeClient] = useState(false); + const [selfHostedClient, setSelfHostedClient] = useState(false); + // To do. Read from Coordinators Obj. const safe_urls = [ 'robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion', @@ -20,25 +23,20 @@ const UnsafeAlert = (): JSX.Element => { const checkClient = () => { const http = new XMLHttpRequest(); - const host = getHost(); - const unsafeClient = !safe_urls.includes(host); + const h = getHost(); + const unsafe = !safe_urls.includes(h); + let selfHosted = false; + try { - http.open('HEAD', `${location.protocol}//${host}/selfhosted`, false); + http.open('HEAD', `${location.protocol}//${h}/selfhosted`, false); http.send(); - setSettings({ - ...settings, - host, - unsafeClient, - selfhostedClient: http.status === 200, - }); + selfHosted = http.status === 200; } catch { - setSettings({ - ...settings, - host, - unsafeClient, - selfhostedClient: false, - }); + selfHosted = false; } + + setUnsafeClient(unsafe); + setSelfHostedClient(selfHosted); }; useEffect(() => { @@ -51,7 +49,7 @@ const UnsafeAlert = (): JSX.Element => { } // Show selfhosted notice - else if (settings.selfhostedClient) { + else if (selfHostedClient) { return (
@@ -75,7 +73,7 @@ const UnsafeAlert = (): JSX.Element => { } // Show unsafe alert - else if (settings.unsafeClient) { + else if (unsafeClient) { return ( {windowSize.width > 57 ? (