Folder Manager: Fix blank spaces in Chat List

This commit is contained in:
Alexander Zinchuk 2022-02-11 19:39:17 +01:00
parent 02a0d2db66
commit 7dd21ffc6c

View File

@ -72,6 +72,7 @@ let prepared: {
let results: { let results: {
orderedIdsByFolderId: Record<string, string[] | undefined>; orderedIdsByFolderId: Record<string, string[] | undefined>;
pinnedCountByFolderId: Record<string, number | undefined>; // Also watched by `callbacks.orderedIdsByFolderId`
chatsCountByFolderId: Record<string, number | undefined>; chatsCountByFolderId: Record<string, number | undefined>;
unreadCountersByFolderId: Record<string, { unreadCountersByFolderId: Record<string, {
chatsCount: number; chatsCount: number;
@ -110,6 +111,12 @@ export function getOrderedIds(folderId: number) {
return results.orderedIdsByFolderId[folderId]; return results.orderedIdsByFolderId[folderId];
} }
export function getPinnedChatsCount(folderId: number) {
if (!inited) init();
return results.pinnedCountByFolderId[folderId] || 0;
}
export function getChatsCount() { export function getChatsCount() {
if (!inited) init(); if (!inited) init();
@ -126,10 +133,6 @@ export function getAllNotificationsCount() {
return getUnreadCounters()[ALL_FOLDER_ID]?.notificationsCount || 0; return getUnreadCounters()[ALL_FOLDER_ID]?.notificationsCount || 0;
} }
export function getPinnedChatsCount(folderId: number) {
return prepared.folderSummariesById[folderId]?.pinnedChatIds?.size;
}
/* Callback managers */ /* Callback managers */
export function addOrderedIdsCallback(folderId: number, callback: (orderedIds: string[]) => void) { export function addOrderedIdsCallback(folderId: number, callback: (orderedIds: string[]) => void) {
@ -550,21 +553,24 @@ function updateResults(affectedFolderIds: number[]) {
let wasChatsCountChanged = false; let wasChatsCountChanged = false;
Array.from(affectedFolderIds).forEach((folderId) => { Array.from(affectedFolderIds).forEach((folderId) => {
const newOrderedIds = buildFolderOrderedIds(folderId); const { pinnedCount: newPinnedCount, orderedIds: newOrderedIds } = buildFolderOrderedIds(folderId);
// When signed out // When signed out
if (!newOrderedIds) { if (!newOrderedIds) {
return; return;
} }
const currentOrderedIds = results.orderedIdsByFolderId[folderId]; const currentOrderedIds = results.orderedIdsByFolderId[folderId];
const currentPinnedCount = results.pinnedCountByFolderId[folderId];
const areOrderedIdsChanged = ( const areOrderedIdsChanged = (
!currentOrderedIds !currentOrderedIds
|| currentPinnedCount === undefined || currentPinnedCount !== newPinnedCount
|| prepared.isOrderedListJustPatched[folderId] || prepared.isOrderedListJustPatched[folderId]
|| !areSortedArraysEqual(newOrderedIds, currentOrderedIds) || !areSortedArraysEqual(newOrderedIds, currentOrderedIds)
); );
if (areOrderedIdsChanged) { if (areOrderedIdsChanged) {
prepared.isOrderedListJustPatched[folderId] = false; prepared.isOrderedListJustPatched[folderId] = false;
results.orderedIdsByFolderId[folderId] = newOrderedIds; results.orderedIdsByFolderId[folderId] = newOrderedIds;
results.pinnedCountByFolderId[folderId] = newPinnedCount;
callbacks.orderedIdsByFolderId[folderId]?.runCallbacks(newOrderedIds); callbacks.orderedIdsByFolderId[folderId]?.runCallbacks(newOrderedIds);
} }
@ -603,7 +609,7 @@ function updateResults(affectedFolderIds: number[]) {
function buildFolderOrderedIds(folderId: number) { function buildFolderOrderedIds(folderId: number) {
const folderSummary = prepared.folderSummariesById[folderId]; const folderSummary = prepared.folderSummariesById[folderId];
if (!folderSummary) { if (!folderSummary) {
return undefined; return {};
} }
const { orderedPinnedIds, pinnedChatIds } = folderSummary; const { orderedPinnedIds, pinnedChatIds } = folderSummary;
@ -615,16 +621,20 @@ function buildFolderOrderedIds(folderId: number) {
orderedIdsByFolderId: { [folderId]: prevOrderedIds }, orderedIdsByFolderId: { [folderId]: prevOrderedIds },
} = results; } = results;
const sortedPinnedIds = chatIds ? orderedPinnedIds?.filter((id) => chatIds.has(id)) : orderedPinnedIds;
const allListIds = prevOrderedIds || (chatIds && Array.from(chatIds)) || []; const allListIds = prevOrderedIds || (chatIds && Array.from(chatIds)) || [];
const notPinnedIds = pinnedChatIds ? allListIds.filter((id) => !pinnedChatIds.has(id)) : allListIds; const notPinnedIds = pinnedChatIds ? allListIds.filter((id) => !pinnedChatIds.has(id)) : allListIds;
const sortedNotPinnedIds = notPinnedIds.sort((chatId1: string, chatId2: string) => { const sortedNotPinnedIds = notPinnedIds.sort((chatId1: string, chatId2: string) => {
return chatSummariesById.get(chatId2)!.order - chatSummariesById.get(chatId1)!.order; return chatSummariesById.get(chatId2)!.order - chatSummariesById.get(chatId1)!.order;
}); });
return [ return {
...(orderedPinnedIds || []), pinnedCount: sortedPinnedIds?.length || 0,
orderedIds: [
...(sortedPinnedIds || []),
...sortedNotPinnedIds, ...sortedNotPinnedIds,
]; ],
};
} }
function buildFolderUnreadCounters(folderId: number) { function buildFolderUnreadCounters(folderId: number) {
@ -684,6 +694,7 @@ function buildInitials() {
results: { results: {
orderedIdsByFolderId: {}, orderedIdsByFolderId: {},
pinnedCountByFolderId: {},
chatsCountByFolderId: {}, chatsCountByFolderId: {},
unreadCountersByFolderId: {}, unreadCountersByFolderId: {},
}, },