2019-12-11 21:09:27 +01:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { FormField } from 'component/common/form';
|
2019-12-12 21:18:13 +01:00
|
|
|
import Button from 'component/button';
|
|
|
|
import * as ICONS from 'constants/icons';
|
|
|
|
|
2019-12-11 21:09:27 +01:00
|
|
|
import ServerInputRow from './internal/inputRow';
|
|
|
|
|
|
|
|
type DaemonSettings = {
|
2019-12-13 20:47:50 +01:00
|
|
|
lbryum_servers: Array<string>,
|
2019-12-12 21:18:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
type DaemonStatus = {
|
|
|
|
wallet: any,
|
2019-12-11 21:09:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
type StatusOfServer = {
|
|
|
|
host: string,
|
|
|
|
port: string,
|
|
|
|
availability: boolean,
|
|
|
|
latency: number,
|
2019-12-12 21:18:13 +01:00
|
|
|
};
|
2019-12-11 21:09:27 +01:00
|
|
|
|
2019-12-12 21:18:13 +01:00
|
|
|
type ServerTuple = Array<string>; // ['host', 'port']
|
|
|
|
type ServerStatus = Array<StatusOfServer>;
|
|
|
|
type ServerConfig = Array<ServerTuple>;
|
2019-12-11 21:09:27 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
daemonSettings: DaemonSettings,
|
|
|
|
getDaemonStatus: () => void,
|
2019-12-12 21:18:13 +01:00
|
|
|
setCustomWalletServers: any => void,
|
2019-12-11 21:09:27 +01:00
|
|
|
clearWalletServers: () => void,
|
2019-12-12 21:18:13 +01:00
|
|
|
customWalletServers: ServerConfig,
|
2019-12-13 20:47:50 +01:00
|
|
|
saveServerConfig: (Array<string>) => void,
|
2019-12-11 21:09:27 +01:00
|
|
|
fetchDaemonSettings: () => void,
|
2019-12-12 21:18:13 +01:00
|
|
|
hasWalletServerPrefs: boolean,
|
|
|
|
daemonStatus: DaemonStatus,
|
2019-12-11 21:09:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function SettingWalletServer(props: Props) {
|
|
|
|
const {
|
|
|
|
daemonSettings,
|
2019-12-12 21:18:13 +01:00
|
|
|
daemonStatus,
|
2019-12-11 21:09:27 +01:00
|
|
|
fetchDaemonSettings,
|
2019-12-12 21:18:13 +01:00
|
|
|
setCustomWalletServers,
|
2019-12-11 21:09:27 +01:00
|
|
|
getDaemonStatus,
|
|
|
|
clearWalletServers,
|
2019-12-12 21:18:13 +01:00
|
|
|
saveServerConfig,
|
|
|
|
customWalletServers,
|
|
|
|
hasWalletServerPrefs,
|
2019-12-11 21:09:27 +01:00
|
|
|
} = props;
|
|
|
|
|
2019-12-12 21:18:13 +01:00
|
|
|
const [advancedMode, setAdvancedMode] = useState(false);
|
|
|
|
|
|
|
|
const activeWalletServers: ServerStatus = (daemonStatus && daemonStatus.wallet && daemonStatus.wallet.servers) || [];
|
|
|
|
const currentServerConfig: ServerConfig = daemonSettings && daemonSettings.lbryum_servers;
|
|
|
|
const serverConfig: ServerConfig = customWalletServers.length ? customWalletServers : currentServerConfig;
|
2019-12-11 21:09:27 +01:00
|
|
|
const STATUS_INTERVAL = 5000;
|
|
|
|
|
|
|
|
useEffect(() => {
|
2019-12-12 21:18:13 +01:00
|
|
|
if (hasWalletServerPrefs) {
|
|
|
|
setAdvancedMode(true);
|
2019-12-11 21:09:27 +01:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const interval = setInterval(() => {
|
2019-12-12 21:18:13 +01:00
|
|
|
getDaemonStatus();
|
2019-12-11 21:09:27 +01:00
|
|
|
}, STATUS_INTERVAL);
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchDaemonSettings();
|
2019-12-12 21:18:13 +01:00
|
|
|
}, []);
|
2019-12-11 21:09:27 +01:00
|
|
|
|
|
|
|
function makeServerParam(configList) {
|
|
|
|
return configList.reduce((acc, cur) => {
|
|
|
|
acc.push(`${cur[0]}:${cur[1]}`);
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doClear() {
|
2019-12-12 21:18:13 +01:00
|
|
|
setAdvancedMode(false);
|
2019-12-11 21:09:27 +01:00
|
|
|
clearWalletServers();
|
|
|
|
}
|
|
|
|
|
2019-12-13 20:47:50 +01:00
|
|
|
function onAdd(serverTuple: Array<string>) {
|
2019-12-12 21:18:13 +01:00
|
|
|
let newServerConfig = serverConfig.concat();
|
2019-12-11 21:09:27 +01:00
|
|
|
newServerConfig.push(serverTuple);
|
2019-12-12 21:18:13 +01:00
|
|
|
updateServers(newServerConfig);
|
2019-12-11 21:09:27 +01:00
|
|
|
}
|
|
|
|
|
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(makeServerParam(newConfig));
|
2019-12-11 21:09:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
2019-12-13 20:47:50 +01:00
|
|
|
label={__('Use official lbry.tv wallet servers')}
|
2019-12-13 18:26:59 +01:00
|
|
|
onChange={e => {
|
|
|
|
if (e.target.checked) {
|
|
|
|
doClear();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<FormField
|
|
|
|
type="radio"
|
|
|
|
name="custom_wallet_servers"
|
|
|
|
checked={advancedMode}
|
|
|
|
onChange={e => {
|
|
|
|
setAdvancedMode(e.target.checked);
|
|
|
|
if (e.target.checked) {
|
|
|
|
setCustomWalletServers(makeServerParam(customWalletServers));
|
|
|
|
}
|
|
|
|
}}
|
2019-12-13 20:47:50 +01:00
|
|
|
label={__('Use custom wallet servers')}
|
2019-12-13 18:26:59 +01:00
|
|
|
/>
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={`${host}:${port}`}
|
|
|
|
className="section section--padded card--inline form-field__internal-option"
|
|
|
|
>
|
|
|
|
<h3>
|
|
|
|
{host}:{port}
|
|
|
|
</h3>
|
|
|
|
<span className="help">{available ? 'Connected' : 'Not connected'}</span>
|
|
|
|
<Button
|
|
|
|
button="close"
|
|
|
|
title={__('Remove custom wallet server')}
|
|
|
|
icon={ICONS.REMOVE}
|
|
|
|
onClick={() => onDelete(index)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
<div className="form-field__internal-option">
|
2019-12-11 21:09:27 +01:00
|
|
|
<ServerInputRow update={onAdd} />
|
2019-12-13 20:47:50 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</fieldset-section>
|
2019-12-11 21:09:27 +01:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SettingWalletServer;
|