2017-08-10 01:56:27 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-11-22 22:26:52 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import electron from 'electron';
|
2017-03-08 05:52:17 +00:00
|
|
|
import FileInputButton from '../../base/file-input-button';
|
|
|
|
import PromptButton from '../../base/prompt-button';
|
2016-11-22 22:26:52 +00:00
|
|
|
import * as misc from '../../../../common/misc';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {trackEvent} from '../../../../analytics/index';
|
2016-11-22 22:26:52 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class FileEditor extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleResetFile () {
|
2016-11-28 07:12:17 +00:00
|
|
|
this.props.onChange('');
|
|
|
|
trackEvent('File Editor', 'Reset');
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-28 07:12:17 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleChooseFile (path) {
|
2016-11-28 07:12:17 +00:00
|
|
|
this.props.onChange(path);
|
|
|
|
trackEvent('File Editor', 'Choose');
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-11-28 07:12:17 +00:00
|
|
|
|
2016-11-22 22:26:52 +00:00
|
|
|
render () {
|
2016-11-28 07:12:17 +00:00
|
|
|
const {path} = this.props;
|
2016-11-22 22:26:52 +00:00
|
|
|
|
|
|
|
// Replace home path with ~/ to make the path shorter
|
|
|
|
const homeDirectory = electron.remote.app.getPath('home');
|
|
|
|
const pathDescription = path.replace(homeDirectory, '~');
|
|
|
|
|
|
|
|
let sizeDescription = '';
|
|
|
|
try {
|
|
|
|
const bytes = fs.statSync(path).size;
|
|
|
|
sizeDescription = misc.describeByteSize(bytes);
|
|
|
|
} catch (e) {
|
|
|
|
sizeDescription = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2016-11-24 01:12:13 +00:00
|
|
|
<div className="text-center">
|
|
|
|
<div className="pad text-left">
|
|
|
|
<label className="label--small">Selected File</label>
|
2016-11-22 22:26:52 +00:00
|
|
|
{path ? (
|
2016-11-24 01:12:13 +00:00
|
|
|
<code className="block txt-sm">
|
2016-11-22 22:26:52 +00:00
|
|
|
<span className="force-wrap selectable" title={path}>
|
|
|
|
{pathDescription}
|
|
|
|
</span>
|
2017-03-03 20:09:08 +00:00
|
|
|
{' '}
|
2016-11-24 01:12:13 +00:00
|
|
|
<span className="no-wrap">({sizeDescription})</span>
|
2016-11-22 22:26:52 +00:00
|
|
|
</code>
|
|
|
|
) : (
|
2016-11-24 01:12:13 +00:00
|
|
|
<code className="super-faint block txt-sm">
|
|
|
|
No file selected
|
|
|
|
</code>
|
2016-11-22 22:26:52 +00:00
|
|
|
)}
|
2016-11-24 01:12:13 +00:00
|
|
|
</div>
|
2016-11-23 19:33:24 +00:00
|
|
|
<div>
|
|
|
|
<PromptButton className="btn btn--super-compact"
|
|
|
|
disabled={!path}
|
2016-11-28 07:12:17 +00:00
|
|
|
onClick={this._handleResetFile}>
|
2016-11-23 19:33:24 +00:00
|
|
|
Reset File
|
|
|
|
</PromptButton>
|
|
|
|
|
|
|
|
<FileInputButton
|
|
|
|
path={path}
|
2016-11-26 00:49:38 +00:00
|
|
|
className="btn btn--clicky"
|
2016-11-28 07:12:17 +00:00
|
|
|
onChange={this._handleChooseFile}
|
2016-11-23 19:33:24 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2016-11-22 22:26:52 +00:00
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-22 22:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileEditor.propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-03-03 20:09:08 +00:00
|
|
|
path: PropTypes.string.isRequired
|
2016-11-22 22:26:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default FileEditor;
|