This repository has been archived on 2025-09-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
repertory-ui/src/containers/MountItems/MountItems.js
Scott E. Graves 2f24544400 SiaPrime support
2018-11-03 12:29:31 -05:00

252 lines
8.6 KiB
JavaScript

import React from 'react';
import {Component} from 'react';
import CSSModules from 'react-css-modules';
import styles from './MountItems.css';
import MountItem from '../../components/MountItem/MountItem';
const Constants = require('../../constants');
let ipcRenderer = null;
if (!process.versions.hasOwnProperty('electron')) {
ipcRenderer = ((window && window.require) ? window.require('electron').ipcRenderer : null);
}
class MountItems extends Component {
constructor(props) {
super(props);
if (ipcRenderer) {
ipcRenderer.on(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply);
ipcRenderer.on(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply);
ipcRenderer.on(Constants.IPC_Unmount_Drive_Reply, this.onUnmountDriveReply);
this.detectMounts();
}
}
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 = () => {
if (ipcRenderer) {
ipcRenderer.removeListener(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply);
ipcRenderer.removeListener(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply);
ipcRenderer.removeListener(Constants.IPC_Unmount_Drive_Reply, this.onUnmountDriveReply);
}
};
detectMounts = ()=> {
this.props.mountsBusy(true);
ipcRenderer.send(Constants.IPC_Detect_Mounts, {
Directory: this.props.directory,
Version: this.props.version,
});
};
handleMountLocationChanged = (systemType, value) => {
if (this.props.platform === 'win32') {
this.props.changed(systemType, this.state[systemType].DriveLetters[value]);
}
else {
this.props.changed(systemType, value);
}
};
handleMountUnMount = (storageType, mount, location, pid) => {
if (ipcRenderer) {
const state = {
...this.state[storageType],
AllowMount: false,
};
this.props.mountsBusy(true);
this.setState({
[storageType]: state,
}, ()=> {
if (mount) {
ipcRenderer.send(Constants.IPC_Mount_Drive, {
Directory: this.props.directory,
Location: location,
StorageType: storageType,
Version: this.props.version,
});
} else {
ipcRenderer.send(Constants.IPC_Unmount_Drive, {
Directory: this.props.directory,
Location: location,
PID: pid,
StorageType: storageType,
Version: this.props.version,
});
}
});
}
};
onDetectMountsReply = (event, arg) => {
if (arg.data.Success) {
const sia = {
...this.state.Sia,
AllowMount: true,
DriveLetters: (arg.data.DriveLetters.Sia),
Mounted: (arg.data.Locations.Sia.length > 0),
PID: arg.data.PIDS.Sia,
};
const siaPrime = {
...this.state.SiaPrime,
AllowMount: true,
DriveLetters: (arg.data.DriveLetters.SiaPrime),
Mounted: (arg.data.Locations.SiaPrime.length > 0),
PID: arg.data.PIDS.SiaPrime,
};
const hs = {
...this.state.Hyperspace,
AllowMount: true,
DriveLetters: (arg.data.DriveLetters.Hyperspace),
Mounted: (arg.data.Locations.Hyperspace.length > 0),
PID: arg.data.PIDS.Hyperspace,
};
this.props.mountsBusy(hs.Mounted || sia.Mounted);
this.setState({
Hyperspace: hs,
Sia: sia,
SiaPrime: siaPrime,
}, () => {
let hsLocation = arg.data.Locations.Hyperspace;
if ((hsLocation.length === 0) && (this.props.platform === 'win32')) {
hsLocation = this.props.hyperspace.MountLocation || arg.data.DriveLetters.Hyperspace[0];
}
if (hsLocation !== this.props.hyperspace.MountLocation) {
this.props.changed('Hyperspace', hsLocation);
}
let siaLocation = arg.data.Locations.Sia;
if ((siaLocation.length === 0) && (this.props.platform === 'win32')) {
siaLocation = this.props.sia.MountLocation || arg.data.DriveLetters.Sia[0];
}
if (siaLocation !== this.props.sia.MountLocation) {
this.props.changed('Sia', siaLocation);
}
let siaPrimeLocation = arg.data.Locations.SiaPrime;
if ((siaPrimeLocation.length === 0) && (this.props.platform === 'win32')) {
siaPrimeLocation = this.props.siaprime.MountLocation || arg.data.DriveLetters.SiaPrime[0];
}
if (siaPrimeLocation !== this.props.siaprime.MountLocation) {
this.props.changed('SiaPrime', siaPrimeLocation);
}
this.performAutoMount();
});
} else {
this.props.errorHandler(arg.data.Error);
}
};
onMountDriveReply = (event, arg) => {
const state = {
...this.state[arg.data.StorageType],
PID: arg.data.PID,
Mounted: arg.data.Success,
};
this.setState({
[arg.data.StorageType]: state,
}, ()=> {
this.detectMounts();
});
};
onUnmountDriveReply = (event, arg) => {
this.detectMounts();
};
performAutoMount = ()=> {
if (this.props.processAutoMount) {
this.props.autoMountProcessed();
if (this.props.hyperspace.AutoMount &&
!this.state.Hyperspace.Mounted &&
(this.props.hyperspace.MountLocation.length > 0)) {
this.handleMountUnMount('Hyperspace', true, this.props.hyperspace.MountLocation);
}
if (this.props.sia.AutoMount &&
!this.state.Sia.Mounted &&
(this.props.sia.MountLocation.length > 0)) {
this.handleMountUnMount('Sia', true, this.props.sia.MountLocation);
}
if (this.props.siaprime.AutoMount &&
!this.state.SiaPrime.Mounted &&
(this.props.siaprime.MountLocation.length > 0)) {
this.handleMountUnMount('SiaPrime', true, this.props.siaprime.MountLocation);
}
}
};
render() {
return (
<div styleName='MountItems'>
<MountItem allowConfig={this.props.allowConfig}
allowMount={this.state.Hyperspace.AllowMount}
autoMount={this.props.hyperspace.AutoMount}
autoMountChanged={(e)=>this.props.autoMountChanged('Hyperspace', e)}
changed={(e) => this.handleMountLocationChanged('Hyperspace', e.target.value)}
clicked={this.handleMountUnMount}
configClicked={()=>this.props.configClicked('Hyperspace')}
items={this.state.Hyperspace.DriveLetters}
location={this.props.hyperspace.MountLocation}
mounted={this.state.Hyperspace.Mounted}
pid={this.state.Hyperspace.PID}
platform={this.props.platform}
title={'Hyperspace'}/>
<div style={{paddingTop: '8px'}}/>
<MountItem allowConfig={this.props.allowConfig}
allowMount={this.state.Sia.AllowMount}
autoMount={this.props.sia.AutoMount}
autoMountChanged={(e)=>this.props.autoMountChanged('Sia', e)}
changed={(e) => this.handleMountLocationChanged('Sia', e.target.value)}
clicked={this.handleMountUnMount}
configClicked={()=>this.props.configClicked('Sia')}
items={this.state.Sia.DriveLetters}
location={this.props.sia.MountLocation}
mounted={this.state.Sia.Mounted}
pid={this.state.Sia.PID}
platform={this.props.platform}
title={'Sia'}/>
<div style={{paddingTop: '8px'}}/>
<MountItem allowConfig={this.props.allowConfig}
allowMount={this.state.Sia.AllowMount}
autoMount={this.props.siaprime.AutoMount}
autoMountChanged={(e)=>this.props.autoMountChanged('SiaPrime', e)}
changed={(e) => this.handleMountLocationChanged('SiaPrime', e.target.value)}
clicked={this.handleMountUnMount}
configClicked={()=>this.props.configClicked('SiaPrime')}
items={this.state.SiaPrime.DriveLetters}
location={this.props.siaprime.MountLocation}
mounted={this.state.SiaPrime.Mounted}
pid={this.state.SiaPrime.PID}
platform={this.props.platform}
title={'SiaPrime'}/>
</div>);
}
}
export default CSSModules(MountItems, styles, {allowMultiple: true});