306 lines
9.7 KiB
JavaScript
306 lines
9.7 KiB
JavaScript
import React from 'react';
|
|
import {Component} from 'react';
|
|
import Box from '../../components/UI/Box/Box';
|
|
import Button from '../../components/UI/Button/Button';
|
|
import CSSModules from 'react-css-modules';
|
|
import styles from './MountItems.css';
|
|
import Modal from '../../components/UI/Modal/Modal';
|
|
import MountItem from '../../components/MountItem/MountItem';
|
|
import RootElem from '../../hoc/RootElem/RootElem';
|
|
|
|
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);
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
this.state[provider] = {
|
|
AllowMount: false,
|
|
DriveLetters: [],
|
|
Mounted: false,
|
|
};
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
|
|
retryIntervals = {};
|
|
|
|
state = {
|
|
DisplayRetry: false,
|
|
RetryItems: {},
|
|
};
|
|
|
|
cancelRetryMount = (storageType, stateCallback) => {
|
|
clearInterval(this.retryIntervals[storageType]);
|
|
delete this.retryIntervals[storageType];
|
|
|
|
if (stateCallback) {
|
|
let retryItems = {
|
|
...this.state.RetryItems,
|
|
};
|
|
delete retryItems[storageType];
|
|
this.setState({
|
|
DisplayRetry: Object.keys(retryItems).length > 0,
|
|
RetryItems: retryItems,
|
|
}, stateCallback);
|
|
}
|
|
};
|
|
|
|
componentWillUnmount = () => {
|
|
for (const storageType in this.state.RetryItems) {
|
|
if (this.state.RetryItems.hasOwnProperty(storageType)) {
|
|
this.cancelRetryMount(storageType);
|
|
}
|
|
}
|
|
|
|
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 = (storageType, value) => {
|
|
if (this.props.platform === 'win32') {
|
|
this.props.changed(storageType, this.state[storageType].DriveLetters[value]);
|
|
}
|
|
else {
|
|
this.props.changed(storageType, value);
|
|
}
|
|
};
|
|
|
|
handleMountUnMount = (storageType, mount, location) => {
|
|
if (!location || (location.trim().length === 0)) {
|
|
this.props.errorHandler('Mount location is not set');
|
|
} else {
|
|
if (ipcRenderer) {
|
|
let allowAction = true;
|
|
if (mount && (this.props.platform !== 'win32')) {
|
|
const result = ipcRenderer.sendSync(Constants.IPC_Check_Mount_Location, {
|
|
Location: location,
|
|
});
|
|
if (!result.Success) {
|
|
allowAction = false;
|
|
this.props.errorHandler(result.Error.toString());
|
|
}
|
|
}
|
|
|
|
if (allowAction) {
|
|
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,
|
|
NoConsoleSupported: this.props.noConsoleSupported,
|
|
StorageType: storageType,
|
|
Version: this.props.version,
|
|
});
|
|
} else {
|
|
ipcRenderer.send(Constants.IPC_Unmount_Drive, {
|
|
Directory: this.props.directory,
|
|
Location: location,
|
|
StorageType: storageType,
|
|
Version: this.props.version,
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
onDetectMountsReply = (event, arg) => {
|
|
if (arg.data.Success) {
|
|
let state = {};
|
|
let mountsBusy = false;
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
state[provider] = {
|
|
...this.state[provider],
|
|
AllowMount: true,
|
|
DriveLetters: (arg.data.DriveLetters[provider]),
|
|
Mounted: (arg.data.Locations[provider].length > 0),
|
|
};
|
|
mountsBusy = mountsBusy || state[provider].Mounted;
|
|
}
|
|
this.props.mountsBusy(mountsBusy);
|
|
|
|
this.setState(state, () => {
|
|
const updateMountLocation = (data, provider) => {
|
|
const providerLower = provider.toLowerCase();
|
|
let location = data.Locations[provider];
|
|
if (location.length === 0) {
|
|
location = (this.props.platform === 'win32') ?
|
|
this.props[providerLower].MountLocation || data.DriveLetters[provider][0] :
|
|
this.props[providerLower].MountLocation;
|
|
}
|
|
if (location !== this.props[providerLower].MountLocation) {
|
|
this.props.changed(provider, location);
|
|
}
|
|
};
|
|
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
updateMountLocation(arg.data, provider);
|
|
}
|
|
|
|
this.performAutoMount();
|
|
});
|
|
} else {
|
|
this.props.errorHandler(arg.data.Error);
|
|
}
|
|
};
|
|
|
|
onMountDriveReply = (event, arg) => {
|
|
const state = {
|
|
...this.state[arg.data.StorageType],
|
|
Mounted: arg.data.Success,
|
|
};
|
|
this.setState({
|
|
[arg.data.StorageType]: state,
|
|
}, ()=> {
|
|
this.detectMounts();
|
|
});
|
|
};
|
|
|
|
onUnmountDriveReply = (event, arg) => {
|
|
if (arg && arg.data && !arg.data.Expected && arg.data.Location && this.props[arg.data.StorageType.toLowerCase()].AutoRestart) {
|
|
const storageType = arg.data.StorageType;
|
|
if (!this.state.RetryItems[storageType]) {
|
|
let retryItems = {
|
|
...this.state.RetryItems
|
|
};
|
|
retryItems[storageType] = {
|
|
RetrySeconds: 10,
|
|
};
|
|
this.setState({
|
|
DisplayRetry: true,
|
|
RetryItems: retryItems,
|
|
}, () => {
|
|
this.retryIntervals[storageType] = setInterval(() => {
|
|
let retryItems = {
|
|
...this.state.RetryItems,
|
|
};
|
|
const retrySeconds = retryItems[storageType].RetrySeconds - 1;
|
|
if (retrySeconds === 0) {
|
|
this.cancelRetryMount(storageType, () => {
|
|
this.handleMountUnMount(storageType, true, arg.data.Location);
|
|
});
|
|
} else {
|
|
retryItems[storageType].RetrySeconds = retrySeconds;
|
|
this.setState({
|
|
RetryItems: retryItems,
|
|
});
|
|
}
|
|
},1000);
|
|
});
|
|
}
|
|
} else {
|
|
this.detectMounts();
|
|
}
|
|
};
|
|
|
|
performAutoMount = ()=> {
|
|
if (this.props.processAutoMount) {
|
|
this.props.autoMountProcessed();
|
|
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 provider of Constants.PROVIDER_LIST) {
|
|
processAutoMount(provider);
|
|
}
|
|
}
|
|
};
|
|
|
|
render() {
|
|
let retryDisplay;
|
|
if (this.state.DisplayRetry) {
|
|
let retryList = [];
|
|
for (const storageType in this.state.RetryItems) {
|
|
if (this.state.RetryItems.hasOwnProperty(storageType)) {
|
|
retryList.push((
|
|
<RootElem key={retryList.length}>
|
|
<p>Retrying {storageType} in {this.state.RetryItems[storageType].RetrySeconds}(s)</p>
|
|
<Button clicked={()=>this.cancelRetryMount(storageType, ()=> this.detectMounts())}>Cancel {storageType}</Button>
|
|
</RootElem>
|
|
));
|
|
}
|
|
}
|
|
|
|
retryDisplay = (
|
|
<Modal>
|
|
<Box dxStyle={{padding: '8px', minWidth: '60vw'}}>
|
|
<h1 style={{textAlign: 'center'}}>Mount Failed</h1>
|
|
{retryList}
|
|
</Box>
|
|
</Modal>
|
|
)
|
|
}
|
|
|
|
let items = [];
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
const providerLower = provider.toLowerCase();
|
|
items.push((
|
|
<MountItem allowConfig={this.props.allowConfig}
|
|
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(provider)}
|
|
items={this.state[provider].DriveLetters}
|
|
key={'mi_' + items.length}
|
|
location={this.props[providerLower].MountLocation}
|
|
mounted={this.state[provider].Mounted}
|
|
platform={this.props.platform}
|
|
title={provider} />
|
|
));
|
|
if (items.length !== this.state.length) {
|
|
items.push(<div key={'di_' + items.length}
|
|
style={{paddingTop: '12px'}} />)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div styleName='MountItems'>
|
|
{retryDisplay}
|
|
{items}
|
|
</div>);
|
|
}
|
|
}
|
|
|
|
export default CSSModules(MountItems, styles, {allowMultiple: true}); |