Files
flipper/src/reducers/notifications.tsx
Daniel Büchele 5f53087c7e client server
Summary: Migrating Server, Client and UninitializedClient to TypeScript

Reviewed By: passy

Differential Revision: D16687855

fbshipit-source-id: 402e4dbcd5d283d3e280d4d8b312662829457886
2019-08-08 11:00:38 -07:00

170 lines
4.0 KiB
TypeScript

/**
* 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 {Notification} from '../plugin';
export type PluginNotification = {
notification: Notification;
pluginId: string;
client: null | string;
};
export type State = {
activeNotifications: Array<PluginNotification>;
invalidatedNotifications: Array<PluginNotification>;
blacklistedPlugins: Array<string>;
blacklistedCategories: Array<string>;
clearedNotifications: Set<string>;
};
type ActiveNotificationsAction = {
type: 'SET_ACTIVE_NOTIFICATIONS';
payload: {
notifications: Array<Notification>;
client: null | string;
pluginId: string;
};
};
export type Action =
| {
type: 'CLEAR_ALL_NOTIFICATIONS';
}
| {
type: 'SET_ACTIVE_NOTIFICATIONS';
payload: {
notifications: Array<Notification>;
client: null | string;
pluginId: string;
};
}
| {
type: 'UPDATE_PLUGIN_BLACKLIST';
payload: Array<string>;
}
| {
type: 'UPDATE_CATEGORY_BLACKLIST';
payload: Array<string>;
};
const INITIAL_STATE: State = {
activeNotifications: [],
invalidatedNotifications: [],
blacklistedPlugins: [],
blacklistedCategories: [],
clearedNotifications: new Set(),
};
export default function reducer(
state: State = INITIAL_STATE,
action: Action,
): State {
switch (action.type) {
case 'SET_ACTIVE_NOTIFICATIONS': {
return activeNotificationsReducer(state, action);
}
case 'CLEAR_ALL_NOTIFICATIONS':
const markAsCleared = ({
pluginId,
notification: {id},
}: PluginNotification) =>
state.clearedNotifications.add(`${pluginId}#${id}`);
state.activeNotifications.forEach(markAsCleared);
state.invalidatedNotifications.forEach(markAsCleared);
// Q: Should this actually delete them, or just invalidate them?
return {
...state,
activeNotifications: [],
invalidatedNotifications: [],
};
case 'UPDATE_PLUGIN_BLACKLIST':
return {
...state,
blacklistedPlugins: action.payload,
};
case 'UPDATE_CATEGORY_BLACKLIST':
return {
...state,
blacklistedCategories: action.payload,
};
default:
return state;
}
}
function activeNotificationsReducer(
state: State,
action: ActiveNotificationsAction,
): State {
const {payload} = action;
const newActiveNotifications = [];
const newInactivatedNotifications = state.invalidatedNotifications;
const newIDs = new Set(payload.notifications.map((n: Notification) => n.id));
for (const activeNotification of state.activeNotifications) {
if (activeNotification.pluginId !== payload.pluginId) {
newActiveNotifications.push(activeNotification);
continue;
}
if (!newIDs.has(activeNotification.notification.id)) {
newInactivatedNotifications.push(activeNotification);
}
}
payload.notifications
.filter(
({id}: Notification) =>
!state.clearedNotifications.has(`${payload.pluginId}#${id}`),
)
.forEach((notification: Notification) => {
newActiveNotifications.push({
pluginId: payload.pluginId,
client: payload.client,
notification,
});
});
return {
...state,
activeNotifications: newActiveNotifications,
invalidatedNotifications: newInactivatedNotifications,
};
}
export function setActiveNotifications(payload: {
notifications: Array<Notification>;
client: null | string;
pluginId: string;
}): Action {
return {
type: 'SET_ACTIVE_NOTIFICATIONS',
payload,
};
}
export function clearAllNotifications(): Action {
return {
type: 'CLEAR_ALL_NOTIFICATIONS',
};
}
export function updatePluginBlacklist(payload: Array<string>): Action {
return {
type: 'UPDATE_PLUGIN_BLACKLIST',
payload,
};
}
export function updateCategoryBlacklist(payload: Array<string>): Action {
return {
type: 'UPDATE_CATEGORY_BLACKLIST',
payload,
};
}