File size: 10,459 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
/**
* Loads the notifications client into Calypso and
* connects the messaging and interactive elements
*
* - messages through iframe
* - keyboard hotkeys
* - window/pane scrolling
* - service worker
* @module notifications
*/
import config from '@automattic/calypso-config';
import page from '@automattic/calypso-router';
import { localizeUrl } from '@automattic/i18n-utils';
import NotificationsPanel, {
refreshNotes,
} from '@automattic/notifications/src/panel/Notifications';
import clsx from 'clsx';
import debugFactory from 'debug';
import { useTranslate } from 'i18n-calypso';
import { Component } from 'react';
import { connect } from 'react-redux';
import localStorageHelper from 'store';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import wpcom from 'calypso/lib/wp';
import { requestAdminMenu as requestAdminMenuAction } from 'calypso/state/admin-menu/actions';
import { recordTracksEvent as recordTracksEventAction } from 'calypso/state/analytics/actions';
import { setUnseenCount } from 'calypso/state/notifications/actions';
import { didForceRefresh } from 'calypso/state/notifications-panel/actions';
import { shouldForceRefresh } from 'calypso/state/notifications-panel/selectors';
import getCurrentLocaleSlug from 'calypso/state/selectors/get-current-locale-slug';
import getCurrentLocaleVariant from 'calypso/state/selectors/get-current-locale-variant';
import getCurrentRoute from 'calypso/state/selectors/get-current-route';
import getSelectedSiteId from 'calypso/state/ui/selectors/get-selected-site-id';
import './style.scss';
/**
* Returns whether or not the browser session
* is currently visible to the user
* @returns {boolean} is the browser session visible
*/
const getIsVisible = () => {
if ( ! document ) {
return true;
}
if ( ! document.visibilityState ) {
return true;
}
return document.visibilityState === 'visible';
};
const isDesktop = config.isEnabled( 'desktop' );
const debug = debugFactory( 'notifications:panel' );
const Notifications3PCNotice = ( { className } ) => {
const translate = useTranslate();
return (
<div className={ `reader-notifications__3pc-notice ${ className }` }>
<p>
{ translate(
"Didn't expect to see this page? {{learnMoreLink}}Learn about 3rd party cookies.{{/learnMoreLink}}",
{
components: {
learnMoreLink: (
<a href={ localizeUrl( 'https://wordpress.com/support/third-party-cookies/' ) } />
),
},
}
) }
</p>
</div>
);
};
export class Notifications extends Component {
state = {
// Desktop: override isVisible to maintain active polling for native UI elements (e.g. notification badge)
isVisible: isDesktop ? true : getIsVisible(),
};
focusedElementBeforeOpen = null;
actionHandlers = {
APP_RENDER_NOTES: [
( store, { newNoteCount } ) => {
localStorageHelper.set( 'wpnotes_unseen_count', newNoteCount );
this.props.setUnseenCount( newNoteCount );
},
],
OPEN_LINK: [
( store, { href, tracksEvent } ) => {
if ( tracksEvent ) {
this.props.recordTracksEventAction( 'calypso_notifications_' + tracksEvent, {
link: href,
} );
}
window.open( href, '_blank' );
},
],
OPEN_POST: [
( store, { siteId, postId } ) => {
this.props.checkToggle();
this.props.recordTracksEventAction( 'calypso_notifications_open_post', {
site_id: siteId,
post_id: postId,
} );
page( `/reader/blogs/${ siteId }/posts/${ postId }` );
},
],
OPEN_COMMENT: [
( store, { siteId, postId, commentId } ) => {
this.props.checkToggle();
this.props.recordTracksEventAction( 'calypso_notifications_open_comment', {
site_id: siteId,
post_id: postId,
comment_id: commentId,
} );
page( `/reader/blogs/${ siteId }/posts/${ postId }#comment-${ commentId }` );
},
],
OPEN_SITE: [
( store, { siteId } ) => {
this.props.checkToggle();
this.props.recordTracksEventAction( 'calypso_notifications_open_site', {
site_id: siteId,
} );
page( `/reader/blogs/${ siteId }` );
},
],
VIEW_SETTINGS: [
() => {
this.props.checkToggle();
page( '/me/notifications' );
},
],
EDIT_COMMENT: [
( store, { siteId, postId, commentId } ) => {
this.props.checkToggle();
this.props.recordTracksEventAction( 'calypso_notifications_edit_comment', {
site_id: siteId,
post_id: postId,
comment_id: commentId,
} );
page( `/comment/${ siteId }/${ commentId }?action=edit` );
},
],
ANSWER_PROMPT: [
( store, { siteId, href } ) => {
this.props.checkToggle();
this.props.recordTracksEventAction( 'calypso_notifications_answer_prompt', {
site_id: siteId,
} );
window.open( href, '_blank' );
},
],
CLOSE_PANEL: [
() => {
this.props.checkToggle();
},
],
APPROVE_NOTE: [
() => {
this.props.requestAdminMenu( this.props.selectedSiteId );
},
],
NOTES_REMOVE: [
( _, { isComment } ) => {
if ( isComment ) {
this.props.requestAdminMenu( this.props.selectedSiteId );
}
},
],
};
componentDidMount() {
document.addEventListener( 'click', this.props.checkToggle );
document.addEventListener( 'keydown', this.handleKeyPress );
if ( this.props.isShowing ) {
this.focusedElementBeforeOpen = document.activeElement;
}
if ( typeof document.hidden !== 'undefined' ) {
document.addEventListener( 'visibilitychange', this.handleVisibilityChange );
}
if (
'serviceWorker' in window.navigator &&
'addEventListener' in window.navigator.serviceWorker
) {
window.navigator.serviceWorker.addEventListener(
'message',
this.receiveServiceWorkerMessage
);
this.postServiceWorkerMessage( { action: 'sendQueuedMessages' } );
}
}
componentDidUpdate( prevProps ) {
if ( prevProps.isShowing === this.props.isShowing ) {
return;
}
if ( ! prevProps.isShowing && this.props.isShowing ) {
this.focusedElementBeforeOpen = document.activeElement;
} else {
this.focusedElementBeforeOpen?.focus();
}
}
componentWillUnmount() {
document.removeEventListener( 'click', this.props.checkToggle );
document.removeEventListener( 'keydown', this.handleKeyPress );
if ( typeof document.hidden !== 'undefined' ) {
document.removeEventListener( 'visibilitychange', this.handleVisibilityChange );
}
if (
'serviceWorker' in window.navigator &&
'removeEventListener' in window.navigator.serviceWorker
) {
window.navigator.serviceWorker.removeEventListener(
'message',
this.receiveServiceWorkerMessage
);
}
window.removeEventListener( 'message', this.handleDesktopNotificationMarkAsRead );
}
handleKeyPress = ( event ) => {
if ( event.target !== document.body && event.target.tagName !== 'A' ) {
return;
}
if ( event.altKey || event.ctrlKey || event.metaKey ) {
return;
}
// 'n' key should toggle the notifications frame
if ( 78 === event.keyCode ) {
event.stopPropagation();
event.preventDefault();
this.props.checkToggle( null, true );
}
if ( 27 === event.keyCode && this.props.isShowing ) {
event.stopPropagation();
event.preventDefault();
this.props.checkToggle( null, true );
}
};
// Desktop: override isVisible to maintain active polling for native UI elements (e.g. notification badge)
handleVisibilityChange = () => this.setState( { isVisible: isDesktop ? true : getIsVisible() } );
receiveServiceWorkerMessage = ( event ) => {
// Receives messages from the service worker
// Older Firefox versions (pre v48) set event.origin to "" for service worker messages
// Firefox does not support document.origin; we can use location.origin instead
if ( event.origin && event.origin !== window.location.origin ) {
return;
}
if ( ! ( 'action' in event.data ) ) {
return;
}
switch ( event.data.action ) {
case 'openPanel':
// Ensure panel is opened.
this.props.checkToggle( null, true, true );
return refreshNotes();
case 'trackClick':
recordTracksEvent( 'calypso_web_push_notification_clicked', {
push_notification_note_id: event.data.notification.note_id,
push_notification_type: event.data.notification.type,
} );
return;
}
};
postServiceWorkerMessage = ( message ) => {
if ( ! ( 'serviceWorker' in window.navigator ) ) {
return;
}
window.navigator.serviceWorker.ready.then(
( registration ) => 'active' in registration && registration.active.postMessage( message )
);
};
render() {
const localeSlug = this.props.currentLocaleSlug || config( 'i18n_default_locale_slug' );
const isDedicatedReaderPage = this.props.currentRoute?.startsWith( '/reader/notifications' );
if ( this.props.forceRefresh ) {
debug( 'Refreshing notes panel...' );
refreshNotes();
this.props.didForceRefresh();
}
return (
<>
{ /*
Due to how the notifs panel width is set differently on this page to control fly-out vs. in place
nested levels, this notice makes sense at different places in the DOM depending on the
breakpoint. We remove display for one or the other via CSS depending on the breakpoint.
*/ }
{ isDedicatedReaderPage && (
<Notifications3PCNotice className="reader-notifications__3pc-notice-external" />
) }
<div
id="wpnc-panel"
className={ clsx( 'wide', 'wpnc__main', {
'wpnt-open': this.props.isShowing,
'wpnt-closed': ! this.props.isShowing,
} ) }
>
{ isDedicatedReaderPage && (
<Notifications3PCNotice className="reader-notifications__3pc-notice-internal" />
) }
<NotificationsPanel
actionHandlers={ this.actionHandlers }
isShowing={ this.props.isShowing }
isVisible={ this.state.isVisible }
locale={ localeSlug }
wpcom={ wpcom }
/>
</div>
</>
);
}
}
export default connect(
( state ) => ( {
currentLocaleSlug: getCurrentLocaleVariant( state ) || getCurrentLocaleSlug( state ),
forceRefresh: shouldForceRefresh( state ),
selectedSiteId: getSelectedSiteId( state ),
currentRoute: getCurrentRoute( state ),
} ),
( dispatch ) => ( {
recordTracksEventAction: ( name, properties ) =>
dispatch( recordTracksEventAction( name, properties ) ),
setUnseenCount: ( count ) => dispatch( setUnseenCount( count ) ),
didForceRefresh: () => dispatch( didForceRefresh() ),
requestAdminMenu: ( siteId ) => dispatch( requestAdminMenuAction( siteId ) ),
} )
)( Notifications );
|