Mailspring/internal_packages/notifications/lib/notif-wrapper.jsx
Halla Moore 9e3c3c14cd feat(sidebar-notifs) Create sidebar notifications to replace old bars
Summary:
Move the old bar notifications to the sidebar, and only display one notification
at a time using a priority-rating system. Remove all of the old notification
infrastructure.

Test Plan: Added specs, also reproduced notifications locally

Reviewers: bengotow

Reviewed By: bengotow

Subscribers: juan

Differential Revision: https://phab.nylas.com/D3310
2016-10-04 08:08:23 -07:00

50 lines
1.3 KiB
JavaScript

import _ from 'underscore';
import {React, ReactDOM} from 'nylas-exports'
import {InjectedComponentSet} from 'nylas-component-kit'
const ROLE = "RootSidebar:Notifications";
export default class NotifWrapper extends React.Component {
static displayName = 'NotifWrapper';
componentDidMount() {
this.observer = new MutationObserver(this.update);
this.observer.observe(ReactDOM.findDOMNode(this), {childList: true})
this.update() // Necessary if notifications are already mounted
}
componentWillUnmount() {
this.observer.disconnect();
}
update = () => {
const className = "highest-priority";
const node = ReactDOM.findDOMNode(this);
const oldHighestPriorityElems = node.querySelectorAll(`.${className}`);
for (const oldElem of oldHighestPriorityElems) {
oldElem.classList.remove(className)
}
const elemsWithPriority = node.querySelectorAll("[data-priority]")
if (elemsWithPriority.length === 0) {
return;
}
const highestPriorityElem = _.max(elemsWithPriority,
(elem) => parseInt(elem.dataset.priority, 10))
highestPriorityElem.classList.add(className);
}
render() {
return (
<InjectedComponentSet
className="notifications"
matching={{role: ROLE}}
direction="column"
/>
)
}
}