diff --git a/lib/hooks/thread-hooks.js b/lib/hooks/thread-hooks.js new file mode 100644 --- /dev/null +++ b/lib/hooks/thread-hooks.js @@ -0,0 +1,39 @@ +// @flow + +import * as React from 'react'; + +import { useChatMentionContext } from './chat-mention-hooks.js'; +import { childThreadInfos } from '../selectors/thread-selectors.js'; +import { getCommunity } from '../shared/thread-utils.js'; +import type { ResolvedThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; +import { useSelector } from '../utils/redux-utils.js'; + +function useChildThreadInfosMap(): { + +[id: string]: $ReadOnlyArray, +} { + const childThreadInfosMap = useSelector(childThreadInfos); + + const { chatMentionCandidatesObj } = useChatMentionContext(); + + return React.useMemo(() => { + const result: { [id: string]: $ReadOnlyArray } = {}; + for (const parentThreadID in childThreadInfosMap) { + result[parentThreadID] = childThreadInfosMap[parentThreadID] + .map(rawThreadInfo => { + const community = getCommunity(rawThreadInfo); + if (!community) { + return undefined; + } + const communityThreads = chatMentionCandidatesObj[community]; + if (!communityThreads) { + return undefined; + } + return communityThreads[rawThreadInfo.id]?.threadInfo; + }) + .filter(Boolean); + } + return result; + }, [childThreadInfosMap, chatMentionCandidatesObj]); +} + +export { useChildThreadInfosMap }; diff --git a/lib/utils/drawer-utils.react.js b/lib/utils/drawer-utils.react.js --- a/lib/utils/drawer-utils.react.js +++ b/lib/utils/drawer-utils.react.js @@ -12,7 +12,7 @@ import { communitySubthreads } from '../types/thread-types-enum.js'; type WritableCommunityDrawerItemData = { - threadInfo: ThreadInfo, + threadInfo: ResolvedThreadInfo, itemChildren: $ReadOnlyArray>, hasSubchannelsButton: boolean, labelStyle: T, @@ -23,7 +23,7 @@ function createRecursiveDrawerItemsData( childThreadInfosMap: { - +[id: string]: $ReadOnlyArray, + +[id: string]: $ReadOnlyArray, }, communities: $ReadOnlyArray, labelStyles: $ReadOnlyArray, @@ -63,9 +63,9 @@ } function threadHasSubchannels( - threadInfo: ThreadInfo, + threadInfo: ResolvedThreadInfo, childThreadInfosMap: { - +[id: string]: $ReadOnlyArray, + +[id: string]: $ReadOnlyArray, }, ): boolean { if (!childThreadInfosMap[threadInfo.id]?.length) { diff --git a/native/navigation/community-drawer-content.react.js b/native/navigation/community-drawer-content.react.js --- a/native/navigation/community-drawer-content.react.js +++ b/native/navigation/community-drawer-content.react.js @@ -5,7 +5,6 @@ import * as React from 'react'; import { FlatList, Platform, View, Text, TouchableOpacity } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; -import { useSelector } from 'react-redux'; import { fetchCommunityInfosActionTypes, @@ -15,10 +14,8 @@ fetchPrimaryInviteLinkActionTypes, useFetchPrimaryInviteLinks, } from 'lib/actions/link-actions.js'; -import { - childThreadInfos, - communityThreadSelector, -} from 'lib/selectors/thread-selectors.js'; +import { useChildThreadInfosMap } from 'lib/hooks/thread-hooks.js'; +import { communityThreadSelector } from 'lib/selectors/thread-selectors.js'; import { threadTypeIsCommunityRoot } from 'lib/types/thread-types-enum.js'; import { createRecursiveDrawerItemsData, @@ -31,6 +28,7 @@ import { CommunityCreationRouteName } from './route-names.js'; import { useNavigateToThread } from '../chat/message-list-types.js'; import SWMansionIcon from '../components/swmansion-icon.react.js'; +import { useSelector } from '../redux/redux-utils.js'; import { useStyles } from '../themes/colors.js'; import { flattenDrawerItemsData, @@ -96,7 +94,7 @@ ], [labelStylesObj], ); - const childThreadInfosMap = useSelector(childThreadInfos); + const childThreadInfosMap = useChildThreadInfosMap(); const drawerItemsData = React.useMemo( () => diff --git a/web/sidebar/community-drawer.react.js b/web/sidebar/community-drawer.react.js --- a/web/sidebar/community-drawer.react.js +++ b/web/sidebar/community-drawer.react.js @@ -2,10 +2,8 @@ import * as React from 'react'; -import { - childThreadInfos, - communityThreadSelector, -} from 'lib/selectors/thread-selectors.js'; +import { useChildThreadInfosMap } from 'lib/hooks/thread-hooks.js'; +import { communityThreadSelector } from 'lib/selectors/thread-selectors.js'; import { createRecursiveDrawerItemsData, useAppendCommunitySuffix, @@ -22,7 +20,7 @@ function CommunityDrawer(): React.Node { const tab = useSelector(state => state.navInfo.tab); - const childThreadInfosMap = useSelector(childThreadInfos); + const childThreadInfosMap = useChildThreadInfosMap(); const communities = useSelector(communityThreadSelector); const resolvedCommunities = useResolvedThreadInfos(communities); const communitiesSuffixed = useAppendCommunitySuffix(resolvedCommunities);