Mailspring/app/internal_packages/notifications/lib/notif-wrapper.jsx

52 lines
1.3 KiB
React
Raw Normal View History

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