insomnia/app/ui/components/editors/body/file-editor.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-08-10 01:56:27 +00:00
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import autobind from 'autobind-decorator';
2016-11-22 22:26:52 +00:00
import fs from 'fs';
import electron from 'electron';
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';
import {trackEvent} from '../../../../analytics/index';
2016-11-22 22:26:52 +00:00
@autobind
class FileEditor extends PureComponent {
_handleResetFile () {
2016-11-28 07:12:17 +00:00
this.props.onChange('');
trackEvent('File Editor', 'Reset');
}
2016-11-28 07:12:17 +00:00
_handleChooseFile (path) {
2016-11-28 07:12:17 +00:00
this.props.onChange(path);
trackEvent('File Editor', 'Choose');
}
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 (
<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>
);
2016-11-22 22:26:52 +00:00
}
}
FileEditor.propTypes = {
onChange: PropTypes.func.isRequired,
path: PropTypes.string.isRequired
2016-11-22 22:26:52 +00:00
};
export default FileEditor;