lbry-desktop/ui/redux/selectors/notifications.js
infinite-persistence 27f346d8f1
Don't memoize selectors without transformation
It was not meant to be used for these cases -- wasting resources creating and going through the cache for each simple direct access.
2021-10-24 13:05:06 +08:00

53 lines
1.6 KiB
JavaScript

import { createSelector } from 'reselect';
export const selectState = (state) => state.notifications || {};
export const selectNotifications = (state) => selectState(state).notifications;
export const selectNotificationsFiltered = (state) => selectState(state).notificationsFiltered;
export const selectNotificationCategories = (state) => selectState(state).notificationCategories;
export const makeSelectNotificationForCommentId = (id) =>
createSelector(selectNotifications, (notifications) => {
const match =
notifications &&
notifications.find(
(n) =>
n.notification_parameters &&
n.notification_parameters.dynamic &&
n.notification_parameters.dynamic.hash === id
);
return match;
});
export const selectIsFetchingNotifications = (state) => selectState(state).fetchingNotifications;
export const selectUnreadNotificationCount = createSelector(selectNotifications, (notifications) => {
return notifications ? notifications.filter((notification) => !notification.is_read).length : 0;
});
export const selectUnseenNotificationCount = createSelector(selectNotifications, (notifications) => {
return notifications ? notifications.filter((notification) => !notification.is_seen).length : 0;
});
export const selectToast = createSelector(selectState, (state) => {
if (state.toasts.length) {
const { id, params } = state.toasts[0];
return {
id,
...params,
};
}
return null;
});
export const selectError = createSelector(selectState, (state) => {
if (state.errors.length) {
const { error } = state.errors[0];
return {
error,
};
}
return null;
});