Skip to content

Commit a4cbe35

Browse files
committed
[2.x] chore: make PostUser more extensible (#4252)
* chore: make PostUser more extensible * fix: userOnline can be null
1 parent 24d2c50 commit a4cbe35

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

framework/core/js/src/forum/components/PostUser.tsx

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Avatar from '../../common/components/Avatar';
88
import type Model from '../../common/Model';
99
import type Post from '../../common/models/Post';
1010
import type User from '../../common/models/User';
11+
import ItemList from '../../common/utils/ItemList';
12+
import type Mithril from 'mithril';
1113

1214
export interface IPostUserAttrs extends ComponentAttrs {
1315
/** Can be a post or similar model like private message */
@@ -22,27 +24,53 @@ export default class PostUser<CustomAttrs extends IPostUserAttrs = IPostUserAttr
2224
const post = this.attrs.post;
2325
const user = post.user();
2426

25-
if (!user) {
26-
return (
27-
<div className="PostUser">
28-
<h3 className="PostUser-name">
29-
<Avatar user={user} className="Post-avatar" /> {username(user)}
30-
</h3>
31-
</div>
32-
);
33-
}
27+
const items = user ? this.userViewItems(user, post) : this.noUserViewItems(user, post);
28+
29+
return <div className="PostUser">{items.toArray()}</div>;
30+
}
31+
32+
noUserViewItems(user: false | User | null, post: Post | (Model & { user: () => false | User | null })): ItemList<Mithril.Children> {
33+
const items = new ItemList<Mithril.Children>();
34+
35+
items.add(
36+
'postUser-name',
37+
<h3 className="PostUser-name">
38+
<Avatar user={user} className="Post-avatar" /> {username(user)}
39+
</h3>,
40+
100
41+
);
42+
43+
return items;
44+
}
45+
46+
userViewItems(user: User, post: Post | (Model & { user: () => false | User | null })): ItemList<Mithril.Children> {
47+
const items = new ItemList<Mithril.Children>();
3448

35-
return (
36-
<div className="PostUser">
37-
<h3 className="PostUser-name">
38-
<Link href={app.route.user(user)}>
39-
<Avatar user={user} className="Post-avatar" />
40-
{userOnline(user)}
41-
{username(user)}
42-
</Link>
43-
</h3>
44-
<ul className="PostUser-badges badges badges--packed">{listItems(user.badges().toArray())}</ul>
45-
</div>
49+
items.add(
50+
'postUser-name',
51+
<h3 className="PostUser-name">
52+
<Link href={app.route.user(user)}>{this.linkChildren(user).toArray()}</Link>
53+
</h3>,
54+
100
4655
);
56+
57+
items.add('postUser-badges', <ul className="PostUser-badges badges badges--packed">{listItems(user.badges().toArray())}</ul>, 90);
58+
59+
return items;
60+
}
61+
62+
linkChildren(user: User): ItemList<Mithril.Children> {
63+
const items = new ItemList<Mithril.Children>();
64+
65+
items.add('avatar', <Avatar user={user} className="Post-avatar" />, 100);
66+
67+
const onlineIndicator = userOnline(user);
68+
if (onlineIndicator !== null) {
69+
items.add('userOnline', onlineIndicator, 90);
70+
}
71+
72+
items.add('username', username(user), 80);
73+
74+
return items;
4775
}
4876
}

0 commit comments

Comments
 (0)