From 023992a548df757bbc2c2b3e27708f923d829d01 Mon Sep 17 00:00:00 2001 From: koalasat Date: Wed, 16 Jul 2025 17:28:00 +0200 Subject: [PATCH] Tor state --- frontend/src/basic/Routes.tsx | 4 +-- frontend/src/contexts/AppContext.tsx | 30 ++++++++++++------- frontend/src/services/Android/index.ts | 4 ++- .../main/java/com/robosats/WebAppInterface.kt | 10 +++++++ 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/frontend/src/basic/Routes.tsx b/frontend/src/basic/Routes.tsx index d67026bc..b1cb8ab0 100644 --- a/frontend/src/basic/Routes.tsx +++ b/frontend/src/basic/Routes.tsx @@ -13,8 +13,8 @@ const Routes: React.FC = () => { useEffect(() => { window.addEventListener('navigateToPage', (event) => { - const orderId: string = event?.detail?.order_id; - const coordinator: string = event?.detail?.coordinator; + const orderId: string = event?.order_id; + const coordinator: string = event?.coordinator; if (orderId && coordinator) { const slot = garage.getSlotByOrder(coordinator, parseInt(orderId, 10)); if (slot?.token) { diff --git a/frontend/src/contexts/AppContext.tsx b/frontend/src/contexts/AppContext.tsx index c2ec91ea..1f3db1d4 100644 --- a/frontend/src/contexts/AppContext.tsx +++ b/frontend/src/contexts/AppContext.tsx @@ -9,6 +9,7 @@ import React, { import { type Page } from '../basic/NavBar'; import { type OpenDialogs } from '../basic/MainDialogs'; import { ThemeProvider } from '@mui/material'; +import { v4 as uuidv4 } from 'uuid'; import { Settings, type Version, type Origin, type Favorites } from '../models'; @@ -160,7 +161,7 @@ export interface UseAppStoreType { export const initialAppContext: UseAppStoreType = { theme: undefined, - torStatus: 'ON', + torStatus: 'OFF', settings: getSettings(), setSettings: () => {}, page: entryPage, @@ -224,15 +225,6 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): React useEffect(() => { setSettings(getSettings()); void i18n.changeLanguage(settings.language); - window.addEventListener('torStatus', (event) => { - // Trick to improve UX on Android webview: delay the "Connected to Tor" status by 5 secs to avoid long waits on the first request. - setTimeout( - () => { - setTorStatus(event?.detail); - }, - event?.detail === 'ON' ? 5000 : 0, - ); - }); }, []); useEffect(() => { @@ -247,6 +239,24 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): React }; }, []); + useEffect(() => { + const getTorstaus = () => { + new Promise((resolve, reject) => { + const uuid: string = uuidv4(); + window.AndroidAppRobosats?.getTorStatus(uuid); + window.AndroidRobosats?.storePromise(uuid, resolve, reject); + }).then((result) => { + setTorStatus(result); + }); + }; + + if (client === 'mobile') { + getTorstaus(); + const interval = setInterval(getTorstaus, 5000); + return () => clearInterval(interval); + } + }, []); + useEffect(() => { setWindowSize(getWindowSize(theme.typography.fontSize)); }, [theme.typography.fontSize]); diff --git a/frontend/src/services/Android/index.ts b/frontend/src/services/Android/index.ts index 2cc2cdd1..cb5fc712 100644 --- a/frontend/src/services/Android/index.ts +++ b/frontend/src/services/Android/index.ts @@ -10,6 +10,7 @@ interface AndroidAppRobosats { generateRoboname: (uuid: string, initialString: string) => void; generateRobohash: (uuid: string, initialString: string) => void; copyToClipboard: (value: string) => void; + getTorStatus: (uuid: string) => void; } class AndroidRobosats { @@ -23,7 +24,8 @@ class AndroidRobosats { public storePromise: ( uuid: string, - resolve: (value: string | PromiseLike) => void, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + resolve: (value: any | PromiseLike) => void, reject?: (reason?: string) => void, ) => void = (uuid, resolve, reject) => { this.promises[uuid] = { diff --git a/mobile_new/app/src/main/java/com/robosats/WebAppInterface.kt b/mobile_new/app/src/main/java/com/robosats/WebAppInterface.kt index 9e6c6f1a..59dd09f5 100644 --- a/mobile_new/app/src/main/java/com/robosats/WebAppInterface.kt +++ b/mobile_new/app/src/main/java/com/robosats/WebAppInterface.kt @@ -5,6 +5,7 @@ import android.util.Log import android.webkit.JavascriptInterface import android.webkit.WebView import android.widget.Toast +import com.robosats.tor.TorKmpManager.getTorKmpObject class WebAppInterface(private val context: Context, private val webView: WebView) { private val TAG = "WebAppInterface" @@ -79,4 +80,13 @@ class WebAppInterface(private val context: Context, private val webView: WebView Toast.makeText(context, "Failed to copy to clipboard", Toast.LENGTH_SHORT).show() } } + + @JavascriptInterface + fun getTorStatus(uuid: String) { + val torState = getTorKmpObject().torState.state.name + + webView.post { + webView.evaluateJavascript("javascript:window.AndroidRobosats.onResolvePromise('${uuid}', '${torState}')", null) + } + } }