import React from 'react'; import { translate } from '../../../translate/translate'; import { Config, iguanaActiveHandle, encryptWallet, settingsWifkeyState, importPrivKey, getDebugLog, getPeersList, addPeerNode, getAppConfig, saveAppConfig, getAppInfo, shepherdCli, checkForUpdateUIPromise, updateUIPromise } from '../../../actions/actionCreators'; import Store from '../../../store'; import { AppInfoTabRender, SettingsRender, AppUpdateTabRender, } from './settings.render'; import { SocketProvider } from 'socket.io-react'; import io from 'socket.io-client'; const socket = io.connect('http://127.0.0.1:' + Config.agamaPort); let updateProgressBar = { patch: -1, }; /* TODO: 1) pre-select active coin in add node tab 2) add fiat section 3) kickstart section 4) batch export/import wallet addresses */ class Settings extends React.Component { constructor(props) { super(props); this.state = { activeTab: 0, debugLinesCount: 10, debugTarget: 'iguana', activeTabHeight: '0', appSettings: {}, tabElId: null, cliCmdString: null, cliCoin: null, cliResponse: null, exportWifKeysRaw: false, seedInputVisibility: false, nativeOnly: Config.iguanaLessMode, updatePatch: null, updateBins: null, updateLog: [], updateProgressPatch: null, wifkeysPassphrase: '', trimPassphraseTimer: null, }; this.exportWifKeys = this.exportWifKeys.bind(this); this.updateInput = this.updateInput.bind(this); this.updateInputSettings = this.updateInputSettings.bind(this); this.importWifKey = this.importWifKey.bind(this); this.readDebugLog = this.readDebugLog.bind(this); this.checkNodes = this.checkNodes.bind(this); this.addNode = this.addNode.bind(this); this.renderPeersList = this.renderPeersList.bind(this); this.renderSNPeersList = this.renderSNPeersList.bind(this); this._saveAppConfig = this._saveAppConfig.bind(this); this.exportWifKeysRaw = this.exportWifKeysRaw.bind(this); this.toggleSeedInputVisibility = this.toggleSeedInputVisibility.bind(this); this._checkForUpdateUIPromise = this._checkForUpdateUIPromise.bind(this); this._updateUIPromise = this._updateUIPromise.bind(this); socket.on('patch', msg => this.updateSocketsData(msg)); } componentDidMount() { Store.dispatch(iguanaActiveHandle()); Store.dispatch(getAppConfig()); Store.dispatch(getAppInfo()); } componentWillReceiveProps(props) { if (this.state.tabElId) { const _height = document.querySelector(`#${this.state.tabElId} .panel-collapse .panel-body`).offsetHeight; this.setState(Object.assign({}, this.state, { activeTab: this.state.activeTab, activeTabHeight: _height, tabElId: this.state.tabElId, })); } } resizeLoginTextarea() { // auto-size textarea setTimeout(() => { if (this.state.seedInputVisibility) { document.querySelector('#wifkeysPassphraseTextarea').style.height = '1px'; document.querySelector('#wifkeysPassphraseTextarea').style.height = `${(15 + document.querySelector('#wifkeysPassphraseTextarea').scrollHeight)}px`; } }, 100); } updateSocketsData(data) { if (data && data.msg && data.msg.type === 'ui') { if (data.msg.status === 'progress' && data.msg.progress && data.msg.progress < 100) { this.setState(Object.assign({}, this.state, { updateProgressPatch: data.msg.progress, })); updateProgressBar.patch = data.msg.progress; } else { if (data.msg.status === 'progress' && data.msg.progress && data.msg.progress === 100) { let _updateLog = []; _updateLog.push('UI update downloaded. Verifying...'); this.setState(Object.assign({}, this.state, { updateLog: _updateLog, })); updateProgressBar.patch = 100; } if (data.msg.status === 'done') { let _updateLog = []; _updateLog.push('UI is updated!'); this.setState(Object.assign({}, this.state, { updateLog: _updateLog, updatePatch: null, })); updateProgressBar.patch = -1; } if (data.msg.status === 'error') { let _updateLog = []; _updateLog.push('Error while verifying update file! Please retry again.'); this.setState(Object.assign({}, this.state, { updateLog: _updateLog, })); updateProgressBar.patch = -1; } } } else { if (data && data.msg) { let _updateLog = this.state.updateLog; _updateLog.push(data.msg); this.setState(Object.assign({}, this.state, { updateLog: _updateLog, })); } } } _checkForUpdateUIPromise() { let _updateLog = []; _updateLog.push('Checking for UI update...'); this.setState(Object.assign({}, this.state, { updateLog: _updateLog, })); checkForUpdateUIPromise() .then((res) => { let _updateLog = this.state.updateLog; _updateLog.push(res.result === 'update' ? (`New UI update available ${res.version.remote}`) : 'You have the lastest UI version'); this.setState(Object.assign({}, this.state, { updatePatch: res.result === 'update' ? true : false, updateLog: _updateLog, })); }); } _updateUIPromise() { updateProgressBar.patch = 0; let _updateLog = []; _updateLog.push('Downloading UI update...'); this.setState(Object.assign({}, this.state, { updateLog: _updateLog, })); updateUIPromise(); } renderUpdateStatus() { let items = []; let patchProgressBar = null; for (let i = 0; i < this.state.updateLog.length; i++) { items.push(
{ this.state.updateLog[i] }
); } return (

Progress:
{ items }
-1 ? 'progress progress-sm' : 'hide' }>
); } toggleSeedInputVisibility() { this.setState({ seedInputVisibility: !this.state.seedInputVisibility, }); } execCliCmd() { Store.dispatch( shepherdCli( 'passthru', this.state.cliCoin, this.state.cliCmd ) ); } openTab(elemId, tab) { setTimeout(() => { const _height = document.querySelector(`#${elemId} .panel-collapse .panel-body`).offsetHeight; this.setState(Object.assign({}, this.state, { activeTab: tab, activeTabHeight: _height, tabElId: elemId, })); }, 100); } exportWifKeys() { Store.dispatch( encryptWallet( this.state.wifkeysPassphrase, settingsWifkeyState, this.props.ActiveCoin.coin ) ); } importWifKey() { Store.dispatch(importPrivKey(this.state.importWifKey)); } readDebugLog() { Store.dispatch( getDebugLog( this.state.debugTarget, this.state.debugLinesCount ) ); } checkNodes() { if (this.state.getPeersCoin) { Store.dispatch(getPeersList(this.state.getPeersCoin.split('|')[0])); } } addNode() { if (this.state.addNodeCoin) { Store.dispatch( addPeerNode( this.state.addNodeCoin.split('|')[0], this.state.addPeerIP ) ); } } renderPeersList() { if (this.state.getPeersCoin) { const _getPeersCoin = this.state.getPeersCoin; const _rawPeers = this.props.Settings.rawPeers; const coin = _getPeersCoin.split('|')[0]; if (_rawPeers && _getPeersCoin && _rawPeers[coin]) { return _rawPeers[coin].map((ip) =>
{ ip }
); } else { return null; } } else { return null; } } renderAppInfoTab() { const releaseInfo = this.props.Settings.appInfo && this.props.Settings.appInfo.releaseInfo; if (releaseInfo) { return AppInfoTabRender.call(this); } return null; } renderAppUpdateTab() { return AppUpdateTabRender.call(this); } renderSNPeersList() { if (this.state.getPeersCoin) { const _getPeersCoin = this.state.getPeersCoin; const _supernetPeers = this.props.Settings.supernetPeers; const coin = _getPeersCoin.split('|')[0]; if (_supernetPeers && _getPeersCoin && _supernetPeers[coin]) { return _supernetPeers[coin].map((ip) =>
{ ip }
); } else { return null; } } else { return null; } } updateInputSettings(e) { let _appSettings = this.state.appSettings; _appSettings[e.target.name] = e.target.value; this.setState({ appSettings: _appSettings, }); } _saveAppConfig() { const _appSettings = this.state.appSettings; let _appSettingsPristine = Object.assign({}, this.props.Settings.appSettings); for (let key in _appSettings) { if (key.indexOf('__') === -1) { _appSettingsPristine[key] = _appSettings[key]; } else { const _nestedKey = key.split('__'); _appSettingsPristine[_nestedKey[0]][_nestedKey[1]] = _appSettings[key]; } } Store.dispatch(saveAppConfig(_appSettingsPristine)); } renderConfigEditForm() { let items = []; const _appConfig = this.props.Settings.appSettings; for (let key in _appConfig) { if (typeof _appConfig[key] === 'object') { items.push( { key } ); for (let _key in _appConfig[key]) { items.push( { _key } ); } } else { items.push( { key } ); } } return items; } updateInput(e) { if (e.target.name === 'wifkeysPassphrase') { // remove any empty chars from the start/end of the string const newValue = e.target.value; clearTimeout(this.state.trimPassphraseTimer); const _trimPassphraseTimer = setTimeout(() => { this.setState({ wifkeysPassphrase: newValue ? newValue.trim() : '', // hardcoded field name }); }, 2000); this.resizeLoginTextarea(); this.setState({ trimPassphraseTimer: _trimPassphraseTimer, [e.target.name]: newValue, }); } else { this.setState({ [e.target.name]: e.target.value, }); } } renderDebugLogData() { if (this.props.Settings.debugLog) { const _debugLogDataRows = this.props.Settings.debugLog.split('\n'); if (_debugLogDataRows && _debugLogDataRows.length) { return _debugLogDataRows.map((_row) =>
{ _row }
); } else { return ( { translate('INDEX.EMPTY_DEBUG_LOG') } ); } } else { return null; } } renderLB(_translationID) { const _translationComponents = translate(_translationID).split('
'); return _translationComponents.map((_translation) => { _translation }
); } renderCliResponse() { const _cliResponse = this.props.Settings.cli; if (_cliResponse) { let _cliResponseParsed; try { _cliResponseParsed = JSON.parse(_cliResponse.result); } catch(e) { _cliResponseParsed = _cliResponse.result; } let __cliResponseParsed; if (typeof _cliResponseParsed !== 'object' && typeof _cliResponseParsed !== 'number' && _cliResponseParsed !== 'wrong cli string format' && _cliResponseParsed.indexOf('\r\n') > -1) { _cliResponseParsed = _cliResponseParsed.split('\r\n') ; } else if ( typeof _cliResponseParsed !== 'object' && typeof _cliResponseParsed !== 'number' && _cliResponseParsed !== 'wrong cli string format' && _cliResponseParsed.indexOf('\n') > -1 ) { __cliResponseParsed = _cliResponseParsed.split('\n') ; } else { __cliResponseParsed = _cliResponseParsed; } let _items = []; if (__cliResponseParsed.length && __cliResponseParsed !== 'wrong cli string format') { for (let i = 0; i < __cliResponseParsed.length; i++) { _items.push(
{ typeof __cliResponseParsed[i] === 'object' ? JSON.stringify(__cliResponseParsed[i], null, '\t') : __cliResponseParsed[i] }
); } } else { if (typeof _cliResponseParsed === 'object') { _items.push(
{ JSON.stringify(__cliResponseParsed, null, '\t') }
); } else if (typeof _cliResponseParsed === 'string' || typeof _cliResponseParsed === 'number' || _cliResponseParsed === 'wrong cli string format') { _items.push(
{ __cliResponseParsed }
); } else { _items.push(
{ translate('INDEX.NO_DATA_AVAILABLE') }
); } } return (
CLI response:
{ _items }
); } else { return null; } } renderActiveCoinsList(mode) { const modes = [ 'native', 'basilisk', 'full' ]; const allCoins = this.props.Main.coins; let items = []; if (allCoins) { if (mode === 'all') { modes.map(function(mode) { allCoins[mode].map(function(coin) { items.push( ); }); }); } else { allCoins[mode].map(function(coin) { items.push( ); }); } return items; } else { return null; } } renderExportWifKeysRaw() { const _wifKeysResponse = this.props.Settings.wifkey; if (_wifKeysResponse && this.state.exportWifKeysRaw) { return (
{ JSON.stringify(_wifKeysResponse, null, '\t') }
); } else { return null; } } renderWifKeys() { let items = []; if (this.props.Settings.wifkey) { const _wifKeys = this.props.Settings.wifkey; for (let i = 0; i < 2; i++) { items.push( { i === 0 ? 'Address list' : 'Wif key list' } ); for (let _key in _wifKeys) { if ((i === 0 && _key.length === 3 && _key !== 'tag') || (i === 1 && _key.indexOf('wif') > -1)) { items.push( { _key } { _wifKeys[_key] } ); } } } return items; } else { return null; } } exportWifKeysRaw() { this.setState(Object.assign({}, this.state, { exportWifKeysRaw: !this.state.exportWifKeysRaw, })); } render() { return SettingsRender.call(this); } } export default Settings;