BlueWallet/screen/settings/torSettings.js

110 lines
3 KiB
JavaScript
Raw Normal View History

2021-09-21 23:55:14 -04:00
import React, { useState, useEffect, useContext } from 'react';
2023-11-05 14:32:50 -04:00
import { View } from 'react-native';
2021-04-23 12:29:45 +01:00
import navigationStyle from '../../components/navigationStyle';
2021-09-21 23:55:14 -04:00
import { BlueButton, BlueCard, BlueListItem, BlueLoading, BlueSpacing20, BlueText, SafeBlueArea } from '../../BlueComponents';
2021-04-23 12:29:45 +01:00
import loc from '../../loc';
2021-09-21 23:55:14 -04:00
import { BlueStorageContext } from '../../blue_modules/storage-context';
import alert from '../../components/Alert';
import { isTorCapable } from '../../blue_modules/environment';
2021-04-23 12:29:45 +01:00
const torrific = isTorCapable ? require('../../blue_modules/torrific') : require('../../scripts/maccatalystpatches/torrific.js');
2021-04-23 12:29:45 +01:00
/*
TorSettings is not displayed in Settings menu if isTorCapable is false. No need to provide code protection.
*/
2021-04-23 12:29:45 +01:00
const TorSettings = () => {
const [isLoading, setIsLoading] = useState(false);
const [daemonStatus, setDaemonStatus] = useState('');
2021-09-22 12:07:13 -04:00
const { isTorDisabled, setIsTorDisabled } = useContext(BlueStorageContext);
2021-04-23 12:29:45 +01:00
const updateStatus = async () => {
const status = await torrific.getDaemonStatus();
setDaemonStatus(status);
};
const startIfNotStarted = async () => {
await torrific.startIfNotStarted();
};
const stopIfRunning = async () => {
await torrific.stopIfRunning();
};
const testSocket = async () => {
try {
setIsLoading(true);
await torrific.testSocket();
2021-09-21 23:55:14 -04:00
alert(loc._.ok);
2021-04-23 12:29:45 +01:00
} catch (error) {
alert(error.message);
} finally {
setIsLoading(false);
}
};
const testHttp = async () => {
try {
setIsLoading(true);
await torrific.testHttp();
2021-09-21 23:55:14 -04:00
alert(loc._.ok);
2021-04-23 12:29:45 +01:00
} catch (error) {
alert(error.message);
} finally {
setIsLoading(false);
}
};
useEffect(() => {
const interval = setInterval(updateStatus, 1000);
2021-09-21 23:55:14 -04:00
2021-04-23 12:29:45 +01:00
return () => {
clearInterval(interval);
};
}, []);
2021-09-21 23:55:14 -04:00
useEffect(() => {
2021-09-22 12:07:13 -04:00
if (isTorDisabled) {
2021-09-21 23:55:14 -04:00
stopIfRunning();
}
2021-09-22 12:07:13 -04:00
}, [isTorDisabled]);
2021-09-21 23:55:14 -04:00
2021-04-23 12:29:45 +01:00
if (isLoading) {
return (
<View>
2021-04-23 12:29:45 +01:00
<BlueLoading />
</View>
);
}
return (
<SafeBlueArea>
2021-09-21 23:55:14 -04:00
<BlueListItem
hideChevron
title={loc._.disabled}
Component={View}
2021-09-22 12:07:13 -04:00
switch={{ onValueChange: setIsTorDisabled, value: isTorDisabled }}
2021-09-21 23:55:14 -04:00
/>
2021-09-22 12:07:13 -04:00
{!isTorDisabled && (
2021-09-21 23:55:14 -04:00
<>
<BlueCard>
<BlueText>Daemon Status: {daemonStatus}</BlueText>
</BlueCard>
<BlueCard>
<BlueButton title={loc.send.dynamic_start} onPress={startIfNotStarted} />
<BlueSpacing20 />
<BlueButton title={loc.send.dynamic_stop} onPress={stopIfRunning} />
<BlueSpacing20 />
<BlueButton title="Test Socket" onPress={testSocket} />
<BlueSpacing20 />
<BlueButton title="Test HTTP" onPress={testHttp} />
</BlueCard>
</>
)}
2021-04-23 12:29:45 +01:00
</SafeBlueArea>
);
};
TorSettings.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.settings.tor_settings }));
export default TorSettings;