forked from CringeStudios/gamja
Mark buffers as unread on new message
This commit is contained in:
parent
9ff1f164e0
commit
cbe76ab824
@ -5,12 +5,7 @@ import BufferList from "/components/buffer-list.js";
|
|||||||
import Connect from "/components/connect.js";
|
import Connect from "/components/connect.js";
|
||||||
import Composer from "/components/composer.js";
|
import Composer from "/components/composer.js";
|
||||||
import { html, Component, createRef } from "/lib/index.js";
|
import { html, Component, createRef } from "/lib/index.js";
|
||||||
|
import { SERVER_BUFFER, Status, Unread } from "/state.js";
|
||||||
const SERVER_BUFFER = "*";
|
|
||||||
|
|
||||||
const DISCONNECTED = "disconnected";
|
|
||||||
const CONNECTING = "connecting";
|
|
||||||
const REGISTERED = "registered";
|
|
||||||
|
|
||||||
function parseQueryString() {
|
function parseQueryString() {
|
||||||
var query = window.location.search.substring(1);
|
var query = window.location.search.substring(1);
|
||||||
@ -37,7 +32,7 @@ export default class App extends Component {
|
|||||||
saslPlain: null,
|
saslPlain: null,
|
||||||
autojoin: [],
|
autojoin: [],
|
||||||
},
|
},
|
||||||
status: DISCONNECTED,
|
status: Status.DISCONNECTED,
|
||||||
buffers: new Map(),
|
buffers: new Map(),
|
||||||
activeBuffer: null,
|
activeBuffer: null,
|
||||||
};
|
};
|
||||||
@ -58,7 +53,12 @@ export default class App extends Component {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var updated = updater(buf);
|
var updated;
|
||||||
|
if (typeof updater === "function") {
|
||||||
|
updated = updater(buf, state);
|
||||||
|
} else {
|
||||||
|
updated = updater;
|
||||||
|
}
|
||||||
if (buf === updated || !updated) {
|
if (buf === updated || !updated) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -82,12 +82,14 @@ export default class App extends Component {
|
|||||||
topic: null,
|
topic: null,
|
||||||
members: new Map(),
|
members: new Map(),
|
||||||
messages: [],
|
messages: [],
|
||||||
|
unread: Unread.NONE,
|
||||||
});
|
});
|
||||||
return { buffers };
|
return { buffers };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
switchBuffer(name) {
|
switchBuffer(name) {
|
||||||
|
this.setBufferState(name, { unread: Unread.NONE });
|
||||||
this.setState({ activeBuffer: name }, () => {
|
this.setState({ activeBuffer: name }, () => {
|
||||||
if (this.composer.current) {
|
if (this.composer.current) {
|
||||||
this.composer.current.focus();
|
this.composer.current.focus();
|
||||||
@ -101,14 +103,26 @@ export default class App extends Component {
|
|||||||
}
|
}
|
||||||
// TODO: set time tag if missing
|
// TODO: set time tag if missing
|
||||||
|
|
||||||
|
var unread = Unread.NONE;
|
||||||
|
if (msg.command == "PRIVMSG" || msg.command == "NOTICE") {
|
||||||
|
unread = Unread.MESSAGE;
|
||||||
|
}
|
||||||
|
|
||||||
this.createBuffer(bufName);
|
this.createBuffer(bufName);
|
||||||
this.setBufferState(bufName, (buf) => {
|
this.setBufferState(bufName, (buf, state) => {
|
||||||
return { messages: buf.messages.concat(msg) };
|
var unread = buf.unread;
|
||||||
|
if (state.activeBuffer != buf.name) {
|
||||||
|
unread = Unread.union(buf.unread, unread);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
messages: buf.messages.concat(msg),
|
||||||
|
unread,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(params) {
|
connect(params) {
|
||||||
this.setState({ status: CONNECTING, connectParams: params });
|
this.setState({ status: Status.CONNECTING, connectParams: params });
|
||||||
|
|
||||||
this.client = new Client({
|
this.client = new Client({
|
||||||
url: params.serverURL,
|
url: params.serverURL,
|
||||||
@ -121,7 +135,7 @@ export default class App extends Component {
|
|||||||
|
|
||||||
this.client.addEventListener("close", () => {
|
this.client.addEventListener("close", () => {
|
||||||
this.setState({
|
this.setState({
|
||||||
status: DISCONNECTED,
|
status: Status.DISCONNECTED,
|
||||||
buffers: new Map(),
|
buffers: new Map(),
|
||||||
activeBuffer: null,
|
activeBuffer: null,
|
||||||
});
|
});
|
||||||
@ -138,7 +152,7 @@ export default class App extends Component {
|
|||||||
handleMessage(msg) {
|
handleMessage(msg) {
|
||||||
switch (msg.command) {
|
switch (msg.command) {
|
||||||
case irc.RPL_WELCOME:
|
case irc.RPL_WELCOME:
|
||||||
this.setState({ status: REGISTERED });
|
this.setState({ status: Status.REGISTERED });
|
||||||
|
|
||||||
if (this.state.connectParams.autojoin.length > 0) {
|
if (this.state.connectParams.autojoin.length > 0) {
|
||||||
this.client.send({
|
this.client.send({
|
||||||
@ -151,9 +165,7 @@ export default class App extends Component {
|
|||||||
var channel = msg.params[1];
|
var channel = msg.params[1];
|
||||||
var topic = msg.params[2];
|
var topic = msg.params[2];
|
||||||
|
|
||||||
this.setBufferState(channel, (buf) => {
|
this.setBufferState(channel, { topic });
|
||||||
return { topic };
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
case irc.RPL_NAMREPLY:
|
case irc.RPL_NAMREPLY:
|
||||||
var channel = msg.params[2];
|
var channel = msg.params[2];
|
||||||
@ -230,9 +242,7 @@ export default class App extends Component {
|
|||||||
var channel = msg.params[0];
|
var channel = msg.params[0];
|
||||||
var topic = msg.params[1];
|
var topic = msg.params[1];
|
||||||
|
|
||||||
this.setBufferState(channel, (buf) => {
|
this.setBufferState(channel, { topic });
|
||||||
return { topic };
|
|
||||||
});
|
|
||||||
this.addMessage(channel, msg);
|
this.addMessage(channel, msg);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -377,10 +387,10 @@ export default class App extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (this.state.status != REGISTERED) {
|
if (this.state.status != Status.REGISTERED) {
|
||||||
return html`
|
return html`
|
||||||
<section id="connect">
|
<section id="connect">
|
||||||
<${Connect} params=${this.state.connectParams} disabled=${this.state.status != DISCONNECTED} onSubmit=${this.handleConnectSubmit}/>
|
<${Connect} params=${this.state.connectParams} disabled=${this.state.status != Status.DISCONNECTED} onSubmit=${this.handleConnectSubmit}/>
|
||||||
</section>
|
</section>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { html, Component } from "/lib/index.js";
|
import { html, Component } from "/lib/index.js";
|
||||||
|
import { SERVER_BUFFER, Unread } from "/state.js";
|
||||||
|
|
||||||
function BufferItem(props) {
|
function BufferItem(props) {
|
||||||
function handleClick(event) {
|
function handleClick(event) {
|
||||||
@ -7,12 +8,19 @@ function BufferItem(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var name = props.buffer.name;
|
var name = props.buffer.name;
|
||||||
if (name == "*") {
|
if (name == SERVER_BUFFER) {
|
||||||
name = "server";
|
name = "server";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var activeClass = props.active ? "active" : "";
|
||||||
|
|
||||||
|
var unreadClass = "";
|
||||||
|
if (props.buffer.unread != Unread.NONE) {
|
||||||
|
unreadClass = "unread-" + props.buffer.unread;
|
||||||
|
}
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<li class=${props.active ? "active" : ""}>
|
<li class="${activeClass} ${unreadClass}">
|
||||||
<a href="#" onClick=${handleClick}>${name}</a>
|
<a href="#" onClick=${handleClick}>${name}</a>
|
||||||
</li>
|
</li>
|
||||||
`;
|
`;
|
||||||
|
20
state.js
Normal file
20
state.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
export const SERVER_BUFFER = "*";
|
||||||
|
|
||||||
|
export const Status = {
|
||||||
|
DISCONNECTED: "disconnected",
|
||||||
|
CONNECTING: "connecting",
|
||||||
|
REGISTERED: "registered",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Unread = {
|
||||||
|
NONE: "",
|
||||||
|
MESSAGE: "message",
|
||||||
|
|
||||||
|
union: (a, b) => {
|
||||||
|
const priority = {
|
||||||
|
[Unread.None]: 0,
|
||||||
|
[Unread.MESSAGE]: 1,
|
||||||
|
};
|
||||||
|
return (priority[a] > priority[b]) ? a : b;
|
||||||
|
},
|
||||||
|
};
|
@ -79,6 +79,9 @@ a {
|
|||||||
#sidebar .active a {
|
#sidebar .active a {
|
||||||
color: black;
|
color: black;
|
||||||
}
|
}
|
||||||
|
#sidebar .unread-message a {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
#sidebar a:hover, #sidebar a:active,
|
#sidebar a:hover, #sidebar a:active,
|
||||||
a.timestamp:hover, a.timestamp:active,
|
a.timestamp:hover, a.timestamp:active,
|
||||||
a.nick:hover, a.nick:active {
|
a.nick:hover, a.nick:active {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user