gamja/components/buffer.js

105 lines
2.3 KiB
JavaScript
Raw Normal View History

import { html, Component } from "/lib/index.js";
2020-06-25 11:26:40 -04:00
import linkify from "/lib/linkify.js";
function djb2(s) {
var hash = 5381;
for (var i = 0; i < s.length; i++) {
hash = (hash << 5) + hash + s.charCodeAt(i);
hash = hash >>> 0; // convert to uint32
}
return hash;
}
function Nick(props) {
function handleClick(event) {
event.preventDefault();
2020-06-25 12:45:41 -04:00
props.onClick();
}
var colorIndex = djb2(props.nick) % 16 + 1;
return html`
<a href="#" class="nick nick-${colorIndex}" onClick=${handleClick}>${props.nick}</a>
`;
}
function LogLine(props) {
var msg = props.message;
2020-06-25 12:45:41 -04:00
function createNick(nick) {
return html`
<${Nick} nick=${nick} onClick=${() => props.onNickClick(nick)}/>
`;
}
2020-06-24 11:16:49 -04:00
var date = new Date(msg.tags["time"]);
var timestamp = date.toLocaleTimeString(undefined, {
timeStyle: "short",
hour12: false,
});
var timestampLink = html`
<a href="#" class="timestamp" onClick=${(event) => event.preventDefault()}>${timestamp}</a>
`;
var lineClass = "";
var content;
switch (msg.command) {
case "NOTICE":
case "PRIVMSG":
var text = msg.params[1];
var actionPrefix = "\x01ACTION ";
if (text.startsWith(actionPrefix) && text.endsWith("\x01")) {
var action = text.slice(actionPrefix.length, -1);
lineClass = "me-tell";
2020-06-25 12:45:41 -04:00
content = html`* ${createNick(msg.prefix.name)} ${linkify(action)}`;
} else {
lineClass = "talk";
2020-06-25 12:45:41 -04:00
content = html`${"<"}${createNick(msg.prefix.name)}${">"} ${linkify(text)}`;
}
break;
case "JOIN":
content = html`
2020-06-25 12:45:41 -04:00
${createNick(msg.prefix.name)} has joined
`;
break;
case "PART":
content = html`
2020-06-25 12:45:41 -04:00
${createNick(msg.prefix.name)} has left
`;
break;
case "NICK":
var newNick = msg.params[0];
content = html`
2020-06-25 15:28:04 -04:00
${createNick(msg.prefix.name)} is now known as ${createNick(newNick)}
`;
break;
case "TOPIC":
var topic = msg.params[1];
content = html`
2020-06-25 12:45:41 -04:00
${createNick(msg.prefix.name)} changed the topic to: ${linkify(topic)}
`;
break;
default:
content = html`${msg.command} ${msg.params.join(" ")}`;
}
return html`
<div class="logline ${lineClass}">${timestampLink} ${content}</div>
`;
}
export default function Buffer(props) {
if (!props.buffer) {
return null;
}
2020-06-25 08:26:33 -04:00
return html`
<div class="logline-list">
${props.buffer.messages.map((msg) => html`
2020-06-25 12:45:41 -04:00
<${LogLine} message=${msg} onNickClick=${props.onNickClick}/>
2020-06-25 08:26:33 -04:00
`)}
</div>
`;
}