diff --git a/packages/pl-fe/src/actions/notifications.ts b/packages/pl-fe/src/actions/notifications.ts index ceb425fd2..ce23dcd5e 100644 --- a/packages/pl-fe/src/actions/notifications.ts +++ b/packages/pl-fe/src/actions/notifications.ts @@ -208,12 +208,6 @@ const expandNotifications = ({ maxId }: Record = {}, done: () => an } } - if (maxId?.includes('+')) { - const ids = maxId.split('+'); - - maxId = ids[ids.length - 1]; - } - const params: Record = { max_id: maxId, }; @@ -263,7 +257,7 @@ const expandNotifications = ({ maxId }: Record = {}, done: () => an dispatch(importFetchedAccounts(Object.values(entries.accounts))); dispatch(importFetchedStatuses(Object.values(entries.statuses))); - const deduplicatedNotifications = normalizeNotifications(response.items); + const deduplicatedNotifications = normalizeNotifications(response.items, state.notifications.items); dispatch(expandNotificationsSuccess(deduplicatedNotifications, response.next)); fetchRelatedRelationships(dispatch, response.items); @@ -314,13 +308,9 @@ const markReadNotifications = () => if (!isLoggedIn(getState)) return; const state = getState(); - let topNotificationId = state.notifications.items.first()?.id; + const topNotificationId = state.notifications.items.first()?.id; const lastReadId = state.notifications.lastRead; - if (typeof topNotificationId === 'string' && topNotificationId?.includes('+')) { - topNotificationId = topNotificationId.split('+')[0]; - } - if (topNotificationId && (lastReadId === -1 || compareId(topNotificationId, lastReadId) > 0)) { const marker = { notifications: { diff --git a/packages/pl-fe/src/features/notifications/index.tsx b/packages/pl-fe/src/features/notifications/index.tsx index 65ffe0c3f..0544e29ab 100644 --- a/packages/pl-fe/src/features/notifications/index.tsx +++ b/packages/pl-fe/src/features/notifications/index.tsx @@ -30,7 +30,7 @@ const messages = defineMessages({ const getNotifications = createSelector([ (state: RootState) => state.notifications.items.toList(), -], (notifications) => notifications.filter(item => item !== null)); +], (notifications) => notifications.filter(item => item !== null && !item.duplicate)); const Notifications = () => { const dispatch = useAppDispatch(); diff --git a/packages/pl-fe/src/normalizers/notification.ts b/packages/pl-fe/src/normalizers/notification.ts index 7dd9ee83a..ebf483d6a 100644 --- a/packages/pl-fe/src/normalizers/notification.ts +++ b/packages/pl-fe/src/normalizers/notification.ts @@ -2,7 +2,9 @@ import { getNotificationStatus } from 'pl-fe/features/notifications/components/n import { normalizeAccount } from './account'; +import type { OrderedMap as ImmutableOrderedMap } from 'immutable'; import type { Notification as BaseNotification } from 'pl-api'; +import type { MinifiedNotification } from 'pl-fe/reducers/notifications'; const STATUS_NOTIFICATION_TYPES = [ 'favourite', @@ -15,16 +17,22 @@ const STATUS_NOTIFICATION_TYPES = [ const normalizeNotification = (notification: BaseNotification) => ({ ...notification, + duplicate: false, account: normalizeAccount(notification.account), account_id: notification.account.id, accounts: [normalizeAccount(notification.account)], account_ids: [notification.account.id], }); -const normalizeNotifications = (notifications: Array) => { +const normalizeNotifications = (notifications: Array, stateNotifications?: ImmutableOrderedMap) => { const deduplicatedNotifications: Notification[] = []; for (const notification of notifications) { + const existingNotification = stateNotifications?.get(notification.id); + + // Do not update grouped notifications + if (existingNotification && (existingNotification.duplicate || existingNotification.account_ids.length)) continue; + if (STATUS_NOTIFICATION_TYPES.includes(notification.type)) { const existingNotification = deduplicatedNotifications .find(deduplicated => @@ -36,7 +44,7 @@ const normalizeNotifications = (notifications: Array) => { if (existingNotification) { existingNotification.accounts.push(normalizeAccount(notification.account)); existingNotification.account_ids.push(notification.account.id); - existingNotification.id += '+' + notification.id; + deduplicatedNotifications.push({ ...normalizeNotification(notification), duplicate: true }); } else { deduplicatedNotifications.push(normalizeNotification(notification)); } diff --git a/packages/pl-fe/src/reducers/notifications.ts b/packages/pl-fe/src/reducers/notifications.ts index 80eb0b587..afb7ae6b2 100644 --- a/packages/pl-fe/src/reducers/notifications.ts +++ b/packages/pl-fe/src/reducers/notifications.ts @@ -64,6 +64,7 @@ const comparator = (a: Pick, b: Pick) => const minifyNotification = (notification: Notification) => { // @ts-ignore const minifiedNotification: { + duplicate: boolean; account_id: string; account_ids: string[]; created_at: string; @@ -127,7 +128,7 @@ type MinifiedNotification = ReturnType; // Count how many notifications appear after the given ID (for unread count) const countFuture = (notifications: ImmutableOrderedMap, lastId: string | number) => notifications.reduce((acc, notification) => { - if (parseId(notification.id.split('+')[0]) > parseId(lastId)) { + if (parseId(notification.id) > parseId(lastId)) { return acc + 1; } else { return acc;