Refactoring

This commit is contained in:
Scott E. Graves
2018-12-08 23:36:43 -06:00
parent ff87b699ab
commit e95477f421
3 changed files with 107 additions and 125 deletions

View File

@@ -28,6 +28,14 @@ class App extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
for (const provider of Constants.PROVIDER_LIST) {
this.state[provider] = {
AutoMount: false,
AutoRestart: false,
MountLocation: '',
}
}
if (ipcRenderer) { if (ipcRenderer) {
ipcRenderer.on(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply); ipcRenderer.on(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply);
ipcRenderer.on(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete); ipcRenderer.on(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete);
@@ -60,15 +68,9 @@ class App extends Component {
DownloadingRelease: false, DownloadingRelease: false,
DownloadingUpgrade: false, DownloadingUpgrade: false,
ExtractActive: false, ExtractActive: false,
Hyperspace: {
AutoMount: false,
AutoRestart: false,
MountLocation: '',
},
LocationsLookup: {}, LocationsLookup: {},
MissingDependencies: [], MissingDependencies: [],
MountsBusy: false, MountsBusy: false,
Platform: 'unknown',
Release: 3, Release: 3,
ReleaseTypes: [ ReleaseTypes: [
'Release', 'Release',
@@ -77,16 +79,6 @@ class App extends Component {
'Alpha', 'Alpha',
], ],
InstalledVersion: 'none', InstalledVersion: 'none',
Sia: {
AutoMount: false,
AutoRestart: false,
MountLocation: '',
},
SiaPrime: {
AutoMount: false,
AutoRestart: false,
MountLocation: '',
},
UpgradeAvailable: false, UpgradeAvailable: false,
UpgradeData: {}, UpgradeData: {},
UpgradeDismissed: false, UpgradeDismissed: false,
@@ -112,7 +104,7 @@ class App extends Component {
this.setState({ this.setState({
AllowDownload: false, AllowDownload: false,
}, ()=> { }, ()=> {
const selectedVersion = this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; const selectedVersion = this.getSelectedVersion();
if (selectedVersion !== 'unavailable') { if (selectedVersion !== 'unavailable') {
if (ipcRenderer) { if (ipcRenderer) {
let dependencies = []; 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 = () => { grabReleases = () => {
if (this.props.platform !== 'unknown') { if (this.props.platform !== 'unknown') {
if (ipcRenderer) { if (ipcRenderer) {
@@ -178,7 +174,7 @@ class App extends Component {
this.setState({ this.setState({
[storageType]: state, [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({ this.setState({
[storageType]: state, [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({ this.setState({
[storageType]: state, [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, Release: val,
Version: versionIndex Version: versionIndex
}, ()=> { }, ()=> {
this.saveState(this.state.Release, this.state.Version, this.state.Sia, this.state.Hyperspace, this.state.SiaPrime); this.saveState();
this.checkVersionInstalled( ); this.checkVersionInstalled( );
}); });
}; };
handleReleaseDownload = () => { handleReleaseDownload = () => {
const selectedVersion = this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; const selectedVersion = this.getSelectedVersion();
const fileName = selectedVersion + '.zip'; const fileName = selectedVersion + '.zip';
if (ipcRenderer) { if (ipcRenderer) {
this.setState({ this.setState({
@@ -289,7 +285,7 @@ class App extends Component {
this.setState({ this.setState({
Version: parseInt(e.target.value, 10), 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( ); this.checkVersionInstalled( );
}); });
}; };
@@ -335,7 +331,7 @@ class App extends Component {
onDownloadFileComplete = (event, arg) => { onDownloadFileComplete = (event, arg) => {
if (this.state.DownloadingRelease) { if (this.state.DownloadingRelease) {
if (arg.data.Success) { 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, { ipcRenderer.send(Constants.IPC_Extract_Release, {
Directory: Constants.DATA_LOCATIONS[this.props.platform], Directory: Constants.DATA_LOCATIONS[this.props.platform],
Source: arg.data.Destination, Source: arg.data.Destination,
@@ -406,37 +402,23 @@ class App extends Component {
onGetStateReply = (event, arg) => { onGetStateReply = (event, arg) => {
if (arg.data) { if (arg.data) {
let hyperspace = arg.data.Hyperspace || this.state.Hyperspace; let state = {
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,
Release: arg.data.Release, Release: arg.data.Release,
Sia: sia,
SiaPrime: siaPrime,
Version: arg.data.Version, 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(); this.grabReleases();
}); });
} else { } else {
@@ -450,7 +432,7 @@ class App extends Component {
let version = this.state.Version; let version = this.state.Version;
if ((version === -1) || !versionLookup[this.state.ReleaseTypes[this.state.Release]][version]) { if ((version === -1) || !versionLookup[this.state.ReleaseTypes[this.state.Release]][version]) {
version = latestVersion; version = latestVersion;
this.saveState(this.state.Release, version, this.state.Sia, this.state.Hyperspace, this.state.SiaPrime); this.saveState(version);
} }
this.setState({ this.setState({
@@ -541,17 +523,19 @@ class App extends Component {
}); });
}; };
saveState = (release, version, sia, hyperspace, siaPrime)=> { saveState = version => {
if (ipcRenderer) { 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, { ipcRenderer.send(Constants.IPC_Save_State, {
Directory: Constants.DATA_LOCATIONS[this.props.platform], Directory: Constants.DATA_LOCATIONS[this.props.platform],
State: { State: state
Hyperspace: hyperspace,
Release: release,
Sia: sia,
SiaPrime: siaPrime,
Version: version,
}
}); });
} }
}; };
@@ -590,7 +574,7 @@ class App extends Component {
render() { render() {
const selectedVersion = (this.state.Version === -1) ? const selectedVersion = (this.state.Version === -1) ?
'unavailable' : 'unavailable' :
this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]][this.state.Version]; this.getSelectedVersion();
const downloadEnabled = this.state.AllowDownload && const downloadEnabled = this.state.AllowDownload &&
!this.state.MountsBusy && !this.state.MountsBusy &&
@@ -697,9 +681,15 @@ class App extends Component {
)); ));
if (allowMount) { if (allowMount) {
let providerProps = {};
for (const provider of Constants.PROVIDER_LIST) {
const providerLower = provider.toLowerCase();
providerProps[providerLower] = this.state[provider];
}
mainContent.push(( mainContent.push((
<div key={'md_' + key++}> <div key={'md_' + key++}>
<MountItems allowConfig={allowConfig} <MountItems {...providerProps}
allowConfig={allowConfig}
allowSiaPrime={allowSiaPrime} allowSiaPrime={allowSiaPrime}
autoMountChanged={this.handleAutoMountChanged} autoMountChanged={this.handleAutoMountChanged}
autoMountProcessed={this.notifyAutoMountProcessed} autoMountProcessed={this.notifyAutoMountProcessed}
@@ -708,12 +698,9 @@ class App extends Component {
configClicked={this.handleConfigClicked} configClicked={this.handleConfigClicked}
directory={Constants.DATA_LOCATIONS[this.props.platform]} directory={Constants.DATA_LOCATIONS[this.props.platform]}
errorHandler={this.setErrorState} errorHandler={this.setErrorState}
hyperspace={this.state.Hyperspace}
mountsBusy={this.notifyMountsBusy} mountsBusy={this.notifyMountsBusy}
platform={this.props.platform} platform={this.props.platform}
processAutoMount={!this.state.AutoMountProcessed} processAutoMount={!this.state.AutoMountProcessed}
sia={this.state.Sia}
siaprime={this.state.SiaPrime}
version={this.state.InstalledVersion}/> version={this.state.InstalledVersion}/>
</div> </div>
)); ));

View File

@@ -9,6 +9,12 @@ exports.DATA_LOCATIONS = {
}; };
exports.UI_RELEASES_URL = 'https://bitbucket.org/blockstorage/repertory-ui/raw/master/releases.json'; 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 = 'check_dependency_installed';
exports.IPC_Check_Dependency_Installed_Reply = 'check_dependency_installed'; exports.IPC_Check_Dependency_Installed_Reply = 'check_dependency_installed';

View File

@@ -14,6 +14,14 @@ if (!process.versions.hasOwnProperty('electron')) {
class MountItems extends Component { class MountItems extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
for (const provider of Constants.PROVIDER_LIST) {
this.state[provider] = {
AllowMount: false,
DriveLetters: [],
Mounted: false,
PID: -1,
};
}
if (ipcRenderer) { if (ipcRenderer) {
ipcRenderer.on(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply); ipcRenderer.on(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply);
ipcRenderer.on(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply); ipcRenderer.on(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply);
@@ -23,26 +31,7 @@ class MountItems extends Component {
} }
} }
state = { 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,
},
};
componentWillUnmount = () => { componentWillUnmount = () => {
if (ipcRenderer) { if (ipcRenderer) {
@@ -105,34 +94,34 @@ class MountItems extends Component {
if (arg.data.Success) { if (arg.data.Success) {
let state = {}; let state = {};
let mountsBusy = false; let mountsBusy = false;
for (const name in this.state) { for (const provider of Constants.PROVIDER_LIST) {
state[name] = { state[provider] = {
...this.state[name], ...this.state[provider],
AllowMount: true, AllowMount: true,
DriveLetters: (arg.data.DriveLetters[name]), DriveLetters: (arg.data.DriveLetters[provider]),
Mounted: (arg.data.Locations[name].length > 0), Mounted: (arg.data.Locations[provider].length > 0),
PID: arg.data.PIDS[name], PID: arg.data.PIDS[provider],
}; };
mountsBusy = mountsBusy || state[name].Mounted; mountsBusy = mountsBusy || state[provider].Mounted;
} }
this.props.mountsBusy(mountsBusy); this.props.mountsBusy(mountsBusy);
this.setState(state, () => { this.setState(state, () => {
const updateMountLocation = (data, name) => { const updateMountLocation = (data, provider) => {
const nameLower = name.toLowerCase(); const providerLower = provider.toLowerCase();
let location = data.Locations[name]; let location = data.Locations[provider];
if (location.length === 0) { if (location.length === 0) {
location = (this.props.platform === 'win32') ? location = (this.props.platform === 'win32') ?
this.props[nameLower].MountLocation || data.DriveLetters[name][0] : this.props[providerLower].MountLocation || data.DriveLetters[provider][0] :
this.props[nameLower].MountLocation; this.props[providerLower].MountLocation;
} }
if (location !== this.props[nameLower].MountLocation) { if (location !== this.props[providerLower].MountLocation) {
this.props.changed(name, location); this.props.changed(provider, location);
} }
}; };
for (const name in this.state) { for (const provider of Constants.PROVIDER_LIST) {
updateMountLocation(arg.data, name); updateMountLocation(arg.data, provider);
} }
this.performAutoMount(); this.performAutoMount();
@@ -166,41 +155,41 @@ class MountItems extends Component {
performAutoMount = ()=> { performAutoMount = ()=> {
if (this.props.processAutoMount) { if (this.props.processAutoMount) {
this.props.autoMountProcessed(); this.props.autoMountProcessed();
const processAutoMount = (name) => { const processAutoMount = (provider) => {
const nameLower = name.toLowerCase(); const providerLower = provider.toLowerCase();
if (this.props[nameLower].AutoMount && if (this.props[providerLower].AutoMount &&
!this.state[name].Mounted && !this.state[provider].Mounted &&
(this.props[nameLower].MountLocation.length > 0)) { (this.props[providerLower].MountLocation.length > 0)) {
this.handleMountUnMount(name, true, this.props[nameLower].MountLocation); this.handleMountUnMount(provider, true, this.props[providerLower].MountLocation);
} }
}; };
for (const name in this.state) { for (const provider of Constants.PROVIDER_LIST) {
processAutoMount(name); processAutoMount(provider);
} }
} }
}; };
render() { render() {
let items = []; let items = [];
for (const name in this.state) { for (const provider of Constants.PROVIDER_LIST) {
const nameLower = name.toLowerCase(); const providerLower = provider.toLowerCase();
items.push(( items.push((
<MountItem allowConfig={this.props.allowConfig} <MountItem allowConfig={this.props.allowConfig}
allowMount={this.state[name].AllowMount} allowMount={this.state[provider].AllowMount}
autoMount={this.props[nameLower].AutoMount} autoMount={this.props[providerLower].AutoMount}
autoMountChanged={(e)=>this.props.autoMountChanged(name, e)} autoMountChanged={(e)=>this.props.autoMountChanged(provider, e)}
autoRestart={this.props[nameLower].AutoRestart} autoRestart={this.props[providerLower].AutoRestart}
autoRestartChanged={(e)=>this.props.autoRestartChanged(name, e)} autoRestartChanged={(e)=>this.props.autoRestartChanged(provider, e)}
changed={(e) => this.handleMountLocationChanged(name, e.target.value)} changed={(e) => this.handleMountLocationChanged(provider, e.target.value)}
clicked={this.handleMountUnMount} clicked={this.handleMountUnMount}
configClicked={()=>this.props.configClicked(name)} configClicked={()=>this.props.configClicked(provider)}
items={this.state[name].DriveLetters} items={this.state[provider].DriveLetters}
key={'mi_' + items.length} key={'mi_' + items.length}
location={this.props[nameLower].MountLocation} location={this.props[providerLower].MountLocation}
mounted={this.state[name].Mounted} mounted={this.state[provider].Mounted}
pid={this.state[name].PID} pid={this.state[provider].PID}
platform={this.props.platform} platform={this.props.platform}
title={name} /> title={provider} />
)); ));
if (items.length !== this.state.length) { if (items.length !== this.state.length) {
items.push(<div key={'di_' + items.length} items.push(<div key={'di_' + items.length}