2017-07-20 01:55:40 +00:00
|
|
|
// @flow
|
|
|
|
import type {Plugin} from '../../../plugins/index';
|
2017-07-22 00:55:34 +00:00
|
|
|
import {getPlugins} from '../../../plugins/index';
|
2017-09-25 21:32:58 +00:00
|
|
|
import * as React from 'react';
|
2017-06-01 02:04:27 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import * as electron from 'electron';
|
|
|
|
import Button from '../base/button';
|
|
|
|
import CopyButton from '../base/copy-button';
|
2017-06-09 01:10:12 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2017-07-11 01:05:54 +00:00
|
|
|
import {reload} from '../../../templating/index';
|
2017-07-22 00:55:34 +00:00
|
|
|
import installPlugin from '../../../plugins/install';
|
|
|
|
import HelpTooltip from '../help-tooltip';
|
|
|
|
import Link from '../base/link';
|
2017-07-28 22:18:06 +00:00
|
|
|
import {delay} from '../../../common/misc';
|
2017-09-17 14:04:46 +00:00
|
|
|
import {PLUGIN_PATH} from '../../../common/constants';
|
2017-06-01 02:04:27 +00:00
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
type State = {
|
|
|
|
plugins: Array<Plugin>,
|
|
|
|
npmPluginValue: string,
|
|
|
|
error: string,
|
|
|
|
isInstallingFromNpm: boolean,
|
|
|
|
isRefreshingPlugins: boolean
|
|
|
|
};
|
2017-07-20 01:55:40 +00:00
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
@autobind
|
|
|
|
class Plugins extends React.PureComponent<void, State> {
|
2017-08-10 00:13:59 +00:00
|
|
|
_isMounted: boolean;
|
|
|
|
|
2017-07-21 18:59:17 +00:00
|
|
|
constructor (props: any) {
|
2017-06-01 02:04:27 +00:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2017-07-20 01:55:40 +00:00
|
|
|
plugins: [],
|
2017-07-22 00:55:34 +00:00
|
|
|
npmPluginValue: '',
|
|
|
|
error: '',
|
2017-07-28 22:18:06 +00:00
|
|
|
isInstallingFromNpm: false,
|
|
|
|
isRefreshingPlugins: false
|
2017-06-01 02:04:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:55:34 +00:00
|
|
|
_handleClearError () {
|
|
|
|
this.setState({error: ''});
|
|
|
|
}
|
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
_handleAddNpmPluginChange (e: Event) {
|
|
|
|
if (e.target instanceof HTMLInputElement) {
|
|
|
|
this.setState({npmPluginValue: e.target.value});
|
|
|
|
}
|
2017-07-20 01:55:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-22 00:55:34 +00:00
|
|
|
async _handleAddFromNpm (e: Event): Promise<void> {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2017-07-28 22:18:06 +00:00
|
|
|
this.setState({isInstallingFromNpm: true});
|
2017-07-22 00:55:34 +00:00
|
|
|
|
2017-07-28 22:18:06 +00:00
|
|
|
const newState = {isInstallingFromNpm: false, error: ''};
|
2017-07-22 00:55:34 +00:00
|
|
|
try {
|
|
|
|
await installPlugin(this.state.npmPluginValue);
|
|
|
|
await this._handleRefreshPlugins();
|
|
|
|
} catch (err) {
|
|
|
|
newState.error = err.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState(newState);
|
2017-07-20 01:55:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 22:18:06 +00:00
|
|
|
_handleOpenDirectory (directory: string): void {
|
2017-06-01 02:04:27 +00:00
|
|
|
electron.remote.shell.showItemInFolder(directory);
|
|
|
|
}
|
|
|
|
|
2017-07-28 22:18:06 +00:00
|
|
|
async _handleRefreshPlugins (): Promise<void> {
|
|
|
|
const start = Date.now();
|
|
|
|
|
|
|
|
this.setState({isRefreshingPlugins: true});
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Get and reload plugins
|
2017-07-20 01:55:40 +00:00
|
|
|
const plugins = await getPlugins(true);
|
2017-07-11 01:05:54 +00:00
|
|
|
reload();
|
|
|
|
|
2017-07-28 22:18:06 +00:00
|
|
|
// Delay loading for at least 500ms. UX FTW!
|
|
|
|
const delta = Date.now() - start;
|
|
|
|
await delay(500 - delta);
|
|
|
|
|
2017-08-10 00:13:59 +00:00
|
|
|
if (this._isMounted) {
|
|
|
|
this.setState({plugins, isRefreshingPlugins: false});
|
|
|
|
}
|
2017-06-01 02:04:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 01:05:14 +00:00
|
|
|
async _handleClickRefreshPlugins () {
|
|
|
|
await this._handleRefreshPlugins();
|
2017-06-09 01:10:12 +00:00
|
|
|
trackEvent('Plugins', 'Refresh');
|
2017-06-01 02:04:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 14:04:46 +00:00
|
|
|
_handleClickShowPluginsFolder () {
|
|
|
|
electron.remote.shell.showItemInFolder(PLUGIN_PATH);
|
|
|
|
trackEvent('Plugins', 'Show Folder');
|
|
|
|
}
|
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
componentDidMount () {
|
2017-08-10 00:13:59 +00:00
|
|
|
this._isMounted = true;
|
2017-07-20 01:55:40 +00:00
|
|
|
this._handleRefreshPlugins();
|
|
|
|
}
|
|
|
|
|
2017-08-10 00:13:59 +00:00
|
|
|
componentWillUnmount () {
|
|
|
|
this._isMounted = false;
|
|
|
|
}
|
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
render () {
|
2017-07-28 22:18:06 +00:00
|
|
|
const {plugins, error, isInstallingFromNpm, isRefreshingPlugins} = this.state;
|
2017-06-01 02:04:27 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p className="notice info no-margin-top">
|
2017-09-17 14:04:46 +00:00
|
|
|
Plugins is still an experimental feature. See
|
2017-07-22 00:55:34 +00:00
|
|
|
{' '}
|
2017-09-17 14:04:46 +00:00
|
|
|
<Link href="https://insomnia.rest/documentation/plugins/">
|
|
|
|
Documentation
|
|
|
|
</Link> for more info.
|
2017-06-01 02:04:27 +00:00
|
|
|
</p>
|
2017-07-28 22:18:06 +00:00
|
|
|
{plugins.length === 0 ? (
|
|
|
|
<div className="text-center faint italic pad">No Plugins Added</div>
|
|
|
|
) : (
|
|
|
|
<table className="table--fancy table--striped margin-top margin-bottom">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Version</th>
|
|
|
|
<th>Folder</th>
|
2017-06-01 02:04:27 +00:00
|
|
|
</tr>
|
2017-07-28 22:18:06 +00:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{plugins.map(plugin => (
|
|
|
|
<tr key={plugin.name}>
|
|
|
|
<td>
|
|
|
|
{plugin.name}
|
|
|
|
{plugin.description && (
|
|
|
|
<HelpTooltip info className="space-left">{plugin.description}</HelpTooltip>
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
<td>{plugin.version}</td>
|
|
|
|
<td className="no-wrap" style={{width: '10rem'}}>
|
|
|
|
<CopyButton className="btn btn--outlined btn--super-duper-compact"
|
|
|
|
title={plugin.directory}
|
|
|
|
content={plugin.directory}>
|
|
|
|
Copy Path
|
|
|
|
</CopyButton>
|
|
|
|
{' '}
|
|
|
|
<Button className="btn btn--outlined btn--super-duper-compact"
|
|
|
|
onClick={this._handleOpenDirectory}
|
|
|
|
value={plugin.directory}>
|
|
|
|
Show Folder
|
|
|
|
</Button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
)}
|
2017-07-22 00:55:34 +00:00
|
|
|
|
|
|
|
{error && (
|
|
|
|
<div className="notice error text-left margin-bottom">
|
|
|
|
<button className="pull-right icon" onClick={this._handleClearError}>
|
|
|
|
<i className="fa fa-times"/>
|
2017-07-20 01:55:40 +00:00
|
|
|
</button>
|
2017-07-22 00:55:34 +00:00
|
|
|
{error}
|
2017-07-20 01:55:40 +00:00
|
|
|
</div>
|
2017-07-22 00:55:34 +00:00
|
|
|
)}
|
|
|
|
|
|
|
|
<form onSubmit={this._handleAddFromNpm}>
|
|
|
|
<div className="form-row">
|
|
|
|
<div className="form-control form-control--outlined">
|
|
|
|
<input onChange={this._handleAddNpmPluginChange}
|
2017-07-28 22:18:06 +00:00
|
|
|
disabled={isInstallingFromNpm}
|
2017-07-22 00:55:34 +00:00
|
|
|
type="text"
|
|
|
|
placeholder="npm-package-name"/>
|
|
|
|
</div>
|
|
|
|
<div className="form-control width-auto">
|
2017-07-28 22:18:06 +00:00
|
|
|
<button className="btn btn--clicky" disabled={isInstallingFromNpm}>
|
|
|
|
{isInstallingFromNpm && <i className="fa fa-refresh fa-spin space-right"/>}
|
2017-07-22 00:55:34 +00:00
|
|
|
Install Plugin
|
|
|
|
</button>
|
|
|
|
</div>
|
2017-07-20 01:55:40 +00:00
|
|
|
</div>
|
2017-07-22 00:55:34 +00:00
|
|
|
</form>
|
|
|
|
|
|
|
|
<hr/>
|
|
|
|
|
|
|
|
<div className="text-right">
|
2017-07-28 22:18:06 +00:00
|
|
|
<button type="button"
|
|
|
|
className="btn btn--clicky"
|
2017-09-17 14:04:46 +00:00
|
|
|
onClick={this._handleClickShowPluginsFolder}>
|
|
|
|
Show Plugins Folder
|
|
|
|
</button>
|
|
|
|
<button type="button"
|
|
|
|
disabled={isRefreshingPlugins}
|
|
|
|
className="btn btn--clicky space-left"
|
2017-08-10 01:05:14 +00:00
|
|
|
onClick={this._handleClickRefreshPlugins}>
|
2017-07-22 00:55:34 +00:00
|
|
|
Reload Plugin List
|
2017-09-17 14:04:46 +00:00
|
|
|
{isRefreshingPlugins && <i className="fa fa-refresh fa-spin space-left"/>}
|
2017-07-22 00:55:34 +00:00
|
|
|
</button>
|
2017-07-20 01:55:40 +00:00
|
|
|
</div>
|
2017-06-01 02:04:27 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Plugins;
|