2021-02-02 22:23:42 +00:00
|
|
|
import { autoBindMethodsForReact } from 'class-autobind-decorator';
|
2016-11-16 17:18:39 +00:00
|
|
|
import classnames from 'classnames';
|
2021-07-22 23:04:56 +00:00
|
|
|
import * as electron from 'electron';
|
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
|
|
|
|
import * as fetch from '../../account/fetch';
|
|
|
|
import * as session from '../../account/session';
|
2020-10-22 19:35:42 +00:00
|
|
|
import {
|
2021-02-02 22:23:42 +00:00
|
|
|
AUTOBIND_CFG,
|
2021-07-22 23:04:56 +00:00
|
|
|
getAppId,
|
2020-10-22 19:35:42 +00:00
|
|
|
getAppName,
|
|
|
|
getAppPlatform,
|
|
|
|
getAppVersion,
|
2021-02-28 14:54:18 +00:00
|
|
|
updatesSupported,
|
2020-10-22 19:35:42 +00:00
|
|
|
} from '../../common/constants';
|
2021-07-22 23:04:56 +00:00
|
|
|
import * as models from '../../models/index';
|
2020-09-30 21:05:00 +00:00
|
|
|
import imgSrcCore from '../images/insomnia-core-logo.png';
|
2021-07-22 23:04:56 +00:00
|
|
|
import Link from './base/link';
|
2016-11-16 17:18:39 +00:00
|
|
|
const LOCALSTORAGE_KEY = 'insomnia::notifications::seen';
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export interface ToastNotification {
|
|
|
|
key: string;
|
|
|
|
url: string;
|
|
|
|
cta: string;
|
|
|
|
message: string;
|
|
|
|
}
|
2017-11-22 21:10:34 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface State {
|
|
|
|
notification: ToastNotification | null;
|
|
|
|
visible: boolean;
|
|
|
|
appName: string;
|
|
|
|
}
|
2017-11-22 21:10:34 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
const StyledLogo = styled.div`
|
2020-09-30 21:05:00 +00:00
|
|
|
margin: var(--padding-xs) var(--padding-sm) var(--padding-xs) var(--padding-xs);
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
img {
|
|
|
|
max-width: 5rem;
|
|
|
|
}
|
|
|
|
`;
|
2021-05-12 06:35:00 +00:00
|
|
|
const StyledContent = styled.div`
|
2020-09-30 21:05:00 +00:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
flex-direction: column;
|
|
|
|
padding: 0 var(--padding-xs) 0 var(--padding-xs);
|
|
|
|
max-width: 20rem;
|
|
|
|
`;
|
2021-05-12 06:35:00 +00:00
|
|
|
const StyledFooter = styled.footer`
|
2020-09-30 21:05:00 +00:00
|
|
|
padding-top: var(--padding-sm);
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
justify-content: space-between;
|
|
|
|
width: 100%;
|
|
|
|
`;
|
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2021-05-12 06:35:00 +00:00
|
|
|
class Toast extends PureComponent<{}, State> {
|
|
|
|
_interval: NodeJS.Timeout | null = null;
|
2017-11-22 21:10:34 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
state: State = {
|
|
|
|
notification: null,
|
|
|
|
visible: false,
|
|
|
|
appName: getAppName(),
|
2021-09-01 14:50:26 +00:00
|
|
|
};
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_handlePostCTACleanup() {
|
|
|
|
const { notification } = this.state;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
if (!notification) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-28 07:12:17 +00:00
|
|
|
this._dismissNotification();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-23 22:55:09 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_handleCancelClick() {
|
|
|
|
const { notification } = this.state;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
if (!notification) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-28 07:12:17 +00:00
|
|
|
this._dismissNotification();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-23 22:55:09 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_hasSeenNotification(notification: ToastNotification) {
|
2017-07-31 20:46:04 +00:00
|
|
|
const seenNotifications = this._loadSeen();
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-07-31 20:46:04 +00:00
|
|
|
return seenNotifications[notification.key];
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async _checkForNotifications() {
|
2016-11-23 22:55:09 +00:00
|
|
|
// If there is a notification open, skip check
|
|
|
|
if (this.state.notification) {
|
2016-11-17 21:22:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:58:37 +00:00
|
|
|
const stats = await models.stats.get();
|
2017-11-21 17:49:33 +00:00
|
|
|
const settings = await models.settings.getOrCreate();
|
2017-11-22 21:10:34 +00:00
|
|
|
let notification: ToastNotification;
|
2017-01-11 20:58:37 +00:00
|
|
|
|
2017-07-31 20:46:04 +00:00
|
|
|
// Try fetching user notification
|
2017-07-31 21:14:16 +00:00
|
|
|
try {
|
|
|
|
const data = {
|
2021-05-12 06:35:00 +00:00
|
|
|
firstLaunch: stats.created,
|
|
|
|
// Used for account verification notifications
|
|
|
|
launches: stats.launches,
|
|
|
|
// Used for CTAs / Informational notifications
|
2020-10-22 19:35:42 +00:00
|
|
|
platform: getAppPlatform(),
|
|
|
|
app: getAppId(),
|
|
|
|
version: getAppVersion(),
|
2021-02-28 14:54:18 +00:00
|
|
|
updatesNotSupported: !updatesSupported(),
|
2017-11-21 17:49:33 +00:00
|
|
|
autoUpdatesDisabled: !settings.updateAutomatically,
|
2018-11-06 00:41:42 +00:00
|
|
|
disableUpdateNotification: settings.disableUpdateNotification,
|
2020-05-29 20:58:09 +00:00
|
|
|
updateChannel: settings.updateChannel,
|
2017-07-31 21:14:16 +00:00
|
|
|
};
|
2020-04-09 17:32:19 +00:00
|
|
|
notification = await fetch.post('/notification', data, session.getCurrentSessionId());
|
2017-07-31 21:14:16 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.warn('[toast] Failed to fetch user notifications', err);
|
2016-11-16 17:18:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2017-11-22 21:10:34 +00:00
|
|
|
this._handleNotification(notification);
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
_handleNotification(notification: ToastNotification | null | undefined) {
|
2017-07-31 20:46:04 +00:00
|
|
|
// No new notifications
|
|
|
|
if (!notification || this._hasSeenNotification(notification)) {
|
2016-11-23 22:55:09 +00:00
|
|
|
return;
|
2016-11-16 17:18:39 +00:00
|
|
|
}
|
2016-11-23 19:33:24 +00:00
|
|
|
|
2016-11-23 23:12:32 +00:00
|
|
|
// Remember that we've seen it
|
2017-07-31 20:46:04 +00:00
|
|
|
const seenNotifications = this._loadSeen();
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-11-23 23:12:32 +00:00
|
|
|
seenNotifications[notification.key] = true;
|
2017-03-03 20:09:08 +00:00
|
|
|
const obj = JSON.stringify(seenNotifications, null, 2);
|
|
|
|
window.localStorage.setItem(LOCALSTORAGE_KEY, obj);
|
2016-11-23 22:55:09 +00:00
|
|
|
// Show the notification
|
2021-05-12 06:35:00 +00:00
|
|
|
this.setState({
|
|
|
|
notification,
|
|
|
|
visible: false,
|
|
|
|
});
|
2016-11-23 22:55:09 +00:00
|
|
|
// Fade the notification in
|
2021-05-12 06:35:00 +00:00
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
this.setState({
|
|
|
|
visible: true,
|
|
|
|
}),
|
|
|
|
1000,
|
|
|
|
);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-28 07:12:17 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_loadSeen() {
|
2016-11-28 07:12:17 +00:00
|
|
|
try {
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2017-03-03 20:09:08 +00:00
|
|
|
return JSON.parse(window.localStorage.getItem(LOCALSTORAGE_KEY)) || {};
|
2016-11-28 07:12:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_dismissNotification() {
|
|
|
|
const { notification } = this.state;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-11-28 07:12:17 +00:00
|
|
|
if (!notification) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide the currently showing notification
|
2021-05-12 06:35:00 +00:00
|
|
|
this.setState({
|
|
|
|
visible: false,
|
|
|
|
});
|
2016-11-28 07:12:17 +00:00
|
|
|
// Give time for toast to fade out, then remove it
|
|
|
|
setTimeout(() => {
|
2021-05-12 06:35:00 +00:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
notification: null,
|
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
await this._checkForNotifications();
|
|
|
|
},
|
|
|
|
);
|
2016-11-28 07:12:17 +00:00
|
|
|
}, 1000);
|
2016-11-16 17:18:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
_listenerShowNotification(_e, notification: ToastNotification) {
|
2017-11-22 21:10:34 +00:00
|
|
|
console.log('[toast] Received notification ' + notification.key);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
this._handleNotification(notification);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentDidMount() {
|
2017-11-22 21:10:34 +00:00
|
|
|
setTimeout(this._checkForNotifications, 1000 * 10);
|
|
|
|
this._interval = setInterval(this._checkForNotifications, 1000 * 60 * 30);
|
2018-10-17 16:42:33 +00:00
|
|
|
electron.ipcRenderer.on('show-notification', this._listenerShowNotification);
|
2016-11-28 07:12:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 17:44:08 +00:00
|
|
|
componentWillUnmount() {
|
2021-05-12 06:35:00 +00:00
|
|
|
if (this._interval !== null) {
|
|
|
|
clearInterval(this._interval);
|
|
|
|
}
|
2018-10-17 16:42:33 +00:00
|
|
|
electron.ipcRenderer.removeListener('show-notification', this._listenerShowNotification);
|
2016-11-16 17:18:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
2020-09-30 21:05:00 +00:00
|
|
|
const { notification, visible, appName } = this.state;
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2016-11-23 19:33:24 +00:00
|
|
|
if (!notification) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
return (
|
2018-07-25 15:30:45 +00:00
|
|
|
<div
|
|
|
|
className={classnames('toast theme--dialog', {
|
2018-12-12 17:36:11 +00:00
|
|
|
'toast--show': visible,
|
2021-08-07 08:03:56 +00:00
|
|
|
})}
|
|
|
|
>
|
2020-09-30 21:05:00 +00:00
|
|
|
<StyledLogo>
|
2021-02-02 23:19:22 +00:00
|
|
|
<img src={imgSrcCore} alt={appName} />
|
2020-09-30 21:05:00 +00:00
|
|
|
</StyledLogo>
|
|
|
|
<StyledContent>
|
|
|
|
<p>{notification ? notification.message : 'Unknown'}</p>
|
|
|
|
<StyledFooter>
|
2018-06-25 17:42:50 +00:00
|
|
|
<button
|
|
|
|
className="btn btn--super-duper-compact btn--outlined"
|
2021-08-07 08:03:56 +00:00
|
|
|
onClick={this._handleCancelClick}
|
|
|
|
>
|
2017-03-28 22:45:23 +00:00
|
|
|
Dismiss
|
|
|
|
</button>
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
<Link
|
|
|
|
button
|
|
|
|
className="btn btn--super-duper-compact btn--outlined no-wrap"
|
|
|
|
onClick={this._handlePostCTACleanup}
|
2021-08-07 08:03:56 +00:00
|
|
|
href={notification.url}
|
|
|
|
>
|
2017-03-28 22:45:23 +00:00
|
|
|
{notification.cta}
|
|
|
|
</Link>
|
2020-09-30 21:05:00 +00:00
|
|
|
</StyledFooter>
|
|
|
|
</StyledContent>
|
2016-11-16 17:18:39 +00:00
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-16 17:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Toast;
|