2021-02-02 22:23:42 +00:00
|
|
|
import { autoBindMethodsForReact } from 'class-autobind-decorator';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { clipboard } from 'electron';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { Button, ButtonProps } from 'insomnia-components';
|
2021-07-22 23:04:56 +00:00
|
|
|
import React, { PureComponent, ReactNode } from 'react';
|
|
|
|
|
|
|
|
import { AUTOBIND_CFG } from '../../../common/constants';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
|
|
|
interface Props extends ButtonProps {
|
2021-09-01 14:50:26 +00:00
|
|
|
content: string | Function;
|
|
|
|
children?: ReactNode;
|
|
|
|
title?: string;
|
|
|
|
confirmMessage?: string;
|
2021-05-12 06:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
showConfirmation: boolean;
|
|
|
|
}
|
2016-07-22 20:02:17 +00:00
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2021-09-03 18:25:32 +00:00
|
|
|
export class CopyButton extends PureComponent<Props, State> {
|
2021-05-12 06:35:00 +00:00
|
|
|
state: State = {
|
|
|
|
showConfirmation: false,
|
2021-09-01 14:50:26 +00:00
|
|
|
};
|
2016-11-26 08:29:16 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
_triggerTimeout: NodeJS.Timeout | null = null;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async _handleClick(e) {
|
2016-07-22 20:02:17 +00:00
|
|
|
e.preventDefault();
|
2017-02-27 21:00:13 +00:00
|
|
|
e.stopPropagation();
|
2018-06-25 17:42:50 +00:00
|
|
|
const content =
|
2018-10-17 16:42:33 +00:00
|
|
|
typeof this.props.content === 'string' ? this.props.content : await this.props.content();
|
2017-06-29 18:27:29 +00:00
|
|
|
|
|
|
|
if (content) {
|
|
|
|
clipboard.writeText(content);
|
|
|
|
}
|
2016-07-22 20:02:17 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
this.setState({
|
|
|
|
showConfirmation: true,
|
|
|
|
});
|
2016-11-22 19:42:10 +00:00
|
|
|
this._triggerTimeout = setTimeout(() => {
|
2021-05-12 06:35:00 +00:00
|
|
|
this.setState({
|
|
|
|
showConfirmation: false,
|
|
|
|
});
|
2016-07-22 20:02:17 +00:00
|
|
|
}, 2000);
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-07-22 20:02:17 +00:00
|
|
|
|
2020-02-18 17:44:08 +00:00
|
|
|
componentWillUnmount() {
|
2021-05-12 06:35:00 +00:00
|
|
|
if (this._triggerTimeout === null) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
clearTimeout(this._triggerTimeout);
|
2016-07-22 20:02:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
2017-03-03 20:09:08 +00:00
|
|
|
const {
|
2021-05-12 06:35:00 +00:00
|
|
|
content,
|
2017-03-03 20:09:08 +00:00
|
|
|
children,
|
2017-06-01 02:04:27 +00:00
|
|
|
title,
|
2017-06-01 23:53:10 +00:00
|
|
|
confirmMessage,
|
2017-03-03 20:09:08 +00:00
|
|
|
...other
|
|
|
|
} = this.props;
|
2018-06-25 17:42:50 +00:00
|
|
|
const { showConfirmation } = this.state;
|
2018-10-17 16:42:33 +00:00
|
|
|
const confirm = typeof confirmMessage === 'string' ? confirmMessage : 'Copied';
|
2016-07-22 20:02:17 +00:00
|
|
|
return (
|
2020-05-02 18:56:39 +00:00
|
|
|
<Button {...other} title={title} onClick={this._handleClick}>
|
2018-06-25 17:42:50 +00:00
|
|
|
{showConfirmation ? (
|
|
|
|
<span>
|
|
|
|
{confirm} <i className="fa fa-check-circle-o" />
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
children || 'Copy to Clipboard'
|
|
|
|
)}
|
2020-05-02 18:56:39 +00:00
|
|
|
</Button>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-07-22 20:02:17 +00:00
|
|
|
}
|
|
|
|
}
|