diff --git a/app/screens/home/channel_list/categories_list/categories/unreads/index.ts b/app/screens/home/channel_list/categories_list/categories/unreads/index.ts index a5e48c687f..4e1c5336ef 100644 --- a/app/screens/home/channel_list/categories_list/categories/unreads/index.ts +++ b/app/screens/home/channel_list/categories_list/categories/unreads/index.ts @@ -39,10 +39,54 @@ type NotifyProps = { [key: string]: Partial; } -const filterMutedFromMyChannels = ([myChannels, notifyProps]: [MyChannelModel[], NotifyProps]) => { - return myChannels.filter( - (myChannel) => notifyProps[myChannel.id]?.mark_unread !== 'mention' || myChannel.mentionsCount > 0, // Muted with Mentions should still go through - ); +const mostRecentFirst = (a: MyChannelModel, b: MyChannelModel) => { + return b.lastPostAt - a.lastPostAt; +}; + +/** + * Filtering / Sorting: + * + * Unreads, Mentions, and Muted Mentions Only + * + * Mentions on top, then unreads, then muted channels with mentions. + * Secondary sorting within each of those is by recent posting. + */ + +const filterAndSortMyChannels = ([myChannels, notifyProps]: [MyChannelModel[], NotifyProps]): MyChannelModel[] => { + const mentions: MyChannelModel[] = []; + const unreads: MyChannelModel[] = []; + const mutedMentions: MyChannelModel[] = []; + + const isMuted = (id: string) => { + return notifyProps[id]?.mark_unread === 'mention'; + }; + + for (const myChannel of myChannels) { + // is it a mention? + if (!isMuted(myChannel.id) && myChannel.mentionsCount > 0) { + mentions.push(myChannel); + continue; + } + + // is it unread? + if (!isMuted(myChannel.id) && myChannel.isUnread) { + unreads.push(myChannel); + continue; + } + + // is it a muted mention? + if (isMuted(myChannel.id) && myChannel.mentionsCount > 0) { + mutedMentions.push(myChannel); + continue; + } + } + + // Sort + mentions.sort(mostRecentFirst); + unreads.sort(mostRecentFirst); + mutedMentions.sort(mostRecentFirst); + + return [...mentions, ...unreads, ...mutedMentions]; }; const enhanced = withObservables(['currentTeamId', 'isTablet'], ({currentTeamId, isTablet, database}: WithDatabaseProps) => { @@ -63,7 +107,7 @@ const enhanced = withObservables(['currentTeamId', 'isTablet'], ({currentTeamId, const unreads = queryMyChannelUnreads(database, currentTeamId).observe().pipe( combineLatestWith(notifyProps), - map(filterMutedFromMyChannels), + map(filterAndSortMyChannels), map(getChannelsFromRelation), concatAll(), );