gamja-old/components/member-list.js

129 lines
2.8 KiB
JavaScript
Raw Normal View History

import { html, Component } from "../lib/index.js";
import { getNickURL } from "../state.js";
2021-09-21 08:29:31 -04:00
import { strip as stripANSI } from "../lib/ansi.js";
import Membership from "./membership.js";
2021-09-21 11:24:39 -04:00
import * as irc from "../lib/irc.js";
2020-06-26 08:32:56 -04:00
class MemberItem extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
shouldComponentUpdate(nextProps) {
return this.props.nick !== nextProps.nick
|| this.props.membership !== nextProps.membership
|| this.props.user !== nextProps.user;
}
handleClick(event) {
2020-06-26 08:32:56 -04:00
event.preventDefault();
this.props.onClick();
2020-06-26 08:32:56 -04:00
}
render() {
// XXX: If we were feeling creative we could generate unique colors for
// each item in ISUPPORT CHANMODES. But I am not feeling creative.
const membmap = {
"~": "owner",
"&": "admin",
"@": "op",
"%": "halfop",
"+": "voice",
};
const membclass = membmap[this.props.membership[0]] || "";
let membership = "";
if (this.props.membership) {
membership = html`
<span class="membership ${membclass}" title=${membclass}>
${this.props.membership}
</span>
`;
};
let title = null;
let user = this.props.user;
let classes = ["nick"];
2021-09-21 08:29:31 -04:00
if (user) {
let mask = "";
if (user.username && user.hostname) {
mask = `${user.username}@${user.hostname}`;
}
2021-09-21 11:24:39 -04:00
if (irc.isMeaningfulRealname(user.realname, this.props.nick)) {
2021-09-21 08:29:31 -04:00
title = stripANSI(user.realname);
if (mask) {
title = `${title} (${mask})`;
}
} else {
title = mask;
}
2021-09-21 09:38:59 -04:00
if (user.account) {
title += `\nAuthenticated as ${user.account}`;
}
if (user.away) {
classes.push("away");
title += "\nAway";
}
}
return html`
<li>
<a
href=${getNickURL(this.props.nick)}
class=${classes.join(" ")}
title=${title}
onClick=${this.handleClick}
>
<${Membership} value=${this.props.membership}/>
${this.props.nick}
</a>
</li>
`;
}
2020-06-26 08:32:56 -04:00
}
function sortMembers(a, b) {
2021-06-10 12:11:11 -04:00
let [nickA, membA] = a, [nickB, membB] = b;
const prefixPrivs = ["~", "&", "@", "%", "+"]; // TODO: grab it from ISUPPORT PREFIX
2021-06-10 12:11:11 -04:00
let i = prefixPrivs.indexOf(membA[0]), j = prefixPrivs.indexOf(membB[0]);
if (i < 0) {
i = prefixPrivs.length;
}
if (j < 0) {
j = prefixPrivs.length;
}
if (i !== j) {
return i - j;
}
return nickA.localeCompare(nickB);
}
export default class MemberList extends Component {
shouldComponentUpdate(nextProps) {
return this.props.members !== nextProps.members
|| this.props.users !== nextProps.users;
}
render() {
return html`
<ul>
${Array.from(this.props.members).sort(sortMembers).map(([nick, membership]) => html`
<${MemberItem}
key=${nick}
nick=${nick}
membership=${membership}
user=${this.props.users.get(nick)}
onClick=${() => this.props.onNickClick(nick)}
/>
`)}
</ul>
`;
}
2020-06-26 08:32:56 -04:00
}