Tor state

This commit is contained in:
koalasat
2025-07-16 17:28:00 +02:00
parent 3e79d8c5d1
commit 023992a548
4 changed files with 35 additions and 13 deletions

View File

@ -13,8 +13,8 @@ const Routes: React.FC = () => {
useEffect(() => { useEffect(() => {
window.addEventListener('navigateToPage', (event) => { window.addEventListener('navigateToPage', (event) => {
const orderId: string = event?.detail?.order_id; const orderId: string = event?.order_id;
const coordinator: string = event?.detail?.coordinator; const coordinator: string = event?.coordinator;
if (orderId && coordinator) { if (orderId && coordinator) {
const slot = garage.getSlotByOrder(coordinator, parseInt(orderId, 10)); const slot = garage.getSlotByOrder(coordinator, parseInt(orderId, 10));
if (slot?.token) { if (slot?.token) {

View File

@ -9,6 +9,7 @@ import React, {
import { type Page } from '../basic/NavBar'; import { type Page } from '../basic/NavBar';
import { type OpenDialogs } from '../basic/MainDialogs'; import { type OpenDialogs } from '../basic/MainDialogs';
import { ThemeProvider } from '@mui/material'; import { ThemeProvider } from '@mui/material';
import { v4 as uuidv4 } from 'uuid';
import { Settings, type Version, type Origin, type Favorites } from '../models'; import { Settings, type Version, type Origin, type Favorites } from '../models';
@ -160,7 +161,7 @@ export interface UseAppStoreType {
export const initialAppContext: UseAppStoreType = { export const initialAppContext: UseAppStoreType = {
theme: undefined, theme: undefined,
torStatus: 'ON', torStatus: 'OFF',
settings: getSettings(), settings: getSettings(),
setSettings: () => {}, setSettings: () => {},
page: entryPage, page: entryPage,
@ -224,15 +225,6 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): React
useEffect(() => { useEffect(() => {
setSettings(getSettings()); setSettings(getSettings());
void i18n.changeLanguage(settings.language); 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(() => { useEffect(() => {
@ -247,6 +239,24 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): React
}; };
}, []); }, []);
useEffect(() => {
const getTorstaus = () => {
new Promise<TorStatus>((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(() => { useEffect(() => {
setWindowSize(getWindowSize(theme.typography.fontSize)); setWindowSize(getWindowSize(theme.typography.fontSize));
}, [theme.typography.fontSize]); }, [theme.typography.fontSize]);

View File

@ -10,6 +10,7 @@ interface AndroidAppRobosats {
generateRoboname: (uuid: string, initialString: string) => void; generateRoboname: (uuid: string, initialString: string) => void;
generateRobohash: (uuid: string, initialString: string) => void; generateRobohash: (uuid: string, initialString: string) => void;
copyToClipboard: (value: string) => void; copyToClipboard: (value: string) => void;
getTorStatus: (uuid: string) => void;
} }
class AndroidRobosats { class AndroidRobosats {
@ -23,7 +24,8 @@ class AndroidRobosats {
public storePromise: ( public storePromise: (
uuid: string, uuid: string,
resolve: (value: string | PromiseLike<string>) => void, // eslint-disable-next-line @typescript-eslint/no-explicit-any
resolve: (value: any | PromiseLike<any>) => void,
reject?: (reason?: string) => void, reject?: (reason?: string) => void,
) => void = (uuid, resolve, reject) => { ) => void = (uuid, resolve, reject) => {
this.promises[uuid] = { this.promises[uuid] = {

View File

@ -5,6 +5,7 @@ import android.util.Log
import android.webkit.JavascriptInterface import android.webkit.JavascriptInterface
import android.webkit.WebView import android.webkit.WebView
import android.widget.Toast import android.widget.Toast
import com.robosats.tor.TorKmpManager.getTorKmpObject
class WebAppInterface(private val context: Context, private val webView: WebView) { class WebAppInterface(private val context: Context, private val webView: WebView) {
private val TAG = "WebAppInterface" 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() 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)
}
}
} }