[Gekidou] Sort unfiltered find channels by mention / unread (#6340)

* Sort unfiltered find channels by mention / unread

* Prevent Double tap to access find channels
This commit is contained in:
Elias Nahum
2022-06-03 09:09:30 -04:00
committed by GitHub
parent 834c81fd27
commit 6d0e65d5fd
5 changed files with 84 additions and 68 deletions

View File

@@ -5,6 +5,24 @@ import {General} from '@constants';
import type Model from '@nozbe/watermelondb/Model';
import type ChannelModel from '@typings/database/models/servers/channel';
import type MyChannelModel from '@typings/database/models/servers/my_channel';
type NotifyProps = {
[key: string]: Partial<ChannelNotifyProps>;
}
/**
* Filtering / Sorting:
*
* Unreads, Mentions, and Muted Mentions Only
* Mentions on top, then unreads, then muted channels with mentions.
*/
type FilterAndSortMyChannelsArgs = [
MyChannelModel[],
Record<string, ChannelModel>,
NotifyProps,
]
export const extractRecordsForTable = <T>(records: Model[], tableName: string): T[] => {
// @ts-expect-error constructor.table not exposed in type definition
@@ -32,3 +50,44 @@ export function extractChannelDisplayName(raw: Pick<Channel, 'type' | 'display_n
return displayName;
}
export const makeChannelsMap = (channels: ChannelModel[]) => {
return channels.reduce<Record<string, ChannelModel>>((result, c) => {
result[c.id] = c;
return result;
}, {});
};
export const filterAndSortMyChannels = ([myChannels, channels, notifyProps]: FilterAndSortMyChannelsArgs): ChannelModel[] => {
const mentions: ChannelModel[] = [];
const unreads: ChannelModel[] = [];
const mutedMentions: ChannelModel[] = [];
const isMuted = (id: string) => {
return notifyProps[id]?.mark_unread === 'mention';
};
for (const myChannel of myChannels) {
const id = myChannel.id;
// is it a mention?
if (!isMuted(id) && myChannel.mentionsCount > 0 && channels[id]) {
mentions.push(channels[id]);
continue;
}
// is it unread?
if (!isMuted(myChannel.id) && myChannel.isUnread && channels[id]) {
unreads.push(channels[id]);
continue;
}
// is it a muted mention?
if (isMuted(myChannel.id) && myChannel.mentionsCount > 0 && channels[id]) {
mutedMentions.push(channels[id]);
continue;
}
}
return [...mentions, ...unreads, ...mutedMentions];
};