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

View File

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

View File

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

View File

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

View File

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

View File

@ -73,7 +73,7 @@ const Item = ({ item, onPress }: IItem): JSX.Element => {
testID={`discussions-view-${item.msg}`}
style={{ backgroundColor: themes[theme].backgroundColor }}>
<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.titleContainer}>
<Text style={[styles.title, { color: themes[theme].titleText }]} numberOfLines={1}>

View File

@ -65,7 +65,7 @@ interface IItem {
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 username = (useRealName && item?.u?.name) || item?.u?.username;
let time;
@ -79,7 +79,7 @@ const Item = ({ item, useRealName, user, badgeColor, onPress, toggleFollowThread
testID={`thread-messages-view-${item.msg}`}
style={{ backgroundColor: themes[theme].backgroundColor }}>
<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.titleContainer}>
<Text style={[styles.title, { color: themes[theme].titleText }]} numberOfLines={1}>