This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
/
Notifications.tsx
137 lines (118 loc) 路 5.28 KB
/
Notifications.tsx
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {Switch} from '@components/Switch';
import {Toast} from '@overlays/Toast';
import {pushNotification, settings} from '@state/index';
import {UploadExtensions} from '@state/models/UploadExtensions';
import {cn} from '@utils/preact-utils';
import {observer} from 'mobx-react';
import {FunctionalComponent, h} from 'preact';
import baseStyles from './_base.module.scss';
import styles from './Notifications.module.scss';
import {UploadStateNotification} from './UploadStateNotification';
export const Notifications: FunctionalComponent = observer(() => {
const {notifications} = settings;
const toggle = () => {
const {turnedOn} = notifications;
if (turnedOn === true) {
notifications.turnedOn = false;
} else if (turnedOn === false) {
switch (Notification.permission) {
case 'default': {
notifications.turnedOn = 'intermediate';
/**
* Request permissions, safari again is really slow in
* catching up with other browser so we have to provide an callback.
*/
const resolve = (status: string) => notifications.turnedOn = status === 'granted';
const request = Notification.requestPermission(resolve);
if (request instanceof Promise) {
void request.then(resolve);
}
break;
}
case 'denied': {
notifications.turnedOn = false;
Toast.instance.show({
text: 'Notifications are disabled by your browser!',
body: 'Check the site settings of your browser to enable notifications for this site.',
type: 'warning'
});
break;
}
case 'granted': {
notifications.turnedOn = true;
break;
}
}
}
};
const testNotifications = () => {
const success = pushNotification({
title: 'Hello World!',
body: 'Now go and share a file :)'
});
if (!success) {
if (notifications.hideIfAppIsVisible) {
Toast.instance.show({
text: 'Notifications are hidden if app is visible!',
body: 'Disable the option to view a notification if the app is open.',
type: 'warning'
});
} else {
Toast.instance.show({
text: 'Failed to show Notification',
body: 'Try closing other beam.cafe tabs!',
type: 'error'
});
}
}
};
return (
<div className={cn(baseStyles.section, styles.notifications, {
[styles.enabled]: notifications.turnedOn === true
})}>
<section className={baseStyles.standalone}>
<header>
<bc-icon name="notification"/>
<h3>Turn on Notifications</h3>
<Switch state={notifications.turnedOn}
onChange={toggle}
aria-describedby="Turn on notifications"/>
</header>
</section>
<section className={cn(styles.optionsHeader, baseStyles.borderless)}>
<h3>Customize Notifications</h3>
<button onClick={testNotifications}
aria-label="Show test Notification">
<bc-tooltip content="Show test Notification"/>
<bc-icon name="notification-color"/>
</button>
</section>
<section className={cn(styles.options, baseStyles.borderless)}>
<div>
<h3>Hide notifications if app is visible</h3>
<Switch state={notifications.hideIfAppIsVisible}
onChange={v => notifications.hideIfAppIsVisible = v}
aria-label="Hide notifications if app is visible"/>
</div>
<div>
<h3>Connection lost / re-established</h3>
<Switch state={notifications.onConnectionChange}
onChange={v => notifications.onConnectionChange = v}
aria-label="Connection lost / re-established"/>
</div>
<div>
<h3>Update available</h3>
<Switch state={notifications.onUpdateAvailable}
onChange={v => notifications.onUpdateAvailable = v}
aria-label="Update available"/>
</div>
</section>
<section className={cn(styles.optionsHeader, baseStyles.borderless)} aria-rule="banner">
<h3>Notify me when a upload...</h3>
</section>
<section className={cn(styles.options, baseStyles.borderless)}>
<UploadStateNotification/>
</section>
</div>
);
});