diff --git a/web/avatars/web-edit-thread-avatar-provider.react.js b/web/avatars/web-edit-thread-avatar-provider.react.js --- a/web/avatars/web-edit-thread-avatar-provider.react.js +++ b/web/avatars/web-edit-thread-avatar-provider.react.js @@ -4,15 +4,14 @@ import { BaseEditThreadAvatarProvider } from 'lib/components/base-edit-thread-avatar-provider.react.js'; -import { useSelector } from '../redux/redux-utils.js'; -import { activeChatThreadItem as activeChatThreadItemSelector } from '../selectors/chat-selectors.js'; +import { useActiveChatThreadItem } from '../selectors/chat-selectors.js'; type Props = { +children: React.Node, }; function WebEditThreadAvatarProvider(props: Props): React.Node { const { children } = props; - const activeChatThreadItem = useSelector(activeChatThreadItemSelector); + const activeChatThreadItem = useActiveChatThreadItem(); const activeThreadID = activeChatThreadItem?.threadInfo?.id ?? ''; return ( diff --git a/web/chat/thread-list-provider.js b/web/chat/thread-list-provider.js --- a/web/chat/thread-list-provider.js +++ b/web/chat/thread-list-provider.js @@ -22,7 +22,7 @@ import { useSelector } from '../redux/redux-utils.js'; import { useChatThreadItem, - activeChatThreadItem as activeChatThreadItemSelector, + useActiveChatThreadItem, } from '../selectors/chat-selectors.js'; type ChatTabType = 'Home' | 'Muted'; @@ -43,7 +43,7 @@ function ThreadListProvider(props: ThreadListProviderProps): React.Node { const [activeTab, setActiveTab] = React.useState('Home'); - const activeChatThreadItem = useSelector(activeChatThreadItemSelector); + const activeChatThreadItem = useActiveChatThreadItem(); const activeThreadInfo = activeChatThreadItem?.threadInfo; const activeThreadID = activeThreadInfo?.id; const activeSidebarParentThreadInfo = useSelector(state => { diff --git a/web/selectors/chat-selectors.js b/web/selectors/chat-selectors.js --- a/web/selectors/chat-selectors.js +++ b/web/selectors/chat-selectors.js @@ -1,7 +1,6 @@ // @flow import * as React from 'react'; -import { createSelector } from 'reselect'; import { type ChatThreadItem, @@ -11,51 +10,10 @@ import { sidebarInfoSelector } from 'lib/selectors/sidebar-selectors.js'; import { threadInfoSelector } from 'lib/selectors/thread-selectors.js'; import { threadIsPending } from 'lib/shared/thread-utils.js'; -import type { MessageInfo, MessageStore } from 'lib/types/message-types.js'; import type { ThreadInfo } from 'lib/types/minimally-encoded-thread-permissions-types.js'; -import type { SidebarInfo } from 'lib/types/thread-types.js'; -import type { AppState } from '../redux/redux-setup.js'; import { useSelector } from '../redux/redux-utils.js'; -const activeChatThreadItem: (state: AppState) => ?ChatThreadItem = - createSelector( - threadInfoSelector, - (state: AppState) => state.messageStore, - messageInfoSelector, - (state: AppState) => state.navInfo.activeChatThreadID, - (state: AppState) => state.navInfo.pendingThread, - sidebarInfoSelector, - ( - threadInfos: { - +[id: string]: ThreadInfo, - }, - messageStore: MessageStore, - messageInfos: { +[id: string]: ?MessageInfo }, - activeChatThreadID: ?string, - pendingThreadInfo: ?ThreadInfo, - sidebarInfos: { +[id: string]: $ReadOnlyArray }, - ): ?ChatThreadItem => { - if (!activeChatThreadID) { - return null; - } - const isPending = threadIsPending(activeChatThreadID); - const threadInfo = isPending - ? pendingThreadInfo - : threadInfos[activeChatThreadID]; - - if (!threadInfo) { - return null; - } - return createChatThreadItem( - threadInfo, - messageStore, - messageInfos, - sidebarInfos[threadInfo.id], - ); - }, - ); - function useChatThreadItem(threadInfo: ?ThreadInfo): ?ChatThreadItem { const messageInfos = useSelector(messageInfoSelector); const sidebarInfos = useSelector(sidebarInfoSelector); @@ -74,4 +32,21 @@ ); }, [messageInfos, messageStore, sidebarInfos, threadInfo]); } -export { useChatThreadItem, activeChatThreadItem }; + +function useActiveChatThreadItem(): ?ChatThreadItem { + const activeChatThreadID = useSelector( + state => state.navInfo.activeChatThreadID, + ); + const pendingThreadInfo = useSelector(state => state.navInfo.pendingThread); + const threadInfos = useSelector(threadInfoSelector); + const threadInfo = React.useMemo(() => { + if (!activeChatThreadID) { + return null; + } + const isPending = threadIsPending(activeChatThreadID); + return isPending ? pendingThreadInfo : threadInfos[activeChatThreadID]; + }, [activeChatThreadID, pendingThreadInfo, threadInfos]); + return useChatThreadItem(threadInfo); +} + +export { useChatThreadItem, useActiveChatThreadItem };