insomnia/app/ui/components/editors/body/FileEditor.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-11-22 22:26:52 +00:00
import fs from 'fs';
import electron from 'electron';
import React, {PropTypes, Component} from 'react';
import FileInputButton from '../../base/FileInputButton';
import PromptButton from '../../base/PromptButton';
2016-11-22 22:26:52 +00:00
import * as misc from '../../../../common/misc';
import {trackEvent} from '../../../../analytics/index';
2016-11-22 22:26:52 +00:00
class FileEditor extends Component {
2016-11-28 07:12:17 +00:00
_handleResetFile = () => {
this.props.onChange('');
trackEvent('File Editor', 'Reset');
};
_handleChooseFile = path => {
this.props.onChange(path);
trackEvent('File Editor', 'Choose');
};
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 (
<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 ? (
<code className="block txt-sm">
2016-11-22 22:26:52 +00:00
<span className="force-wrap selectable" title={path}>
{pathDescription}
</span>
{" "}
<span className="no-wrap">({sizeDescription})</span>
2016-11-22 22:26:52 +00:00
</code>
) : (
<code className="super-faint block txt-sm">
No file selected
</code>
2016-11-22 22:26:52 +00:00
)}
</div>
<div>
<PromptButton className="btn btn--super-compact"
disabled={!path}
2016-11-28 07:12:17 +00:00
onClick={this._handleResetFile}>
Reset File
</PromptButton>
&nbsp;&nbsp;
<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}
/>
</div>
2016-11-22 22:26:52 +00:00
</div>
)
}
}
FileEditor.propTypes = {
onChange: PropTypes.func.isRequired,
path: PropTypes.string.isRequired,
};
export default FileEditor;