Skip to content

Commit

Permalink
fix: crash when reactor is missing (#7448) (#7449)
Browse files Browse the repository at this point in the history
(cherry picked from commit 40254fb)

Co-authored-by: Elias Nahum <[email protected]>
  • Loading branch information
mattermost-build and enahum authored Jul 12, 2023
1 parent 5eea5ae commit 6b686ab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
21 changes: 12 additions & 9 deletions app/components/user_item/user_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {useTheme} from '@context/theme';
import {nonBreakingString} from '@utils/strings';
import {makeStyleSheetFromTheme, changeOpacity} from '@utils/theme';
import {typography} from '@utils/typography';
import {displayUsername, getUserCustomStatus, isBot, isCustomStatusExpired, isGuest, isShared} from '@utils/user';
import {displayUsername, getUserCustomStatus, isBot, isCustomStatusExpired, isDeactivated, isGuest, isShared} from '@utils/user';

import type UserModel from '@typings/database/models/servers/user';

type AtMentionItemProps = {
FooterComponent?: ReactNode;
user: UserProfile | UserModel;
user?: UserProfile | UserModel;
containerStyle?: StyleProp<ViewStyle>;
currentUserId: string;
includeMargin?: boolean;
Expand Down Expand Up @@ -115,13 +115,12 @@ const UserItem = ({
const bot = user ? isBot(user) : false;
const guest = user ? isGuest(user.roles) : false;
const shared = user ? isShared(user) : false;
const deactivated = user ? isDeactivated(user) : false;

const isCurrentUser = currentUserId === user?.id;
const customStatus = getUserCustomStatus(user);
const customStatusExpired = isCustomStatusExpired(user);

const deleteAt = 'deleteAt' in user ? user.deleteAt : user.delete_at;

let displayName = displayUsername(user, locale, teammateNameDisplay);
const showTeammateDisplay = displayName !== user?.username;
if (isCurrentUser) {
Expand All @@ -142,11 +141,15 @@ const UserItem = ({
}, [disabled, padding, includeMargin]);

const onPress = useCallback(() => {
onUserPress?.(user);
if (user) {
onUserPress?.(user);
}
}, [user, onUserPress]);

const onLongPress = useCallback(() => {
onUserLongPress?.(user);
if (user) {
onUserLongPress?.(user);
}
}, [user, onUserLongPress]);

return (
Expand Down Expand Up @@ -175,15 +178,15 @@ const UserItem = ({
testID={`${userItemTestId}.display_name`}
>
{nonBreakingString(displayName)}
{Boolean(showTeammateDisplay) && (
{Boolean(showTeammateDisplay) && Boolean(user?.username) && (
<Text
style={style.rowUsername}
testID={`${userItemTestId}.username`}
>
{nonBreakingString(` @${user!.username}`)}
{nonBreakingString(` @${user?.username}`)}
</Text>
)}
{Boolean(deleteAt) && (
{deactivated && (
<Text
style={style.rowUsername}
testID={`${userItemTestId}.deactivated`}
Expand Down
17 changes: 14 additions & 3 deletions app/screens/reactions/reactors_list/reactor/reactor.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';
import React, {useEffect} from 'react';
import {useIntl} from 'react-intl';
import {Keyboard} from 'react-native';

import {fetchUsersByIds} from '@actions/remote/user';
import UserItem from '@components/user_item';
import {Screens} from '@constants';
import {useServerUrl} from '@context/server';
import {useTheme} from '@context/theme';
import {dismissBottomSheet, openAsBottomSheet} from '@screens/navigation';

import type ReactionModel from '@typings/database/models/servers/reaction';
import type UserModel from '@typings/database/models/servers/user';

type Props = {
channelId: string;
location: string;
user: UserModel;
reaction: ReactionModel;
user?: UserModel;
}

const Reactor = ({channelId, location, user}: Props) => {
const Reactor = ({channelId, location, reaction, user}: Props) => {
const intl = useIntl();
const theme = useTheme();
const serverUrl = useServerUrl();
const openUserProfile = async () => {
if (user) {
await dismissBottomSheet(Screens.REACTIONS);
Expand All @@ -34,6 +39,12 @@ const Reactor = ({channelId, location, user}: Props) => {
}
};

useEffect(() => {
if (!user) {
fetchUsersByIds(serverUrl, [reaction.userId]);
}
}, []);

return (
<UserItem
user={user}
Expand Down
4 changes: 4 additions & 0 deletions app/utils/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ export function isShared(user: UserProfile | UserModel): boolean {
return ('remoteId' in user) ? Boolean(user.remoteId) : Boolean(user.remote_id);
}

export function isDeactivated(user: UserProfile | UserModel): boolean {
return Boolean('deleteAt' in user ? user.deleteAt : user.delete_at);
}

export function removeUserFromList(userId: string, originalList: UserProfile[]): UserProfile[] {
const list = [...originalList];
for (let i = list.length - 1; i >= 0; i--) {
Expand Down

0 comments on commit 6b686ab

Please sign in to comment.