Skip to content

Commit c2ffedd

Browse files
authored
Merge pull request #12837 from nanaya/user-card-more-mobx
Move user card state to mobx
2 parents 7c117f7 + d58abeb commit c2ffedd

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed

resources/js/components/user-card.tsx

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import FriendButton from 'components/friend-button';
66
import Reportable from 'interfaces/reportable';
77
import UserJson from 'interfaces/user-json';
88
import { route } from 'laroute';
9-
import * as _ from 'lodash';
9+
import { action, makeObservable, observable } from 'mobx';
1010
import { observer } from 'mobx-react';
1111
import core from 'osu-core-singleton';
1212
import * as React from 'react';
@@ -67,17 +67,14 @@ export class UserCard extends React.PureComponent<Props, State> {
6767
username: trans('users.card.loading'),
6868
};
6969

70-
state: Readonly<State> = {
71-
avatarLoaded: false,
72-
backgroundLoaded: false,
73-
};
74-
70+
@observable private avatarLoaded = false;
71+
@observable private backgroundLoaded = false;
7572
private readonly popupMenuState = new PopupMenuState();
7673
private url?: string;
7774

7875
private get canMessage() {
7976
return !this.isSelf
80-
&& _.find(core.currentUser?.blocks ?? [], { target_id: this.user.id }) == null;
77+
&& !core.currentUserModel.blocks.has(this.user.id);
8178
}
8279

8380
private get isOnline() {
@@ -104,13 +101,10 @@ export class UserCard extends React.PureComponent<Props, State> {
104101
return this.props.user || UserCard.userLoading;
105102
}
106103

107-
onAvatarLoad = () => {
108-
this.setState({ avatarLoaded: true });
109-
};
110-
111-
onBackgroundLoad = () => {
112-
this.setState({ backgroundLoaded: true });
113-
};
104+
constructor(props: Props) {
105+
super(props);
106+
makeObservable(this);
107+
}
114108

115109
render() {
116110
if (this.props.mode === 'brick') {
@@ -156,7 +150,7 @@ export class UserCard extends React.PureComponent<Props, State> {
156150
}
157151

158152
renderAvatar() {
159-
const modifiers = { loaded: this.state.avatarLoaded };
153+
const modifiers = { loaded: this.avatarLoaded };
160154
const hasAvatar = present(this.user.avatar_url) && !this.isUserNotFound;
161155

162156
return (
@@ -177,44 +171,28 @@ export class UserCard extends React.PureComponent<Props, State> {
177171
}
178172

179173
renderBackground() {
180-
let background: React.ReactNode;
181-
let backgroundLink: React.ReactNode;
182-
183-
const overlayCssClass = classWithModifiers(
174+
const overlay = (<div className={classWithModifiers(
184175
'user-card__background-overlay',
185-
this.isOnline ? ['online'] : [],
186-
);
187-
188-
if (this.user.cover?.url != null) {
189-
let backgroundCssClass = 'user-card__background';
190-
if (!this.state.backgroundLoaded) {
191-
backgroundCssClass += ' user-card__background--loading';
192-
}
193-
194-
background = (
195-
<>
196-
<img className={backgroundCssClass} onLoad={this.onBackgroundLoad} src={this.user.cover.url} />
197-
<div className={overlayCssClass} />
198-
</>
199-
);
200-
} else {
201-
background = <div className={overlayCssClass} />;
202-
}
203-
204-
if (this.isUserVisible) {
205-
backgroundLink = (
206-
<a
207-
className='user-card__background-container'
208-
href={this.url}
209-
>
176+
{ online: this.isOnline },
177+
)} />);
178+
179+
const background = this.user.cover?.url == null
180+
? overlay
181+
: (<>
182+
<img
183+
className={classWithModifiers('user-card__background', { loading: !this.backgroundLoaded })}
184+
onLoad={this.onBackgroundLoad}
185+
src={this.user.cover.url}
186+
/>
187+
{overlay}
188+
</>);
189+
190+
return this.isUserVisible
191+
? (
192+
<a className='user-card__background-container' href={this.url}>
210193
{background}
211194
</a>
212-
);
213-
} else {
214-
backgroundLink = background;
215-
}
216-
217-
return backgroundLink;
195+
) : background;
218196
}
219197

220198
renderIcons() {
@@ -345,6 +323,16 @@ export class UserCard extends React.PureComponent<Props, State> {
345323
);
346324
}
347325

326+
@action
327+
private readonly onAvatarLoad = () => {
328+
this.avatarLoaded = true;
329+
};
330+
331+
@action
332+
private readonly onBackgroundLoad = () => {
333+
this.backgroundLoaded = true;
334+
};
335+
348336
private readonly renderMenuItems = () => (
349337
<div className='simple-menu'>
350338
{this.canMessage && (

0 commit comments

Comments
 (0)