From 0d9f7f35f086a0d63891a8751a3fdc8af48d1592 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 15 Jul 2020 18:47:33 +0200 Subject: [PATCH] Add message URLs, unify URL generation --- components/buffer-list.js | 17 ++--------------- components/buffer.js | 13 ++++++------- components/member-list.js | 4 ++-- state.js | 21 +++++++++++++++++++++ 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/components/buffer-list.js b/components/buffer-list.js index 64524f8..a51f352 100644 --- a/components/buffer-list.js +++ b/components/buffer-list.js @@ -1,5 +1,5 @@ import { html, Component } from "/lib/index.js"; -import { BufferType, Unread } from "/state.js"; +import { BufferType, Unread, getBufferURL } from "/state.js"; function BufferItem(props) { function handleClick(event) { @@ -19,22 +19,9 @@ function BufferItem(props) { unreadClass = "unread-" + props.buffer.unread; } - var url = "#"; - switch (props.buffer.type) { - case BufferType.SERVER: - url = "irc:///"; - break; - case BufferType.CHANNEL: - url = "irc:///" + encodeURIComponent(props.buffer.name); - break; - case BufferType.NICK: - url = "irc:///" + encodeURIComponent(props.buffer.name) + ",isnick"; - break; - } - return html`
  • - ${name} + ${name}
  • `; } diff --git a/components/buffer.js b/components/buffer.js index 33fda5f..5bfb564 100644 --- a/components/buffer.js +++ b/components/buffer.js @@ -1,7 +1,7 @@ import { html, Component } from "/lib/index.js"; import linkify from "/lib/linkify.js"; import * as irc from "/lib/irc.js"; -import { BufferType } from "/state.js"; +import { BufferType, getNickURL, getMessageURL } from "/state.js"; function djb2(s) { var hash = 5381; @@ -19,13 +19,12 @@ function Nick(props) { } var colorIndex = djb2(props.nick) % 16 + 1; - var url = "irc:///" + encodeURIComponent(props.nick) + ",isnick"; return html` - ${props.nick} + ${props.nick} `; } -function Timestamp({ date }) { +function Timestamp({ date, url }) { if (!date) { return html`--:--:--`; } @@ -35,7 +34,7 @@ function Timestamp({ date }) { var ss = date.getSeconds().toString().padStart(2, "0"); var timestamp = `${hh}:${mm}:${ss}`; return html` - event.preventDefault()}>${timestamp} + event.preventDefault()}>${timestamp} `; } @@ -107,7 +106,7 @@ class LogLine extends Component { return html`
    - <${Timestamp} date=${new Date(msg.tags.time)}/> + <${Timestamp} date=${new Date(msg.tags.time)} url=${getMessageURL(this.props.buffer, msg)}/> ${" "} ${content}
    @@ -172,7 +171,7 @@ export default class Buffer extends Component {
    ${notifNagger} ${this.props.buffer.messages.map((msg) => html` - <${LogLine} key=${msg.key} message=${msg} onNickClick=${this.props.onNickClick}/> + <${LogLine} key=${msg.key} message=${msg} buffer=${this.props.buffer} onNickClick=${this.props.onNickClick}/> `)}
    `; diff --git a/components/member-list.js b/components/member-list.js index f1d67b7..48bdcdf 100644 --- a/components/member-list.js +++ b/components/member-list.js @@ -1,4 +1,5 @@ import { html, Component } from "/lib/index.js"; +import { getNickURL } from "/state.js"; class MemberItem extends Component { constructor(props) { @@ -17,10 +18,9 @@ class MemberItem extends Component { } render() { - var url = "irc:///" + encodeURIComponent(this.props.nick) + ",isnick"; return html`
  • - ${this.props.nick} + ${this.props.nick}
  • `; } diff --git a/state.js b/state.js index b6dcce4..5a60b4d 100644 --- a/state.js +++ b/state.js @@ -26,3 +26,24 @@ export const Unread = { return (priority[a] > priority[b]) ? a : b; }, }; + +export function getNickURL(nick) { + return "irc:///" + encodeURIComponent(nick) + ",isnick"; +} + +export function getBufferURL(buf) { + switch (buf.type) { + case BufferType.SERVER: + return "irc:///"; + case BufferType.CHANNEL: + return "irc:///" + encodeURIComponent(buf.name); + case BufferType.NICK: + return getNickURL(buf.name); + } + throw new Error("Unknown buffer type: " + buf.type); +} + +export function getMessageURL(buf, msg) { + var bufURL = getBufferURL(buf); + return bufURL + "#timestamp=" + encodeURIComponent(msg.tags.time); +}