2019-12-11 21:09:27 +01:00
|
|
|
// @flow
|
|
|
|
|
2019-12-19 14:53:15 +01:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2019-12-11 21:09:27 +01:00
|
|
|
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';
|
2020-07-10 23:04:36 +02:00
|
|
|
import { stringifyServerParam } from 'util/sync-settings';
|
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-19 14:53:15 +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,
|
|
|
|
};
|
|
|
|
|
2019-12-11 21:09:27 +01:00
|
|
|
type Props = {
|
|
|
|
getDaemonStatus: () => void,
|
2021-08-22 09:58:56 +02: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-19 14:53:15 +01:00
|
|
|
saveServerConfig: (Array<ServerTuple>) => void,
|
2019-12-12 21:18:13 +01:00
|
|
|
hasWalletServerPrefs: boolean,
|
|
|
|
daemonStatus: DaemonStatus,
|
2019-12-19 14:53:15 +01:00
|
|
|
walletReconnecting: boolean,
|
2022-05-31 20:03:29 +02:00
|
|
|
walletRollbackToDefault: boolean,
|
|
|
|
walletReconnectingToDefault: boolean,
|
2019-12-11 21:09:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function SettingWalletServer(props: Props) {
|
|
|
|
const {
|
2019-12-12 21:18:13 +01:00
|
|
|
daemonStatus,
|
|
|
|
setCustomWalletServers,
|
2019-12-11 21:09:27 +01:00
|
|
|
getDaemonStatus,
|
|
|
|
clearWalletServers,
|
2019-12-12 21:18:13 +01:00
|
|
|
saveServerConfig,
|
|
|
|
customWalletServers,
|
|
|
|
hasWalletServerPrefs,
|
2019-12-19 14:53:15 +01:00
|
|
|
walletReconnecting,
|
2022-05-31 20:03:29 +02:00
|
|
|
walletRollbackToDefault,
|
|
|
|
walletReconnectingToDefault,
|
2019-12-11 21:09:27 +01:00
|
|
|
} = props;
|
|
|
|
|
2022-05-31 20:03:29 +02:00
|
|
|
const [usingCustomServer, setUsingCustomServer] = useState(false);
|
|
|
|
const [showCustomServers, setShowCustomServers] = useState(false);
|
2019-12-12 21:18:13 +01:00
|
|
|
|
2019-12-19 14:53:15 +01:00
|
|
|
const walletStatus = daemonStatus && daemonStatus.wallet;
|
|
|
|
const activeWalletServers: ServerStatus = (walletStatus && walletStatus.servers) || [];
|
|
|
|
const availableServers = walletStatus && walletStatus.available_servers;
|
|
|
|
const serverConfig: ServerConfig = customWalletServers;
|
2019-12-11 21:09:27 +01:00
|
|
|
const STATUS_INTERVAL = 5000;
|
|
|
|
|
2019-12-19 14:53:15 +01:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2019-12-11 21:09:27 +01:00
|
|
|
useEffect(() => {
|
2019-12-12 21:18:13 +01:00
|
|
|
if (hasWalletServerPrefs) {
|
2022-05-31 20:03:29 +02:00
|
|
|
setUsingCustomServer(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);
|
|
|
|
}, []);
|
|
|
|
|
2022-05-31 20:03:29 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (walletRollbackToDefault) {
|
|
|
|
doClear();
|
|
|
|
}
|
|
|
|
}, [walletRollbackToDefault]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (usingCustomServer) {
|
|
|
|
setShowCustomServers(true);
|
|
|
|
}
|
|
|
|
}, [usingCustomServer]);
|
|
|
|
|
2019-12-11 21:09:27 +01:00
|
|
|
function doClear() {
|
2022-05-31 20:03:29 +02:00
|
|
|
setUsingCustomServer(false);
|
2019-12-11 21:09:27 +01:00
|
|
|
clearWalletServers();
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:53:15 +01:00
|
|
|
function onAdd(serverTuple: ServerTuple) {
|
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);
|
2020-07-10 23:04:36 +02:00
|
|
|
setCustomWalletServers(stringifyServerParam(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"
|
2022-05-31 20:03:29 +02:00
|
|
|
checked={!usingCustomServer}
|
2021-11-28 21:05:10 +01:00
|
|
|
label={__('Use official LBRY wallet servers')}
|
2021-08-22 09:58:56 +02:00
|
|
|
onChange={(e) => {
|
2019-12-13 18:26:59 +01:00
|
|
|
if (e.target.checked) {
|
|
|
|
doClear();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<FormField
|
|
|
|
type="radio"
|
|
|
|
name="custom_wallet_servers"
|
2022-05-31 20:03:29 +02:00
|
|
|
checked={usingCustomServer}
|
2021-08-22 09:58:56 +02:00
|
|
|
onChange={(e) => {
|
2022-05-31 20:03:29 +02:00
|
|
|
setUsingCustomServer(e.target.checked);
|
2019-12-19 14:53:15 +01:00
|
|
|
if (e.target.checked && customWalletServers.length) {
|
2020-07-10 23:04:36 +02:00
|
|
|
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
|
|
|
|
2022-05-31 20:03:29 +02:00
|
|
|
{showCustomServers && (
|
2019-12-13 20:47:50 +01:00
|
|
|
<div>
|
|
|
|
{serverConfig &&
|
|
|
|
serverConfig.map((entry, index) => {
|
|
|
|
const [host, port] = entry;
|
|
|
|
const available = activeWalletServers.some(
|
2021-08-22 09:58:56 +02:00
|
|
|
(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>
|
2019-12-19 14:53:15 +01:00
|
|
|
<span className="help">
|
2022-05-31 20:03:29 +02:00
|
|
|
{available
|
|
|
|
? __('Connected')
|
|
|
|
: walletReconnecting && !walletReconnectingToDefault
|
|
|
|
? __('Connecting...')
|
|
|
|
: __('Not connected')}
|
2019-12-19 14:53:15 +01:00
|
|
|
</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">
|
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;
|