Fixes grouped unread sorting (#6230)

* Fixes grouped unread sorting

* Type fix
This commit is contained in:
Shaz MJ
2022-05-05 22:45:50 +10:00
committed by GitHub
parent 362db9d98d
commit c4e1b4ad3e

View File

@@ -39,10 +39,54 @@ type NotifyProps = {
[key: string]: Partial<ChannelNotifyProps>;
}
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(),
);