Take a buffer ID in switchBuffer

This commit is contained in:
Simon Ser 2021-01-21 22:15:33 +01:00
parent dd35f1df95
commit ffed801b0e
3 changed files with 39 additions and 27 deletions

View file

@ -72,10 +72,13 @@ export default {
}, },
"buffer": (app, args) => { "buffer": (app, args) => {
var name = args[0]; var name = args[0];
if (!app.state.buffers.has(name)) { for (var buf of app.state.buffers.values()) {
throw new Error("Unknown buffer"); if (buf.name === name) {
app.switchBuffer(buf);
return;
} }
app.switchBuffer(name); }
throw new Error("Unknown buffer");
}, },
"topic": (app, args) => { "topic": (app, args) => {
var channel = app.state.activeBuffer; var channel = app.state.activeBuffer;

View file

@ -104,6 +104,10 @@ function getBuffer(state, id) {
case "number": case "number":
return state.buffers.get(id); return state.buffers.get(id);
case "object": case "object":
if (id.id) {
return state.buffers.get(id.id);
}
var network = id.network, name = id.name; var network = id.network, name = id.name;
if (!network) { if (!network) {
network = state.activeNetwork; network = state.activeNetwork;
@ -284,30 +288,35 @@ export default class App extends Component {
}); });
} }
switchBuffer(name) { switchBuffer(id) {
var lastReadReceipt = this.getReceipt(name, ReceiptType.READ);
// TODO: only mark as read if user scrolled at the bottom
this.setBufferState({ name }, {
unread: Unread.NONE,
lastReadReceipt,
});
var buf; var buf;
this.setState((state) => { this.setState((state) => {
buf = getBuffer(state, { name }); buf = getBuffer(state, id);
if (!buf) { if (!buf) {
return; return;
} }
return { activeBuffer: buf.id }; return { activeBuffer: buf.id };
}, () => { }, () => {
if (!buf) {
return;
}
var lastReadReceipt = this.getReceipt(buf.name, ReceiptType.READ);
// TODO: only mark as read if user scrolled at the bottom
this.setBufferState(buf.id, {
unread: Unread.NONE,
lastReadReceipt,
});
if (this.composer.current) { if (this.composer.current) {
this.composer.current.focus(); this.composer.current.focus();
} }
if (!buf || buf.messages.length == 0) { if (buf.messages.length == 0) {
return; return;
} }
var lastMsg = buf.messages[buf.messages.length - 1]; var lastMsg = buf.messages[buf.messages.length - 1];
this.setReceipt(name, ReceiptType.READ, lastMsg); this.setReceipt(buf.name, ReceiptType.READ, lastMsg);
}); });
} }
@ -387,7 +396,7 @@ export default class App extends Component {
}); });
notif.addEventListener("click", () => { notif.addEventListener("click", () => {
// TODO: scroll to message // TODO: scroll to message
this.switchBuffer(target); this.switchBuffer({ network: netID, name: target });
}); });
} }
} }
@ -450,7 +459,7 @@ export default class App extends Component {
}); });
this.createBuffer(netID, SERVER_BUFFER); this.createBuffer(netID, SERVER_BUFFER);
this.switchBuffer(SERVER_BUFFER); this.switchBuffer({ network: netID, name: SERVER_BUFFER });
} }
handleClose(netID) { handleClose(netID) {
@ -584,7 +593,7 @@ export default class App extends Component {
} }
if (channel == this.state.connectParams.autojoin[0]) { if (channel == this.state.connectParams.autojoin[0]) {
// TODO: only switch once right after connect // TODO: only switch once right after connect
this.switchBuffer(channel); this.switchBuffer({ network: netID, name: channel });
} }
var receipt = this.getReceipt(channel, ReceiptType.READ); var receipt = this.getReceipt(channel, ReceiptType.READ);
@ -624,7 +633,7 @@ export default class App extends Component {
var members = new Map(buf.members); var members = new Map(buf.members);
members.delete(msg.prefix.name); members.delete(msg.prefix.name);
var offline = buf.name == msg.prefix.name; var offline = buf.name == msg.prefix.name;
buffers.set(buf.name, { ...buf, members, offline }); buffers.set(buf.id, { ...buf, members, offline });
affectedBuffers.push(buf.name); affectedBuffers.push(buf.name);
}); });
return { buffers }; return { buffers };
@ -644,7 +653,7 @@ export default class App extends Component {
var members = new Map(buf.members); var members = new Map(buf.members);
members.set(newNick, members.get(msg.prefix.name)); members.set(newNick, members.get(msg.prefix.name));
members.delete(msg.prefix.name); members.delete(msg.prefix.name);
buffers.set(buf.name, { ...buf, members }); buffers.set(buf.id, { ...buf, members });
affectedBuffers.push(buf.name); affectedBuffers.push(buf.name);
}); });
return { buffers }; return { buffers };
@ -707,7 +716,7 @@ export default class App extends Component {
this.client.send({ command: "WHO", params: [target] }); this.client.send({ command: "WHO", params: [target] });
} }
this.createBuffer(this.state.activeNetwork, target); this.createBuffer(this.state.activeNetwork, target);
this.switchBuffer(target); this.switchBuffer({ name: target });
} }
close(target) { close(target) {
@ -724,7 +733,7 @@ export default class App extends Component {
this.client.send({ command: "PART", params: [target] }); this.client.send({ command: "PART", params: [target] });
} }
this.switchBuffer(SERVER_BUFFER); this.switchBuffer({ name: SERVER_BUFFER });
this.setState((state) => { this.setState((state) => {
var buffers = new Map(state.buffers); var buffers = new Map(state.buffers);
buffers.delete(target); buffers.delete(target);
@ -789,7 +798,7 @@ export default class App extends Component {
} }
handleBufferListClick(name) { handleBufferListClick(name) {
this.switchBuffer(name); this.switchBuffer({ name });
} }
handleJoinClick(event) { handleJoinClick(event) {

View file

@ -28,10 +28,10 @@ export const keybindings = [
description: "Jump to next buffer with activity", description: "Jump to next buffer with activity",
execute: (app) => { execute: (app) => {
// TODO: order by priority, then by age // TODO: order by priority, then by age
var target = SERVER_BUFFER; var target = { name: SERVER_BUFFER };
for (var buf of app.state.buffers.values()) { for (var buf of app.state.buffers.values()) {
if (buf.unread != Unread.NONE) { if (buf.unread != Unread.NONE) {
target = buf.name; target = buf;
break; break;
} }
} }
@ -45,9 +45,9 @@ export const keybindings = [
execute: (app) => { execute: (app) => {
var prev = null; var prev = null;
for (var buf of app.state.buffers.values()) { for (var buf of app.state.buffers.values()) {
if (app.state.activeBuffer == buf.name) { if (app.state.activeBuffer == buf.id) {
if (prev) { if (prev) {
app.switchBuffer(prev.name); app.switchBuffer(prev);
} }
break; break;
} }
@ -63,9 +63,9 @@ export const keybindings = [
var found = false; var found = false;
for (var buf of app.state.buffers.values()) { for (var buf of app.state.buffers.values()) {
if (found) { if (found) {
app.switchBuffer(buf.name); app.switchBuffer(buf);
break; break;
} else if (app.state.activeBuffer == buf.name) { } else if (app.state.activeBuffer == buf.id) {
found = true; found = true;
} }
} }