import PropTypes from 'prop-types' import React from 'react' import CSSModules from 'browser/lib/CSSModules' import styles from './StoragesTab.styl' import dataApi from 'browser/main/lib/dataApi' import StorageItem from './StorageItem' const electron = require('electron') const { shell, remote } = electron function browseFolder () { const dialog = remote.dialog const defaultPath = remote.app.getPath('home') return new Promise((resolve, reject) => { dialog.showOpenDialog({ title: 'Select Directory', defaultPath, properties: ['openDirectory', 'createDirectory'] }, function (targetPaths) { if (targetPaths == null) return resolve('') resolve(targetPaths[0]) }) }) } class StoragesTab extends React.Component { constructor (props) { super(props) this.state = { page: 'LIST', newStorage: { name: 'Unnamed', type: 'FILESYSTEM', path: '' } } } handleAddStorageButton (e) { this.setState({ page: 'ADD_STORAGE', newStorage: { name: 'Unnamed', type: 'FILESYSTEM', path: '' } }, () => { this.refs.addStorageName.select() }) } handleLinkClick (e) { shell.openExternal(e.currentTarget.href) e.preventDefault() } renderList () { const { data, boundingBox } = this.props if (!boundingBox) { return null } const storageList = data.storageMap.map((storage) => { return }) return (
Storages
{storageList.length > 0 ? storageList :
No storage found.
}
) } handleAddStorageBrowseButtonClick (e) { browseFolder() .then((targetPath) => { if (targetPath.length > 0) { const { newStorage } = this.state newStorage.path = targetPath this.setState({ newStorage }) } }) .catch((err) => { console.error('BrowseFAILED') console.error(err) }) } handleAddStorageChange (e) { const { newStorage } = this.state newStorage.name = this.refs.addStorageName.value newStorage.path = this.refs.addStoragePath.value this.setState({ newStorage }) } handleAddStorageCreateButton (e) { dataApi .addStorage({ name: this.state.newStorage.name, path: this.state.newStorage.path }) .then((data) => { const { dispatch } = this.props dispatch({ type: 'ADD_STORAGE', storage: data.storage, notes: data.notes }) this.setState({ page: 'LIST' }) }) } handleAddStorageCancelButton (e) { this.setState({ page: 'LIST' }) } renderAddStorage () { return (
Add Storage
Name
this.handleAddStorageChange(e)} />
Type
Setting up 3rd-party cloud storage integration: this.handleLinkClick(e)} >Cloud-Syncing-and-Backup
Location
this.handleAddStorageChange(e)} />
) } renderContent () { switch (this.state.page) { case 'ADD_STORAGE': case 'ADD_FOLDER': return this.renderAddStorage() case 'LIST': default: return this.renderList() } } render () { return (
{this.renderContent()}
) } } StoragesTab.propTypes = { boundingBox: PropTypes.shape({ bottom: PropTypes.number, height: PropTypes.number, left: PropTypes.number, right: PropTypes.number, top: PropTypes.number, width: PropTypes.number }), dispatch: PropTypes.func } export default CSSModules(StoragesTab, styles)