Files
flipper/src/dispatcher/notifications.js
Daniel Büchele c8a7ce5cfb link to hub
Summary: Clicking on a notifications links to the Notification Hub highlighting the selected notification.

Reviewed By: jknoxville

Differential Revision: D10487822

fbshipit-source-id: ed907ec244bef970d1b30ddb719856949229d0c4
2018-10-22 06:59:52 -07:00

78 lines
2.5 KiB
JavaScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import type {Store} from '../reducers/index.js';
import type Logger from '../fb-stubs/Logger.js';
import type {PluginNotification} from '../reducers/notifications';
import type {FlipperPlugin} from '../plugin.js';
import {selectPlugin} from '../reducers/connections';
import {setActiveNotifications} from '../reducers/notifications';
import {textContent} from '../utils/index';
import {clientPlugins} from '../plugins/index.js';
import GK from '../fb-stubs/GK';
export default (store: Store, logger: Logger) => {
if (GK.get('flipper_disable_notifications')) {
return;
}
const knownNotifications: Set<string> = new Set();
const knownPluginStates: Map<string, Object> = new Map();
store.subscribe(() => {
const {notifications, pluginStates} = store.getState();
Object.keys(pluginStates).forEach(key => {
if (knownPluginStates.get(key) !== pluginStates[key]) {
knownPluginStates.set(key, pluginStates[key]);
const [client, pluginId] = key.split('#');
const persistingPlugin: ?Class<FlipperPlugin<>> = clientPlugins.find(
(p: Class<FlipperPlugin<>>) =>
p.id === pluginId && p.getActiveNotifications,
);
if (persistingPlugin) {
store.dispatch(
setActiveNotifications({
// $FlowFixMe: Ensured getActiveNotifications is implemented in filter
notifications: persistingPlugin.getActiveNotifications(
pluginStates[key],
),
client,
pluginId,
}),
);
}
}
});
const {activeNotifications, blacklistedPlugins} = notifications;
activeNotifications.forEach((n: PluginNotification) => {
if (
!knownNotifications.has(n.notification.id) &&
blacklistedPlugins.indexOf(n.pluginId) === -1
) {
const notification = new window.Notification(n.notification.title, {
body: textContent(n.notification.message),
});
logger.track('usage', 'native-notification', n.notification);
notification.onclick = () =>
store.dispatch(
selectPlugin({
selectedPlugin: 'notifications',
selectedApp: null,
deepLinkPayload: n.notification.id,
}),
);
knownNotifications.add(n.notification.id);
}
});
});
};