2021-05-12 06:35:00 +00:00
|
|
|
import React, { PureComponent, ReactNode } from 'react';
|
2021-02-02 22:23:42 +00:00
|
|
|
import { autoBindMethodsForReact } from 'class-autobind-decorator';
|
|
|
|
import { AUTOBIND_CFG } from '../../common/constants';
|
2019-11-26 17:22:21 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
const DISMISSED_VALUE = 'dismissed';
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface Props {
|
|
|
|
dismissKey?: string;
|
|
|
|
color?: 'surprise' | 'success' | 'warning' | 'info' | 'error' | 'subtle';
|
|
|
|
children: ReactNode;
|
|
|
|
className?: string;
|
|
|
|
}
|
2019-11-26 17:22:21 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface State {
|
|
|
|
visible: boolean;
|
|
|
|
localStorageKey: string | null;
|
|
|
|
}
|
2019-11-26 17:22:21 +00:00
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2021-05-12 06:35:00 +00:00
|
|
|
class Notice extends PureComponent<Props, State> {
|
2019-11-26 17:22:21 +00:00
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
const { dismissKey } = props;
|
|
|
|
const localStorageKey = dismissKey ? 'insomnia::notice::' + dismissKey : null;
|
|
|
|
// It's visible if it's not dismissible
|
|
|
|
let visible = false;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-11-26 17:22:21 +00:00
|
|
|
if (localStorageKey) {
|
|
|
|
visible = window.localStorage.getItem(localStorageKey) !== DISMISSED_VALUE;
|
|
|
|
} else {
|
|
|
|
visible = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
visible,
|
|
|
|
localStorageKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_dismissNotification() {
|
|
|
|
const { localStorageKey } = this.state;
|
|
|
|
// Hide the currently showing notification
|
|
|
|
this.setState({ visible: false });
|
2021-05-12 06:35:00 +00:00
|
|
|
if (localStorageKey !== null) {
|
|
|
|
window.localStorage.setItem(localStorageKey, DISMISSED_VALUE);
|
|
|
|
}
|
2019-11-26 17:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { children, color, className } = this.props;
|
|
|
|
const { visible, localStorageKey } = this.state;
|
|
|
|
|
|
|
|
if (!visible) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<p className={classnames('notice', color, className)}>
|
|
|
|
{localStorageKey && (
|
|
|
|
<button className="icon pull-right" onClick={this._dismissNotification}>
|
|
|
|
<i className="fa fa-times" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{children}
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Notice;
|