forked from conwnet/github1s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.ts
56 lines (47 loc) · 2.18 KB
/
notification.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @file render notification
* @author netcon
*/
import './notification.css';
const NOTIFICATION_STORAGE_KEY = 'GITHUB1S_NOTIFICATION';
// Change this if a new notification should be shown
const NOTIFICATION_STORAGE_VALUE = '20210212';
/*** begin notification block ***/
export const renderNotification = (platform: string) => {
// If user has confirmed the notification and checked `don't show me again`, ignore it
if (window.localStorage.getItem(NOTIFICATION_STORAGE_KEY) === NOTIFICATION_STORAGE_VALUE) {
return;
}
const notifications = [
{
title: 'ATTENTION: This page is NOT officially provided by ' + platform + '.',
content: platform + '1s is an open source project, which is not officially provided by ' + platform + '.',
link: 'https://github.com/conwnet/github1s',
},
];
const notificationBlocksHtml = notifications.map((item) => {
const linkHtml = item.link
? ' <a class="notification-link" href="' + item.link + '" target="_blank">See more</a>'
: '';
const titleHtml = '<div class="notification-main"><div class="notification-title">' + item.title + '</div>';
const contentHtml = '<div class="notification-content">' + item.content + linkHtml + '</div></div>';
return titleHtml + contentHtml;
});
const notificationHtml =
notificationBlocksHtml +
'<div class="notification-footer"><button class="notification-confirm-button">OK</button>' +
'<div class="notification-show-me-again"><input type="checkbox" checked>Don\'t show me again</div></div></div>';
const notificationElement = document.createElement('div');
notificationElement.classList.add('github1s-notification');
notificationElement.innerHTML = notificationHtml;
document.body.appendChild(notificationElement);
const confirmButton = notificationElement.querySelector('.notification-confirm-button') as HTMLButtonElement;
confirmButton.onclick = () => {
const showAgainCheckBox = notificationElement?.querySelector('.notification-show-me-again input');
const notShowMeAgain = !!(showAgainCheckBox as HTMLInputElement)?.checked;
if (notShowMeAgain) {
window.localStorage.setItem(NOTIFICATION_STORAGE_KEY, NOTIFICATION_STORAGE_VALUE);
}
document.body.removeChild(notificationElement);
};
};