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(() => {
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) {

View File

@ -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<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(() => {
setWindowSize(getWindowSize(theme.typography.fontSize));
}, [theme.typography.fontSize]);

View File

@ -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<string>) => void,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
resolve: (value: any | PromiseLike<any>) => void,
reject?: (reason?: string) => void,
) => void = (uuid, resolve, reject) => {
this.promises[uuid] = {

View File

@ -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)
}
}
}