import React, { useEffect, useState } from "react"; import { Shield, ShieldAlert, RefreshCw, Wifi, Power } from "lucide-react"; interface Props { children: React.ReactNode; } const VpnGuard: React.FC = ({ children }) => { const VPN_API = "http://10.0.0.1"; const DEFAULT_API = "https://portal-sf.docva.com" const [checking, setChecking] = useState(true); const [vpnConnected, setVpnConnected] = useState(true); const [isRetrying, setIsRetrying] = useState(false); const checkVPN = async () => { try { setChecking(true); const res = await window.electron?.checkRegion(); console.log("VpnGuard", res); if (res.status === 200) { setVpnConnected(true); } else { setVpnConnected(false); return; } // setVpnConnected(res?.success === true); } catch (error) { console.error("VPN check failed:", error); setVpnConnected(false); } finally { setChecking(false); setIsRetrying(false); } }; useEffect(() => { checkVPN(); }, []); const connectVPN = async () => { setChecking(true); try { const res = await window.electron.connectVPN(); console.log("connectVPN result → ", res); // Only proceed if VPN actually connected if (res === "Windows VPN connected silently") { const vpnRes = await window.electron.updateBaseURL(VPN_API); console.log("vpnRes", vpnRes); if (vpnRes) { // Run VPN check again AFTER URL changes await checkVPN(); } else { console.warn("Base URL updated but health check failed"); } } } catch (error) { console.log(error); const vpnRes = await window.electron.updateBaseURL(VPN_API); console.log("vpnRes", vpnRes); if (vpnRes) { // Run VPN check again AFTER URL changes await checkVPN(); } else { console.warn("Base URL updated but health check failed"); } } finally { setChecking(false); } }; const disconnectVPN = async () => { setChecking(true); try { const res = await window.electron.disconnectVPN();; console.log("disconnectVPN →", res); // Restore original base URL const baseRes = await window.electron.updateBaseURL(DEFAULT_API); console.log("baseRes disconnect →", baseRes); setVpnConnected(true); // await checkVPN(); } catch (error) { console.log("Disconnect Error:", error); } finally { setChecking(false); } }; // Checking state if (checking && !isRetrying) { return (

Verifying Connection

Checking VPN status...

); } // VPN not connected state if (!vpnConnected) { return (
{/* Icon */}

VPN Required

Please connect to your company VPN to access this application.

{/* Connect VPN */} {/* Disconnect VPN (Disabled when NOT connected) */}

Need help? Contact IT support at{" "} support@docva.com

); } return ( <> {children} ); }; export default VpnGuard;