hyper/app/notifications.ts
2023-06-26 16:02:13 +05:30

38 lines
1.1 KiB
TypeScript

import ms from 'ms';
import fetch from 'electron-fetch';
import {version} from './package.json';
import type {BrowserWindow} from 'electron';
const NEWS_URL = 'https://hyper-news.now.sh';
export default function fetchNotifications(win: BrowserWindow) {
const {rpc} = win;
const retry = (err?: Error) => {
setTimeout(() => fetchNotifications(win), ms('30m'));
if (err) {
console.error('Notification messages fetch error', err.stack);
}
};
console.log('Checking for notification messages');
fetch(NEWS_URL, {
headers: {
'X-Hyper-Version': version,
'X-Hyper-Platform': process.platform
}
})
.then((res) => res.json())
.then((data) => {
const message: {text: string; url: string; dismissable: boolean} | '' = data.message || '';
if (typeof message !== 'object' && message !== '') {
throw new Error('Bad response');
}
if (message === '') {
console.log('No matching notification messages');
} else {
rpc.emit('add notification', message);
}
retry();
})
.catch(retry);
}