FAB: Fix mentions FAB freezing (#4713)

This commit is contained in:
Alexander Zinchuk 2024-07-15 15:50:47 +02:00
parent eb6694d061
commit 0f2c670e0f
4 changed files with 58 additions and 23 deletions

View File

@ -26,6 +26,7 @@ type StateProps = {
messageListType?: MessageListType; messageListType?: MessageListType;
unreadCount?: number; unreadCount?: number;
unreadReactions?: number[]; unreadReactions?: number[];
unreadMentions?: number[];
reactionsCount?: number; reactionsCount?: number;
mentionsCount?: number; mentionsCount?: number;
}; };
@ -39,6 +40,7 @@ const FloatingActionButtons: FC<OwnProps & StateProps> = ({
chatId, chatId,
unreadCount, unreadCount,
unreadReactions, unreadReactions,
unreadMentions,
reactionsCount, reactionsCount,
mentionsCount, mentionsCount,
withExtraShift, withExtraShift,
@ -66,6 +68,12 @@ const FloatingActionButtons: FC<OwnProps & StateProps> = ({
} }
}, [chatId, fetchUnreadReactions, hasUnreadReactions]); }, [chatId, fetchUnreadReactions, hasUnreadReactions]);
useEffect(() => {
if (hasUnreadMentions && chatId && !unreadMentions?.length) {
fetchUnreadMentions({ chatId });
}
}, [chatId, fetchUnreadMentions, hasUnreadMentions, unreadMentions?.length]);
useEffect(() => { useEffect(() => {
if (hasUnreadMentions && chatId) { if (hasUnreadMentions && chatId) {
fetchUnreadMentions({ chatId }); fetchUnreadMentions({ chatId });
@ -153,6 +161,7 @@ export default memo(withGlobal<OwnProps>(
chatId, chatId,
reactionsCount: shouldShowCount ? chat.unreadReactionsCount : undefined, reactionsCount: shouldShowCount ? chat.unreadReactionsCount : undefined,
unreadReactions: shouldShowCount ? chat.unreadReactions : undefined, unreadReactions: shouldShowCount ? chat.unreadReactions : undefined,
unreadMentions: shouldShowCount ? chat.unreadMentions : undefined,
mentionsCount: shouldShowCount ? chat.unreadMentionsCount : undefined, mentionsCount: shouldShowCount ? chat.unreadMentionsCount : undefined,
unreadCount: shouldShowCount ? chat.unreadCount : undefined, unreadCount: shouldShowCount ? chat.unreadCount : undefined,
}; };

View File

@ -67,10 +67,12 @@ import {
import { import {
addChatMessagesById, addChatMessagesById,
addChats, addChats,
addUnreadMentions,
addUsers, addUsers,
deleteSponsoredMessage, deleteSponsoredMessage,
removeOutlyingList, removeOutlyingList,
removeRequestedMessageTranslation, removeRequestedMessageTranslation,
removeUnreadMentions,
replaceSettings, replaceSettings,
replaceThreadParam, replaceThreadParam,
replaceUserStatuses, replaceUserStatuses,
@ -1655,9 +1657,7 @@ async function fetchUnreadMentions<T extends GlobalState>(global: T, chatId: str
global = addChatMessagesById(global, chat.id, byId); global = addChatMessagesById(global, chat.id, byId);
global = addUsers(global, buildCollectionByKey(users, 'id')); global = addUsers(global, buildCollectionByKey(users, 'id'));
global = addChats(global, buildCollectionByKey(chats, 'id')); global = addChats(global, buildCollectionByKey(chats, 'id'));
global = updateChat(global, chatId, { global = addUnreadMentions(global, chatId, chat, ids);
unreadMentions: [...(chat.unreadMentions || []), ...ids],
});
setGlobal(global); setGlobal(global);
} }
@ -1668,18 +1668,7 @@ addActionHandler('markMentionsRead', (global, actions, payload): ActionReturnTyp
const chat = selectCurrentChat(global, tabId); const chat = selectCurrentChat(global, tabId);
if (!chat) return; if (!chat) return;
const currentUnreadMentions = chat.unreadMentions || []; global = removeUnreadMentions(global, chat.id, chat, messageIds, true);
const unreadMentions = currentUnreadMentions.filter((id) => !messageIds.includes(id));
const removedCount = currentUnreadMentions.length - unreadMentions.length;
global = updateChat(global, chat.id, {
...(chat.unreadMentionsCount && {
unreadMentionsCount: Math.max(chat.unreadMentionsCount - removedCount, 0) || undefined,
}),
unreadMentions,
});
setGlobal(global); setGlobal(global);
actions.markMessagesRead({ messageIds, tabId }); actions.markMessagesRead({ messageIds, tabId });

View File

@ -12,8 +12,10 @@ import {
addActionHandler, getGlobal, setGlobal, addActionHandler, getGlobal, setGlobal,
} from '../../index'; } from '../../index';
import { import {
addUnreadMentions,
deleteChatMessages, deleteChatMessages,
leaveChat, leaveChat,
removeUnreadMentions,
replaceThreadParam, replaceThreadParam,
updateChat, updateChat,
updateChatFullInfo, updateChatFullInfo,
@ -177,13 +179,10 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
global = updateChat(global, update.chatId, { global = updateChat(global, update.chatId, {
unreadCount: chat.unreadCount ? chat.unreadCount + 1 : 1, unreadCount: chat.unreadCount ? chat.unreadCount + 1 : 1,
...(hasMention && { unreadMentionsCount: (chat.unreadMentionsCount || 0) + 1 }),
}); });
if (hasMention) { if (hasMention) {
global = updateChat(global, update.chatId, { global = addUnreadMentions(global, update.chatId, chat, [update.message.id!], true);
unreadMentions: [...(chat.unreadMentions || []), update.message.id!],
});
} }
const topic = chat.isForum ? selectTopicFromMessage(global, message as ApiMessage) : undefined; const topic = chat.isForum ? selectTopicFromMessage(global, message as ApiMessage) : undefined;
@ -220,10 +219,7 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
} }
if (!messageUpdate.hasUnreadMention && chat?.unreadMentionsCount) { if (!messageUpdate.hasUnreadMention && chat?.unreadMentionsCount) {
global = updateChat(global, chatId, { global = removeUnreadMentions(global, chatId, chat, [id], true);
unreadMentionsCount: Math.max(chat.unreadMentionsCount - 1, 0) || undefined,
unreadMentions: chat.unreadMentions?.filter((i) => i !== id),
});
} }
}); });

View File

@ -96,6 +96,47 @@ export function replaceChats<T extends GlobalState>(global: T, newById: Record<s
}; };
} }
export function addUnreadMentions<T extends GlobalState>(
global: T, chatId: string, chat: ApiChat, ids: number[], shouldUpdateCount: boolean = false,
): T {
const prevChatUnreadMentions = (chat.unreadMentions || []);
const updatedUnreadMentions = unique([...prevChatUnreadMentions, ...ids]).sort((a, b) => b - a);
global = updateChat(global, chatId, {
unreadMentions: updatedUnreadMentions,
});
if (shouldUpdateCount) {
const updatedUnreadMentionsCount = (chat.unreadMentionsCount || 0)
+ Math.max(0, updatedUnreadMentions.length - prevChatUnreadMentions.length);
global = updateChat(global, chatId, {
unreadMentionsCount: updatedUnreadMentionsCount,
});
}
return global;
}
export function removeUnreadMentions<T extends GlobalState>(
global: T, chatId: string, chat: ApiChat, ids: number[], shouldUpdateCount: boolean = false,
): T {
const prevChatUnreadMentions = (chat.unreadMentions || []);
const updatedUnreadMentions = prevChatUnreadMentions?.filter((id) => !ids.includes(id));
global = updateChat(global, chatId, {
unreadMentions: updatedUnreadMentions,
});
if (shouldUpdateCount && chat.unreadMentionsCount) {
const removedCount = prevChatUnreadMentions.length - updatedUnreadMentions.length;
const updatedUnreadMentionsCount = Math.max(chat.unreadMentionsCount - removedCount, 0) || undefined;
global = updateChat(global, chatId, {
unreadMentionsCount: updatedUnreadMentionsCount,
});
}
return global;
}
export function updateChat<T extends GlobalState>( export function updateChat<T extends GlobalState>(
global: T, chatId: string, chatUpdate: Partial<ApiChat>, photo?: ApiPhoto, global: T, chatId: string, chatUpdate: Partial<ApiChat>, photo?: ApiPhoto,
noOmitUnreadReactionCount = false, noOmitUnreadReactionCount = false,