gamja/components/buffer-header.js

178 lines
4.3 KiB
JavaScript
Raw Normal View History

import { html, Component } from "../lib/index.js";
import linkify from "../lib/linkify.js";
import { strip as stripANSI } from "../lib/ansi.js";
import { BufferType, ServerStatus, getServerName } from "../state.js";
2021-09-21 11:24:39 -04:00
import * as irc from "../lib/irc.js";
2020-06-25 12:30:21 -04:00
2021-01-22 05:53:17 -05:00
const UserStatus = {
HERE: "here",
GONE: "gone",
OFFLINE: "offline",
};
function NickStatus(props) {
2021-06-10 12:11:11 -04:00
let textMap = {
2021-01-22 05:53:17 -05:00
[UserStatus.HERE]: "User is online",
[UserStatus.GONE]: "User is away",
[UserStatus.OFFLINE]: "User is offline",
};
2021-06-10 12:11:11 -04:00
let text = textMap[props.status];
return html`<span class="status status-${props.status}" title=${text}>●</span>`;
}
2020-06-25 12:30:21 -04:00
export default function BufferHeader(props) {
2021-03-09 13:10:22 -05:00
function handleCloseClick(event) {
2020-06-25 12:30:21 -04:00
event.preventDefault();
props.onClose();
}
2021-03-02 10:13:49 -05:00
function handleJoinClick(event) {
event.preventDefault();
props.onJoin();
}
2021-03-08 12:15:04 -05:00
function handleAddNetworkClick(event) {
event.preventDefault();
props.onAddNetwork();
}
2021-03-09 13:10:22 -05:00
function handleManageNetworkClick(event) {
event.preventDefault();
props.onManageNetwork();
}
2020-06-25 12:30:21 -04:00
2021-06-10 12:11:11 -04:00
let description = null, actions = null;
switch (props.buffer.type) {
case BufferType.SERVER:
switch (props.server.status) {
case ServerStatus.DISCONNECTED:
2021-01-22 05:53:17 -05:00
description = "Disconnected";
break;
case ServerStatus.CONNECTING:
2021-01-22 05:53:17 -05:00
description = "Connecting...";
break;
case ServerStatus.REGISTERING:
2021-01-22 12:29:22 -05:00
description = "Logging in...";
break;
case ServerStatus.REGISTERED:
2021-03-10 05:48:58 -05:00
if (props.bouncerNetwork) {
switch (props.bouncerNetwork.state) {
case "disconnected":
description = "Bouncer disconnected from network";
break;
case "connecting":
description = "Bouncer connecting to network...";
break;
case "connected":
// host can be undefined e.g. when using UNIX domain sockets
description = `Connected to ${props.bouncerNetwork.host || "network"}`;
2021-03-10 05:48:58 -05:00
break;
}
} else if (props.buffer.serverInfo) {
2021-06-10 12:11:11 -04:00
let serverInfo = props.buffer.serverInfo;
2021-03-10 05:48:58 -05:00
description = `Connected to ${serverInfo.name}`;
} else {
description = "Connected";
2021-03-10 05:48:58 -05:00
}
2021-01-22 05:53:17 -05:00
break;
}
2020-06-26 06:45:27 -04:00
2021-03-09 13:10:22 -05:00
if (props.isBouncer) {
if (props.server.isupport.get("BOUNCER_NETID")) {
2021-03-09 13:10:22 -05:00
actions = html`
2021-05-27 13:45:21 -04:00
<button
key="join"
onClick=${handleJoinClick}
>Join channel</button>
<button
key="manage"
onClick=${handleManageNetworkClick}
>Manage network</button>
2021-03-09 13:10:22 -05:00
`;
} else {
actions = html`
2021-05-27 13:45:21 -04:00
<button
key="add"
onClick=${handleAddNetworkClick}
>Add network</button>
<button
key="disconnect"
class="danger"
onClick=${handleCloseClick}
>Disconnect</button>
2021-03-09 13:10:22 -05:00
`;
}
2021-03-08 12:15:04 -05:00
} else {
2021-03-09 13:10:22 -05:00
actions = html`
2021-05-27 13:45:21 -04:00
<button
key="join"
onClick=${handleJoinClick}
>Join channel</button>
<button
key="disconnect"
class="danger"
onClick=${handleCloseClick}
>Disconnect</button>
2021-03-09 13:10:22 -05:00
`;
2021-03-08 12:15:04 -05:00
}
break;
case BufferType.CHANNEL:
if (props.buffer.topic) {
2021-05-31 22:39:35 -04:00
description = linkify(stripANSI(props.buffer.topic), props.onChannelClick);
}
actions = html`
<button
key="part"
class="danger"
onClick=${handleCloseClick}
>Leave</button>
`;
2021-03-09 13:10:22 -05:00
break;
case BufferType.NICK:
if (props.user) {
2021-06-10 12:11:11 -04:00
let status = UserStatus.HERE;
if (props.user.offline) {
status = UserStatus.OFFLINE;
} else if (props.user.away) {
status = UserStatus.GONE;
}
let realname = props.buffer.name;
2021-09-21 11:24:39 -04:00
if (irc.isMeaningfulRealname(props.user.realname, props.buffer.name)) {
realname = stripANSI(props.user.realname || "");
}
2021-09-21 09:38:59 -04:00
let details = [];
if (props.user.username && props.user.hostname) {
2021-09-21 09:38:59 -04:00
details.push(`${props.user.username}@${props.user.hostname}`);
}
2021-09-21 09:38:59 -04:00
if (props.user.account) {
details.push(`authenticated as ${props.user.account}`);
}
if (props.user.operator) {
details.push("server operator");
}
2021-09-21 09:38:59 -04:00
details = details.length > 0 ? `(${details.join(", ")})` : null;
2021-09-21 09:38:59 -04:00
description = html`<${NickStatus} status=${status}/> ${realname} ${details}`;
}
actions = html`
<button
key="close"
class="danger"
onClick=${handleCloseClick}
>Close</button>
`;
break;
}
2021-06-10 12:11:11 -04:00
let name = props.buffer.name;
if (props.buffer.type == BufferType.SERVER) {
name = getServerName(props.server, props.bouncerNetwork, props.isBouncer);
}
2020-06-25 12:30:21 -04:00
return html`
<div class="title">${name}</div>
${description ? html`<div class="description">${description}</div>` : null}
<div class="actions">${actions}</div>
2020-06-25 12:30:21 -04:00
`;
}