From 458f71a555786e25e2ae6a72ded905c1f56af29d Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 6 Sep 2024 15:43:26 +0200 Subject: [PATCH] Revert "[Perf] Story Toggler: Avoid expensive global updates on users and chats" This reverts commit 9ab1f9d9789a211140b692e49dac7237d3718edf. --- src/components/story/StoryToggler.tsx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/components/story/StoryToggler.tsx b/src/components/story/StoryToggler.tsx index 294478a02..d5f6b438a 100644 --- a/src/components/story/StoryToggler.tsx +++ b/src/components/story/StoryToggler.tsx @@ -1,6 +1,7 @@ import React, { memo, useEffect, useMemo } from '../../lib/teact/teact'; -import { getActions, getGlobal, withGlobal } from '../../global'; +import { getActions, withGlobal } from '../../global'; +import type { ApiChat, ApiUser } from '../../api/types'; import type { GlobalState } from '../../global/types'; import { ANIMATION_END_DELAY, PREVIEW_AVATAR_COUNT } from '../../config'; @@ -27,6 +28,8 @@ interface StateProps { isShown: boolean; isForumPanelOpen?: boolean; withAnimation?: boolean; + usersById: Record; + chatsById: Record; peerStories: GlobalState['stories']['byPeerId']; } @@ -35,6 +38,8 @@ const PRELOAD_PEERS = 5; function StoryToggler({ currentUserId, orderedPeerIds, + usersById, + chatsById, canShow, isShown, isForumPanelOpen, @@ -47,10 +52,6 @@ function StoryToggler({ const lang = useOldLang(); const peers = useMemo(() => { - // No need for expensive global updates on users, so we avoid them - const usersById = getGlobal().users.byId; - const chatsById = getGlobal().chats.byId; - if (orderedPeerIds.length === 1) { return [usersById[orderedPeerIds[0]] || chatsById[orderedPeerIds[0]]]; } @@ -60,7 +61,7 @@ function StoryToggler({ .filter((peer) => peer && peer.id !== currentUserId) .slice(0, PREVIEW_AVATAR_COUNT) .reverse(); - }, [currentUserId, orderedPeerIds]); + }, [currentUserId, orderedPeerIds, usersById, chatsById]); const closeFriends = useMemo(() => { if (!peers?.length) return {}; @@ -145,6 +146,8 @@ export default memo(withGlobal((global, { isArchived }): StateProps => isShown: isArchived ? !isArchivedRibbonShown : !isRibbonShown, isForumPanelOpen, withAnimation, + usersById: global.users.byId, + chatsById: global.chats.byId, peerStories: byPeerId, }; })(StoryToggler));