Chore: Evaluate `Avatar` - TypeScript (#3929)

* update: `Avatar` component

remove: non-null assertion from ThreadMessagesView Item

* remove: `this.mounted` from `Avatar` component

update: navParam on MessageAvatar component
This commit is contained in:
Gerzon Z 2022-03-29 12:23:45 -04:00 committed by GitHub
parent 93c9d2efdf
commit 1c70ffc614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 49 deletions

View File

@ -8,6 +8,7 @@ import { avatarURL } from '../../utils/avatar';
import { SubscriptionType } from '../../definitions/ISubscription'; import { SubscriptionType } from '../../definitions/ISubscription';
import Emoji from '../markdown/Emoji'; import Emoji from '../markdown/Emoji';
import { IAvatar } from './interfaces'; import { IAvatar } from './interfaces';
import { useTheme } from '../../theme';
const Avatar = React.memo( const Avatar = React.memo(
({ ({
@ -18,7 +19,6 @@ const Avatar = React.memo(
user, user,
onPress, onPress,
emoji, emoji,
theme,
getCustomEmoji, getCustomEmoji,
avatarETag, avatarETag,
isStatic, isStatic,
@ -34,6 +34,8 @@ const Avatar = React.memo(
return null; return null;
} }
const { theme } = useTheme();
const avatarStyle = { const avatarStyle = {
width: size, width: size,
height: size, height: size,
@ -44,7 +46,7 @@ const Avatar = React.memo(
if (emoji) { if (emoji) {
image = ( image = (
<Emoji <Emoji
theme={theme!} theme={theme}
baseUrl={server} baseUrl={server}
getCustomEmoji={getCustomEmoji} getCustomEmoji={getCustomEmoji}
isMessageContainsOnlyEmoji isMessageContainsOnlyEmoji

View File

@ -5,13 +5,11 @@ import { Observable, Subscription } from 'rxjs';
import database from '../../lib/database'; import database from '../../lib/database';
import { getUserSelector } from '../../selectors/login'; import { getUserSelector } from '../../selectors/login';
import { TSubscriptionModel, TUserModel } from '../../definitions'; import { IApplicationState, TSubscriptionModel, TUserModel } from '../../definitions';
import Avatar from './Avatar'; import Avatar from './Avatar';
import { IAvatar } from './interfaces'; import { IAvatar } from './interfaces';
class AvatarContainer extends React.Component<IAvatar, any> { class AvatarContainer extends React.Component<IAvatar, any> {
private mounted: boolean;
private subscription?: Subscription; private subscription?: Subscription;
static defaultProps = { static defaultProps = {
@ -21,16 +19,11 @@ class AvatarContainer extends React.Component<IAvatar, any> {
constructor(props: IAvatar) { constructor(props: IAvatar) {
super(props); super(props);
this.mounted = false;
this.state = { avatarETag: '' }; this.state = { avatarETag: '' };
this.init(); this.init();
} }
componentDidMount() { componentDidUpdate(prevProps: IAvatar) {
this.mounted = true;
}
componentDidUpdate(prevProps: any) {
const { text, type } = this.props; const { text, type } = this.props;
if (prevProps.text !== text || prevProps.type !== type) { if (prevProps.text !== text || prevProps.type !== type) {
this.init(); this.init();
@ -88,12 +81,7 @@ class AvatarContainer extends React.Component<IAvatar, any> {
const observable = record.observe() as Observable<TSubscriptionModel | TUserModel>; const observable = record.observe() as Observable<TSubscriptionModel | TUserModel>;
this.subscription = observable.subscribe(r => { this.subscription = observable.subscribe(r => {
const { avatarETag } = r; const { avatarETag } = r;
if (this.mounted) { this.setState({ avatarETag });
this.setState({ avatarETag });
} else {
// @ts-ignore
this.state.avatarETag = avatarETag;
}
}); });
} }
}; };
@ -105,12 +93,12 @@ class AvatarContainer extends React.Component<IAvatar, any> {
} }
} }
const mapStateToProps = (state: any) => ({ const mapStateToProps = (state: IApplicationState) => ({
user: getUserSelector(state), user: getUserSelector(state),
server: state.share.server.server || state.server.server, server: state.share.server.server || state.server.server,
serverVersion: state.share.server.version || state.server.version, serverVersion: state.share.server.version || state.server.version,
blockUnauthenticatedAccess: blockUnauthenticatedAccess:
state.share.settings?.Accounts_AvatarBlockUnauthenticatedAccess ?? (state.share.settings?.Accounts_AvatarBlockUnauthenticatedAccess as boolean) ??
state.settings.Accounts_AvatarBlockUnauthenticatedAccess ?? state.settings.Accounts_AvatarBlockUnauthenticatedAccess ??
true true
}); });

View File

@ -16,12 +16,11 @@ export interface IAvatar {
id?: string; id?: string;
token?: string; token?: string;
}; };
theme?: string;
onPress?: () => void; onPress?: () => void;
getCustomEmoji?: TGetCustomEmoji; getCustomEmoji?: TGetCustomEmoji;
avatarETag?: string; avatarETag?: string;
isStatic?: boolean | string; isStatic?: boolean | string;
rid?: string; rid?: string;
blockUnauthenticatedAccess?: boolean; blockUnauthenticatedAccess?: boolean;
serverVersion: string; serverVersion: string | null;
} }

View File

@ -4,32 +4,30 @@ import Avatar from '../Avatar';
import styles from './styles'; import styles from './styles';
import MessageContext from './Context'; import MessageContext from './Context';
import { IMessageAvatar } from './interfaces'; import { IMessageAvatar } from './interfaces';
import { SubscriptionType } from '../../definitions';
const MessageAvatar = React.memo( const MessageAvatar = React.memo(({ isHeader, avatar, author, small, navToRoomInfo, emoji, getCustomEmoji }: IMessageAvatar) => {
({ isHeader, avatar, author, small, navToRoomInfo, emoji, getCustomEmoji, theme }: IMessageAvatar) => { const { user } = useContext(MessageContext);
const { user } = useContext(MessageContext); if (isHeader && author) {
if (isHeader && author) { const navParam = {
const navParam = { t: SubscriptionType.DIRECT,
t: 'd', rid: author._id
rid: author._id };
}; return (
return ( <Avatar
<Avatar style={small ? styles.avatarSmall : styles.avatar}
style={small ? styles.avatarSmall : styles.avatar} text={avatar ? '' : author.username}
text={avatar ? '' : author.username} size={small ? 20 : 36}
size={small ? 20 : 36} borderRadius={small ? 2 : 4}
borderRadius={small ? 2 : 4} onPress={author._id === user.id ? undefined : () => navToRoomInfo(navParam)}
onPress={author._id === user.id ? undefined : () => navToRoomInfo(navParam)} getCustomEmoji={getCustomEmoji}
getCustomEmoji={getCustomEmoji} avatar={avatar}
avatar={avatar} emoji={emoji}
emoji={emoji} />
theme={theme} );
/>
);
}
return null;
} }
); return null;
});
MessageAvatar.displayName = 'MessageAvatar'; MessageAvatar.displayName = 'MessageAvatar';

View File

@ -31,7 +31,6 @@ export interface IMessageAvatar {
small?: boolean; small?: boolean;
navToRoomInfo: Function; navToRoomInfo: Function;
getCustomEmoji: TGetCustomEmoji; getCustomEmoji: TGetCustomEmoji;
theme: string;
} }
export interface IMessageBlocks { export interface IMessageBlocks {

View File

@ -73,7 +73,7 @@ const Item = ({ item, onPress }: IItem): JSX.Element => {
testID={`discussions-view-${item.msg}`} testID={`discussions-view-${item.msg}`}
style={{ backgroundColor: themes[theme].backgroundColor }}> style={{ backgroundColor: themes[theme].backgroundColor }}>
<View style={styles.container}> <View style={styles.container}>
<Avatar style={styles.avatar} text={item?.u?.username} size={36} borderRadius={4} theme={theme} /> <Avatar style={styles.avatar} text={item?.u?.username} size={36} borderRadius={4} />
<View style={styles.contentContainer}> <View style={styles.contentContainer}>
<View style={styles.titleContainer}> <View style={styles.titleContainer}>
<Text style={[styles.title, { color: themes[theme].titleText }]} numberOfLines={1}> <Text style={[styles.title, { color: themes[theme].titleText }]} numberOfLines={1}>

View File

@ -65,7 +65,7 @@ interface IItem {
toggleFollowThread: (isFollowing: boolean, id: string) => void; toggleFollowThread: (isFollowing: boolean, id: string) => void;
} }
const Item = ({ item, useRealName, user, badgeColor, onPress, toggleFollowThread }: IItem) => { const Item = ({ item, useRealName, user, badgeColor, onPress, toggleFollowThread }: IItem): React.ReactElement => {
const { theme } = useTheme(); const { theme } = useTheme();
const username = (useRealName && item?.u?.name) || item?.u?.username; const username = (useRealName && item?.u?.name) || item?.u?.username;
let time; let time;
@ -79,7 +79,7 @@ const Item = ({ item, useRealName, user, badgeColor, onPress, toggleFollowThread
testID={`thread-messages-view-${item.msg}`} testID={`thread-messages-view-${item.msg}`}
style={{ backgroundColor: themes[theme].backgroundColor }}> style={{ backgroundColor: themes[theme].backgroundColor }}>
<View style={styles.container}> <View style={styles.container}>
<Avatar style={styles.avatar} text={item?.u?.username} size={36} borderRadius={4} theme={theme} /> <Avatar style={styles.avatar} text={item?.u?.username} size={36} borderRadius={4} />
<View style={styles.contentContainer}> <View style={styles.contentContainer}>
<View style={styles.titleContainer}> <View style={styles.titleContainer}>
<Text style={[styles.title, { color: themes[theme].titleText }]} numberOfLines={1}> <Text style={[styles.title, { color: themes[theme].titleText }]} numberOfLines={1}>