File size: 4,607 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Card } from '@automattic/components';
import { ExternalLink, ToggleControl } from '@wordpress/components';
import { getLocaleSlug, localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import Notice from 'calypso/components/notice';
import userAgent from 'calypso/lib/user-agent';
import { setEnabledState } from 'calypso/state/push-notifications/actions';
import { getStatus, isApiReady, isEnabled } from 'calypso/state/push-notifications/selectors';

import './style.scss';

function getPlatform( os ) {
	if ( os === 'iOS' ) {
		return 'iOS';
	} else if ( os === 'Android' ) {
		return 'Android';
	}
	return 'Desktop';
}

class PushNotificationSettings extends Component {
	static propTypes = {
		setEnabledState: PropTypes.func.isRequired,
	};

	clickHandler = ( value ) => {
		this.props.setEnabledState( value );
	};

	getBlockedInstructionURL = () => {
		const { browser, platform, isMobile, isAndroid, isIOS } = userAgent;
		const locale = getLocaleSlug();
		const mappedPlatform = getPlatform( platform );

		switch ( browser ) {
			case 'Chrome':
				// Google supports all our locales: https://serpapi.com/google-languages
				return `https://support.google.com/chrome/answer/3220216?hl=${ locale }&co=GENIE.Platform%3D${ mappedPlatform }&oco=1`;
			case 'Safari': {
				if ( isIOS ) {
					// Apple doesn't have docs for this.
					return null;
				}
				return 'https://support.apple.com/guide/safari/customize-website-notifications-sfri40734/mac';
			}
			case 'Edge':
				if ( isMobile ) {
					// MS doesn't have docs for this.
					return null;
				}
				// MS supports all our locales: https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/help/language-locale
				return `https://support.microsoft.com/${ locale }/microsoft-edge/manage-website-notifications-in-microsoft-edge-0c555609-5bf2-479d-a59d-fb30a0b80b2b`;
			case 'Firefox': {
				if ( isAndroid ) {
					return `https://support.mozilla.org/${ locale }/kb/manage-notifications-firefox-android`;
				} else if ( isIOS ) {
					return `https://support.mozilla.org/${ locale }/kb/turn-push-notifications-or-firefox-ios`;
				}
				return 'https://support.mozilla.org/en-US/kb/push-notifications-firefox';
			}
			default: {
				return null;
			}
		}
	};

	render() {
		let buttonDisabled;
		let isActive;
		let deniedText;

		if ( ! this.props.apiReady ) {
			return null;
		}

		const blockedInstructionUrl = this.getBlockedInstructionURL();

		switch ( this.props.status ) {
			case 'disabling':
				buttonDisabled = true;
				isActive = false;
				break;
			case 'enabling':
				buttonDisabled = true;
				isActive = true;
				break;
			case 'unsubscribed':
				buttonDisabled = false;
				isActive = false;
				break;
			case 'subscribed':
				buttonDisabled = false;
				isActive = true;
				break;
			case 'denied':
				buttonDisabled = true;
				isActive = false;

				deniedText = (
					<Notice
						className="notification-settings-push-notification-settings__instruction"
						showDismiss={ false }
						status="is-info"
						text={
							<div className="notification-settings-push-notification-settings__instruction-actions">
								<div>
									{ this.props.translate(
										'Your browser is currently set to block notifications from WordPress.com.'
									) }
								</div>
								<div>
									{ blockedInstructionUrl &&
										this.props.translate(
											'{{instructionsButton}}View instructions to enable{{/instructionsButton}}',
											{
												components: {
													instructionsButton: (
														<ExternalLink href={ blockedInstructionUrl } target="_blank" />
													),
												},
											}
										) }
								</div>
							</div>
						}
					/>
				);
				break;

			default:
				return null;
		}

		return (
			<>
				{ deniedText }
				<Card className="notification-settings-push-notification-settings__settings">
					<ToggleControl
						disabled={ buttonDisabled }
						checked={ isActive }
						help={ this.props.translate(
							'Get instant notifications for new comments and likes, even when you are not actively using WordPress.com'
						) }
						label={ this.props.translate( 'Browser notifications' ) }
						onChange={ this.clickHandler }
					/>
				</Card>
			</>
		);
	}
}

export default connect(
	( state ) => {
		return {
			apiReady: isApiReady( state ),
			isEnabled: isEnabled( state ),
			status: getStatus( state ),
		};
	},
	{
		setEnabledState: setEnabledState,
	}
)( localize( PushNotificationSettings ) );