From b9024f51839bba52598709a7ad91021ef34322f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Tue, 31 Oct 2023 15:59:54 +0100 Subject: [PATCH] Fix error around combined user activity (#7637) --- .../combined_user_activity.tsx | 38 +++++++++++-------- .../post_list/combined_user_activity/index.ts | 27 +++++++------ 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/app/components/post_list/combined_user_activity/combined_user_activity.tsx b/app/components/post_list/combined_user_activity/combined_user_activity.tsx index f29225ba0..845688c04 100644 --- a/app/components/post_list/combined_user_activity/combined_user_activity.tsx +++ b/app/components/post_list/combined_user_activity/combined_user_activity.tsx @@ -25,7 +25,7 @@ type Props = { currentUserId?: string; currentUsername?: string; location: string; - post: Post; + post: Post | null; showJoinLeave: boolean; testID?: string; theme: Theme; @@ -65,23 +65,11 @@ const CombinedUserActivity = ({ const intl = useIntl(); const isTablet = useIsTablet(); const serverUrl = useServerUrl(); - const itemTestID = `${testID}.${post.id}`; const textStyles = getMarkdownTextStyles(theme); - const {allUserIds, allUsernames, messageData} = post.props.user_activity; const styles = getStyleSheet(theme); const content = []; const removedUserIds: string[] = []; - const loadUserProfiles = () => { - if (allUserIds.length) { - fetchMissingProfilesByIds(serverUrl, allUserIds); - } - - if (allUsernames.length) { - fetchMissingProfilesByUsernames(serverUrl, allUsernames); - } - }; - const getUsernames = (userIds: string[]) => { const someone = intl.formatMessage({id: 'channel_loader.someone', defaultMessage: 'Someone'}); const you = intl.formatMessage({id: 'combined_system_message.you', defaultMessage: 'You'}); @@ -120,6 +108,9 @@ const CombinedUserActivity = ({ }, [post, canDelete, isTablet, intl, location]); const renderMessage = (postType: string, userIds: string[], actorId: string) => { + if (!post) { + return null; + } let actor = ''; if (usernamesById[actorId]) { actor = `@${usernamesById[actorId]}`; @@ -177,9 +168,26 @@ const CombinedUserActivity = ({ }; useEffect(() => { - loadUserProfiles(); - }, [allUserIds, allUsernames]); + if (!post) { + return; + } + const {allUserIds, allUsernames} = post.props.user_activity; + if (allUserIds.length) { + fetchMissingProfilesByIds(serverUrl, allUserIds); + } + + if (allUsernames.length) { + fetchMissingProfilesByUsernames(serverUrl, allUsernames); + } + }, [post?.props.user_activity.allUserIds, post?.props.user_activity.allUsernames]); + + if (!post) { + return null; + } + + const itemTestID = `${testID}.${post.id}`; + const {messageData} = post.props.user_activity; for (const message of messageData) { const {postType, actorId} = message; const userIds = new Set(message.userIds); diff --git a/app/components/post_list/combined_user_activity/index.ts b/app/components/post_list/combined_user_activity/index.ts index 72bcfa6cb..32842e06e 100644 --- a/app/components/post_list/combined_user_activity/index.ts +++ b/app/components/post_list/combined_user_activity/index.ts @@ -28,24 +28,29 @@ const withCombinedPosts = withObservables(['postId'], ({database, postId}: WithD // Columns observed: `props` is used by `usernamesById`. `message` is used by generateCombinedPost. const posts = queryPostsById(database, postIds).observeWithColumns(['props', 'message']); - const post = posts.pipe(map((ps) => generateCombinedPost(postId, ps))); + const post = posts.pipe(map((ps) => (ps.length ? generateCombinedPost(postId, ps) : null))); const canDelete = combineLatest([posts, currentUser]).pipe( switchMap(([ps, u]) => (ps.length ? observePermissionForPost(database, ps[0], u, Permissions.DELETE_OTHERS_POSTS, false) : of$(false))), ); const usernamesById = post.pipe( switchMap( - (p) => queryUsersByIdsOrUsernames(database, p.props.user_activity.allUserIds, p.props.user_activity.allUsernames).observeWithColumns(['username']). - pipe( - // eslint-disable-next-line max-nested-callbacks - switchMap((users) => { + (p) => { + if (!p) { + return of$>({}); + } + return queryUsersByIdsOrUsernames(database, p.props.user_activity.allUserIds, p.props.user_activity.allUsernames).observeWithColumns(['username']). + pipe( // eslint-disable-next-line max-nested-callbacks - return of$(users.reduce((acc: Record, user) => { - acc[user.id] = user.username; - return acc; - }, {})); - }), - ), + switchMap((users) => { + // eslint-disable-next-line max-nested-callbacks + return of$(users.reduce((acc: Record, user) => { + acc[user.id] = user.username; + return acc; + }, {})); + }), + ); + }, ), );