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 (
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'}/>
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'}/>
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'}/>
); } } export default CSSModules(MountItems, styles, {allowMultiple: true});