lbry-desktop/ui/component/settingWalletServer/view.jsx

187 lines
5.3 KiB
React
Raw Normal View History

// @flow
import React, { useState, useEffect, useRef } from 'react';
import { FormField } from 'component/common/form';
2019-12-12 21:18:13 +01:00
import Button from 'component/button';
2019-12-19 07:05:06 +01:00
import I18nMessage from 'component/i18nMessage';
2019-12-12 21:18:13 +01:00
import * as ICONS from 'constants/icons';
import ServerInputRow from './internal/inputRow';
import { stringifyServerParam } from 'util/sync-settings';
type StatusOfServer = {
host: string,
port: string,
availability: boolean,
latency: number,
2019-12-12 21:18:13 +01:00
};
type ServerTuple = [string, string]; // ['host', 'port']
2019-12-12 21:18:13 +01:00
type ServerStatus = Array<StatusOfServer>;
type ServerConfig = Array<ServerTuple>;
2019-12-19 07:05:06 +01:00
type DaemonStatus = {
wallet: any,
};
type Props = {
getDaemonStatus: () => void,
setCustomWalletServers: (any) => void,
clearWalletServers: () => void,
2019-12-12 21:18:13 +01:00
customWalletServers: ServerConfig,
saveServerConfig: (Array<ServerTuple>) => void,
2019-12-12 21:18:13 +01:00
hasWalletServerPrefs: boolean,
daemonStatus: DaemonStatus,
walletReconnecting: boolean,
};
function SettingWalletServer(props: Props) {
const {
2019-12-12 21:18:13 +01:00
daemonStatus,
setCustomWalletServers,
getDaemonStatus,
clearWalletServers,
2019-12-12 21:18:13 +01:00
saveServerConfig,
customWalletServers,
hasWalletServerPrefs,
walletReconnecting,
} = props;
2019-12-12 21:18:13 +01:00
const [advancedMode, setAdvancedMode] = useState(false);
const walletStatus = daemonStatus && daemonStatus.wallet;
const activeWalletServers: ServerStatus = (walletStatus && walletStatus.servers) || [];
const availableServers = walletStatus && walletStatus.available_servers;
const serverConfig: ServerConfig = customWalletServers;
const STATUS_INTERVAL = 5000;
// onUnmount, if there are no available servers, doClear()
// in order to replicate componentWillUnmount, the effect needs to get the value from a ref
const hasAvailableRef = useRef();
useEffect(
() => () => {
hasAvailableRef.current = availableServers;
},
[availableServers]
);
useEffect(
() => () => {
if (!hasAvailableRef.current) {
doClear();
}
},
[]
);
useEffect(() => {
2019-12-12 21:18:13 +01:00
if (hasWalletServerPrefs) {
setAdvancedMode(true);
}
}, []);
useEffect(() => {
const interval = setInterval(() => {
2019-12-12 21:18:13 +01:00
getDaemonStatus();
}, STATUS_INTERVAL);
return () => clearInterval(interval);
}, []);
function doClear() {
2019-12-12 21:18:13 +01:00
setAdvancedMode(false);
clearWalletServers();
}
function onAdd(serverTuple: ServerTuple) {
2019-12-12 21:18:13 +01:00
let newServerConfig = serverConfig.concat();
newServerConfig.push(serverTuple);
2019-12-12 21:18:13 +01:00
updateServers(newServerConfig);
}
2019-12-13 20:47:50 +01:00
function onDelete(i: number) {
2019-12-12 21:18:13 +01:00
const newServerConfig = serverConfig.concat();
newServerConfig.splice(i, 1);
updateServers(newServerConfig);
}
function updateServers(newConfig) {
saveServerConfig(newConfig);
setCustomWalletServers(stringifyServerParam(newConfig));
}
return (
<React.Fragment>
2019-12-13 20:47:50 +01:00
<fieldset-section>
2019-12-13 18:26:59 +01:00
<FormField
type="radio"
name="default_wallet_servers"
checked={!advancedMode}
label={__('Use official LBRY wallet servers')}
onChange={(e) => {
2019-12-13 18:26:59 +01:00
if (e.target.checked) {
doClear();
}
}}
/>
<FormField
type="radio"
name="custom_wallet_servers"
checked={advancedMode}
onChange={(e) => {
2019-12-13 18:26:59 +01:00
setAdvancedMode(e.target.checked);
if (e.target.checked && customWalletServers.length) {
setCustomWalletServers(stringifyServerParam(customWalletServers));
2019-12-13 18:26:59 +01:00
}
}}
2019-12-13 20:47:50 +01:00
label={__('Use custom wallet servers')}
2019-12-13 18:26:59 +01:00
/>
2019-12-19 07:05:06 +01:00
<p className="help">
<I18nMessage
tokens={{
2019-12-19 18:22:38 +01:00
learn_more: <Button button="link" href="http://lbry.com/faq/wallet-servers" label={__('Learn More')} />,
2019-12-19 07:05:06 +01:00
}}
>
2019-12-19 18:22:38 +01:00
Wallet servers are used to relay data to and from the LBRY blockchain. They also determine what content
shows in trending or is blocked. %learn_more%
2019-12-19 07:05:06 +01:00
</I18nMessage>
</p>
2019-12-13 20:47:50 +01:00
{advancedMode && (
<div>
{serverConfig &&
serverConfig.map((entry, index) => {
const [host, port] = entry;
const available = activeWalletServers.some(
(s) => s.host === entry[0] && String(s.port) === entry[1] && s.availability
2019-12-13 20:47:50 +01:00
);
return (
<div
key={`${host}:${port}`}
className="section section--padded card--inline form-field__internal-option"
>
<h3>
{host}:{port}
</h3>
<span className="help">
{available ? __('Connected') : walletReconnecting ? __('Connecting...') : __('Not connected')}
</span>
2019-12-13 20:47:50 +01:00
<Button
button="close"
title={__('Remove custom wallet server')}
icon={ICONS.REMOVE}
onClick={() => onDelete(index)}
/>
</div>
);
})}
<div className="form-field__internal-option">
<ServerInputRow update={onAdd} />
2019-12-13 20:47:50 +01:00
</div>
</div>
)}
</fieldset-section>
</React.Fragment>
);
}
export default SettingWalletServer;