gamja-old/components/buffer-list.js

85 lines
2 KiB
JavaScript
Raw Normal View History

import * as irc from "../lib/irc.js";
import { strip as stripANSI } from "../lib/ansi.js";
import { html, Component } from "../lib/index.js";
import { BufferType, Unread, ServerStatus, getBufferURL, getServerName } from "../state.js";
2021-01-22 05:43:47 -05:00
function BufferItem(props) {
function handleClick(event) {
event.preventDefault();
props.onClick();
}
2021-10-17 13:33:02 -04:00
function handleMouseDown(event) {
if (event.button === 1) { // middle click
event.preventDefault();
props.onClose();
}
}
2021-06-10 12:11:11 -04:00
let name = props.buffer.name;
2020-06-26 04:35:38 -04:00
if (props.buffer.type == BufferType.SERVER) {
name = getServerName(props.server, props.bouncerNetwork);
}
let title;
2021-06-10 12:11:11 -04:00
let classes = ["type-" + props.buffer.type];
if (props.active) {
classes.push("active");
}
2020-06-24 10:56:28 -04:00
if (props.buffer.unread != Unread.NONE) {
classes.push("unread-" + props.buffer.unread);
2020-06-24 10:56:28 -04:00
}
switch (props.buffer.type) {
case BufferType.SERVER:
let isError = props.server.status === ServerStatus.DISCONNECTED;
if (props.bouncerNetwork && props.bouncerNetwork.error) {
isError = true;
}
if (isError) {
classes.push("error");
}
break;
case BufferType.NICK:
let user = props.server.users.get(name);
if (user && irc.isMeaningfulRealname(user.realname, name)) {
title = stripANSI(user.realname);
}
break;
}
2020-06-24 10:56:28 -04:00
return html`
<li class="${classes.join(" ")}">
2021-10-17 13:33:02 -04:00
<a
href=${getBufferURL(props.buffer)}
title=${title}
2021-10-17 13:33:02 -04:00
onClick=${handleClick}
onMouseDown=${handleMouseDown}
>${name}</a>
</li>
`;
}
export default function BufferList(props) {
2021-06-10 12:11:11 -04:00
let items = Array.from(props.buffers.values()).map((buf) => {
let server = props.servers.get(buf.server);
2021-06-10 12:11:11 -04:00
let bouncerNetwork = null;
if (server.bouncerNetID) {
bouncerNetwork = props.bouncerNetworks.get(server.bouncerNetID);
}
return html`
2021-05-31 11:34:36 -04:00
<${BufferItem}
key=${buf.id}
buffer=${buf}
server=${server}
2021-05-31 11:34:36 -04:00
bouncerNetwork=${bouncerNetwork}
onClick=${() => props.onBufferClick(buf)}
2021-10-17 13:33:02 -04:00
onClose=${() => props.onBufferClose(buf)}
2021-05-31 11:34:36 -04:00
active=${props.activeBuffer == buf.id}
/>
`;
});
return html`<ul>${items}</ul>`;
}