~emersion/public-inbox

gamja: Show number of unread buffers in document title v1 SUPERSEDED

sitting33: 1
 Show number of unread buffers in document title

 1 files changed, 71 insertions(+), 6 deletions(-)
#1011825 .build.yml success
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/42074/mbox | git am -3
Learn more about email & git

[PATCH gamja] Show number of unread buffers in document title Export this patch

---
This also prevents marking the buffer as read when the window/tab isn't
focused. When the focus gets restored, it gets marked.

I also added the server name in the title, since it fits there anyway.
Thanks for the awesome webchat!

Implements: https://todo.sr.ht/~emersion/gamja/134

 components/app.js | 77 +++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 71 insertions(+), 6 deletions(-)

diff --git a/components/app.js b/components/app.js
index 3987749..a5b6aeb 100644
--- a/components/app.js
+++ b/components/app.js
@@ -585,14 +585,40 @@ export default class App extends Component {
				this.whoChannelBuffer(buf.name, buf.server);
			}

			if (buf.type !== BufferType.SERVER) {
				document.title = buf.name + ' · ' + this.baseTitle;
			} else {
				document.title = this.baseTitle;
			}
			this.updateDocumentTitle();
		});
	}

	updateDocumentTitle() {
		const currentBuffer = State.getBuffer(this.state, this.state.activeBuffer);
		const currentServerName = getServerName(this.state.servers.get(currentBuffer.server));

		let resultingTitle = "";

		// add unread buffers counter
		let unreadBuffersCounter = 0;
		for (const buffer of this.state.buffers.values()) {
			if (buffer.unread > Unread.NONE) unreadBuffersCounter += 1;
		}
		if (unreadBuffersCounter > 0) {
			resultingTitle += "(";
			resultingTitle += unreadBuffersCounter;
			resultingTitle += ") ";
		}

		// add current buffer name
		if (currentBuffer.type !== BufferType.SERVER) {
			resultingTitle += currentBuffer.name + ' · ';
		}

		resultingTitle += currentServerName + ' · ';

		// add the base title (document.title)
		resultingTitle += this.baseTitle;

		document.title = resultingTitle;
	}

	prepareChatMessage(serverID, msg) {
		// Treat server-wide broadcasts as highlights. They're sent by server
		// operators and can contain important information.
@@ -717,7 +743,7 @@ export default class App extends Component {
			let prevReadReceipt = buf.prevReadReceipt;
			let receipts = { [ReceiptType.DELIVERED]: receiptFromMessage(msg) };

			if (this.state.activeBuffer !== buf.id) {
			if (this.state.activeBuffer !== buf.id || !document.hasFocus()) {
				unread = Unread.union(unread, msgUnread);
			} else {
				receipts[ReceiptType.READ] = receiptFromMessage(msg);
@@ -738,6 +764,10 @@ export default class App extends Component {
				this.sendReadReceipt(client, stored);
			}
			return { unread, prevReadReceipt };
		}, () => {
			// don't update the title if we're getting the history
			const chatHistoryBatch = irc.findBatchByType(msg, "chathistory");
			if (!chatHistoryBatch) this.updateDocumentTitle();
		});
	}

@@ -1912,13 +1942,48 @@ export default class App extends Component {
		}
	}

	onDocumentFocus() {
		const buffer = State.getBuffer(this.state, this.state.activeBuffer);
		const client = this.clients.get(buffer.server);

		const lastMessage = buffer.messages[buffer.messages.length - 1];
		if (!lastMessage) return;

		const lastReceipt = getReceipt(buffer, ReceiptType.READ);
		const newReceipt = receiptFromMessage(lastMessage);
		if (lastReceipt && lastReceipt.time === newReceipt.time) {
			return;
		}

		this.setBufferState(buffer, buffer => {
			const stored = {
				name: buffer.name,
				server: client.params,
				unread: Unread.NONE,
				receipts: { [ReceiptType.READ]: newReceipt },
			};
			if (this.bufferStore.put(stored)) {
				this.sendReadReceipt(client, stored);
			}

			return {
				unread: Unread.NONE,
			};
		}, () => {
			this.updateDocumentTitle();
		});
	}

	componentDidMount() {
		this.baseTitle = document.title;
		setupKeybindings(this);

		document.addEventListener("focus", this.onDocumentFocus.bind(this));
	}

	componentWillUnmount() {
		document.title = this.baseTitle;
		document.removeEventListener("blur", this.onDocumentFocus.bind(this));
	}

	render() {
-- 
2.41.0
Thanks for sending a patch! Here are a few comments/questions.
gamja/patches/.build.yml: SUCCESS in 39s

[Show number of unread buffers in document title][0] from [sitting33][1]

[0]: https://lists.sr.ht/~emersion/public-inbox/patches/42074
[1]: mailto:me@sit.sh

✓ #1011825 SUCCESS gamja/patches/.build.yml https://builds.sr.ht/~emersion/job/1011825