From e95477f4210df55bade37ab6bd5fe589941de697 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 8 Dec 2018 23:36:43 -0600 Subject: [PATCH] Refactoring --- src/App.js | 121 +++++++++++------------- src/constants.js | 6 ++ src/containers/MountItems/MountItems.js | 105 +++++++++----------- 3 files changed, 107 insertions(+), 125 deletions(-) diff --git a/src/App.js b/src/App.js index 320356c..ecd111c 100644 --- a/src/App.js +++ b/src/App.js @@ -28,6 +28,14 @@ class App extends Component { constructor(props) { super(props); + for (const provider of Constants.PROVIDER_LIST) { + this.state[provider] = { + AutoMount: false, + AutoRestart: false, + MountLocation: '', + } + } + if (ipcRenderer) { ipcRenderer.on(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply); ipcRenderer.on(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete); @@ -60,15 +68,9 @@ class App extends Component { DownloadingRelease: false, DownloadingUpgrade: false, ExtractActive: false, - Hyperspace: { - AutoMount: false, - AutoRestart: false, - MountLocation: '', - }, LocationsLookup: {}, MissingDependencies: [], MountsBusy: false, - Platform: 'unknown', Release: 3, ReleaseTypes: [ 'Release', @@ -77,16 +79,6 @@ class App extends Component { 'Alpha', ], InstalledVersion: 'none', - Sia: { - AutoMount: false, - AutoRestart: false, - MountLocation: '', - }, - SiaPrime: { - AutoMount: false, - AutoRestart: false, - MountLocation: '', - }, UpgradeAvailable: false, UpgradeData: {}, UpgradeDismissed: false, @@ -112,7 +104,7 @@ class App extends Component { this.setState({ AllowDownload: false, }, ()=> { - const selectedVersion = this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; + const selectedVersion = this.getSelectedVersion(); if (selectedVersion !== 'unavailable') { if (ipcRenderer) { let dependencies = []; @@ -161,6 +153,10 @@ class App extends Component { } }; + getSelectedVersion = () => { + return this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; + }; + grabReleases = () => { if (this.props.platform !== 'unknown') { if (ipcRenderer) { @@ -178,7 +174,7 @@ class App extends Component { this.setState({ [storageType]: state, }, ()=> { - this.saveState(this.state.Release, this.state.Version, this.state.Sia, this.state.Hyperspace, this.state.SiaPrime); + this.saveState(); }); }; @@ -190,7 +186,7 @@ class App extends Component { this.setState({ [storageType]: state, }, ()=> { - this.saveState(this.state.Release, this.state.Version, this.state.Sia, this.state.Hyperspace, this.state.SiaPrime); + this.saveState(); }); }; @@ -233,7 +229,7 @@ class App extends Component { this.setState({ [storageType]: state, }, ()=> { - this.saveState(this.state.Release, this.state.Version, this.state.Sia, this.state.Hyperspace, this.state.SiaPrime); + this.saveState(); }); }; @@ -244,13 +240,13 @@ class App extends Component { Release: val, Version: versionIndex }, ()=> { - this.saveState(this.state.Release, this.state.Version, this.state.Sia, this.state.Hyperspace, this.state.SiaPrime); + this.saveState(); this.checkVersionInstalled( ); }); }; handleReleaseDownload = () => { - const selectedVersion = this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; + const selectedVersion = this.getSelectedVersion(); const fileName = selectedVersion + '.zip'; if (ipcRenderer) { this.setState({ @@ -289,7 +285,7 @@ class App extends Component { this.setState({ Version: parseInt(e.target.value, 10), }, ()=> { - this.saveState(this.state.Release, this.state.Version, this.state.Sia, this.state.Hyperspace, this.state.SiaPrime); + this.saveState(); this.checkVersionInstalled( ); }); }; @@ -335,7 +331,7 @@ class App extends Component { onDownloadFileComplete = (event, arg) => { if (this.state.DownloadingRelease) { if (arg.data.Success) { - const selectedVersion = this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; + const selectedVersion = this.getSelectedVersion(); ipcRenderer.send(Constants.IPC_Extract_Release, { Directory: Constants.DATA_LOCATIONS[this.props.platform], Source: arg.data.Destination, @@ -406,37 +402,23 @@ class App extends Component { onGetStateReply = (event, arg) => { if (arg.data) { - let hyperspace = arg.data.Hyperspace || this.state.Hyperspace; - if (hyperspace.AutoMount === undefined) { - hyperspace['AutoMount'] = false; - } - if (hyperspace.AutoRestart === undefined) { - hyperspace['AutoRestart'] = false; - } - - let sia = arg.data.Sia || this.state.Sia; - if (sia.AutoMount === undefined) { - sia['AutoMount'] = false; - } - if (sia.AutoRestart === undefined) { - sia['AutoRestart'] = false; - } - - let siaPrime = arg.data.SiaPrime || this.state.SiaPrime; - if (siaPrime.AutoMount === undefined) { - siaPrime['AutoMount'] = false; - } - if (siaPrime.AutoRestart === undefined) { - siaPrime['AutoRestart'] = false; - } - - this.setState({ - Hyperspace: hyperspace, + let state = { Release: arg.data.Release, - Sia: sia, - SiaPrime: siaPrime, Version: arg.data.Version, - }, ()=> { + }; + + for (const provider of Constants.PROVIDER_LIST) { + let data = arg.data[provider] || this.state[provider]; + if (data.AutoMount === undefined) { + data['AutoMount'] = false; + } + if (data.AutoRestart === undefined) { + data['AutoRestart'] = false; + } + state[provider] = data; + } + + this.setState(state, ()=> { this.grabReleases(); }); } else { @@ -450,7 +432,7 @@ class App extends Component { let version = this.state.Version; if ((version === -1) || !versionLookup[this.state.ReleaseTypes[this.state.Release]][version]) { version = latestVersion; - this.saveState(this.state.Release, version, this.state.Sia, this.state.Hyperspace, this.state.SiaPrime); + this.saveState(version); } this.setState({ @@ -541,17 +523,19 @@ class App extends Component { }); }; - saveState = (release, version, sia, hyperspace, siaPrime)=> { + saveState = version => { if (ipcRenderer) { + let state = { + Release: this.state.Release, + Version: version || this.state.Version, + }; + for (const provider of Constants.PROVIDER_LIST) { + state[provider] = this.state[provider]; + } + ipcRenderer.send(Constants.IPC_Save_State, { Directory: Constants.DATA_LOCATIONS[this.props.platform], - State: { - Hyperspace: hyperspace, - Release: release, - Sia: sia, - SiaPrime: siaPrime, - Version: version, - } + State: state }); } }; @@ -590,7 +574,7 @@ class App extends Component { render() { const selectedVersion = (this.state.Version === -1) ? 'unavailable' : - this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; + this.getSelectedVersion(); const downloadEnabled = this.state.AllowDownload && !this.state.MountsBusy && @@ -697,9 +681,15 @@ class App extends Component { )); if (allowMount) { + let providerProps = {}; + for (const provider of Constants.PROVIDER_LIST) { + const providerLower = provider.toLowerCase(); + providerProps[providerLower] = this.state[provider]; + } mainContent.push((
-
)); diff --git a/src/constants.js b/src/constants.js index 407c7a5..ba7fdf0 100644 --- a/src/constants.js +++ b/src/constants.js @@ -9,6 +9,12 @@ exports.DATA_LOCATIONS = { }; exports.UI_RELEASES_URL = 'https://bitbucket.org/blockstorage/repertory-ui/raw/master/releases.json'; +exports.PROVIDER_LIST = [ + 'Hyperspace', + 'Sia', + 'SiaPrime' +]; + exports.IPC_Check_Dependency_Installed = 'check_dependency_installed'; exports.IPC_Check_Dependency_Installed_Reply = 'check_dependency_installed'; diff --git a/src/containers/MountItems/MountItems.js b/src/containers/MountItems/MountItems.js index 3b93e23..9f1dfe9 100644 --- a/src/containers/MountItems/MountItems.js +++ b/src/containers/MountItems/MountItems.js @@ -14,6 +14,14 @@ if (!process.versions.hasOwnProperty('electron')) { class MountItems extends Component { constructor(props) { super(props); + for (const provider of Constants.PROVIDER_LIST) { + this.state[provider] = { + AllowMount: false, + DriveLetters: [], + Mounted: false, + PID: -1, + }; + } if (ipcRenderer) { ipcRenderer.on(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply); ipcRenderer.on(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply); @@ -23,26 +31,7 @@ class MountItems extends Component { } } - state = { - Hyperspace: { - AllowMount: false, - DriveLetters: [], - Mounted: false, - PID: -1, - }, - Sia: { - AllowMount: false, - DriveLetters: [], - Mounted: false, - PID: -1, - }, - SiaPrime: { - AllowMount: false, - DriveLetters: [], - Mounted: false, - PID: -1, - }, - }; + state = {}; componentWillUnmount = () => { if (ipcRenderer) { @@ -105,34 +94,34 @@ class MountItems extends Component { if (arg.data.Success) { let state = {}; let mountsBusy = false; - for (const name in this.state) { - state[name] = { - ...this.state[name], + for (const provider of Constants.PROVIDER_LIST) { + state[provider] = { + ...this.state[provider], AllowMount: true, - DriveLetters: (arg.data.DriveLetters[name]), - Mounted: (arg.data.Locations[name].length > 0), - PID: arg.data.PIDS[name], + DriveLetters: (arg.data.DriveLetters[provider]), + Mounted: (arg.data.Locations[provider].length > 0), + PID: arg.data.PIDS[provider], }; - mountsBusy = mountsBusy || state[name].Mounted; + mountsBusy = mountsBusy || state[provider].Mounted; } this.props.mountsBusy(mountsBusy); this.setState(state, () => { - const updateMountLocation = (data, name) => { - const nameLower = name.toLowerCase(); - let location = data.Locations[name]; + const updateMountLocation = (data, provider) => { + const providerLower = provider.toLowerCase(); + let location = data.Locations[provider]; if (location.length === 0) { location = (this.props.platform === 'win32') ? - this.props[nameLower].MountLocation || data.DriveLetters[name][0] : - this.props[nameLower].MountLocation; + this.props[providerLower].MountLocation || data.DriveLetters[provider][0] : + this.props[providerLower].MountLocation; } - if (location !== this.props[nameLower].MountLocation) { - this.props.changed(name, location); + if (location !== this.props[providerLower].MountLocation) { + this.props.changed(provider, location); } }; - for (const name in this.state) { - updateMountLocation(arg.data, name); + for (const provider of Constants.PROVIDER_LIST) { + updateMountLocation(arg.data, provider); } this.performAutoMount(); @@ -166,41 +155,41 @@ class MountItems extends Component { performAutoMount = ()=> { if (this.props.processAutoMount) { this.props.autoMountProcessed(); - const processAutoMount = (name) => { - const nameLower = name.toLowerCase(); - if (this.props[nameLower].AutoMount && - !this.state[name].Mounted && - (this.props[nameLower].MountLocation.length > 0)) { - this.handleMountUnMount(name, true, this.props[nameLower].MountLocation); + const processAutoMount = (provider) => { + const providerLower = provider.toLowerCase(); + if (this.props[providerLower].AutoMount && + !this.state[provider].Mounted && + (this.props[providerLower].MountLocation.length > 0)) { + this.handleMountUnMount(provider, true, this.props[providerLower].MountLocation); } }; - for (const name in this.state) { - processAutoMount(name); + for (const provider of Constants.PROVIDER_LIST) { + processAutoMount(provider); } } }; render() { let items = []; - for (const name in this.state) { - const nameLower = name.toLowerCase(); + for (const provider of Constants.PROVIDER_LIST) { + const providerLower = provider.toLowerCase(); items.push(( this.props.autoMountChanged(name, e)} - autoRestart={this.props[nameLower].AutoRestart} - autoRestartChanged={(e)=>this.props.autoRestartChanged(name, e)} - changed={(e) => this.handleMountLocationChanged(name, e.target.value)} + allowMount={this.state[provider].AllowMount} + autoMount={this.props[providerLower].AutoMount} + autoMountChanged={(e)=>this.props.autoMountChanged(provider, e)} + autoRestart={this.props[providerLower].AutoRestart} + autoRestartChanged={(e)=>this.props.autoRestartChanged(provider, e)} + changed={(e) => this.handleMountLocationChanged(provider, e.target.value)} clicked={this.handleMountUnMount} - configClicked={()=>this.props.configClicked(name)} - items={this.state[name].DriveLetters} + configClicked={()=>this.props.configClicked(provider)} + items={this.state[provider].DriveLetters} key={'mi_' + items.length} - location={this.props[nameLower].MountLocation} - mounted={this.state[name].Mounted} - pid={this.state[name].PID} + location={this.props[providerLower].MountLocation} + mounted={this.state[provider].Mounted} + pid={this.state[provider].PID} platform={this.props.platform} - title={name} /> + title={provider} /> )); if (items.length !== this.state.length) { items.push(