From 2f284f183ad83f46afbfc73d1393f79fbf041d05 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 29 Jun 2020 11:50:42 +0200 Subject: [PATCH] Open notification on new highlight --- components/app.js | 22 ++++++++++++++++++++++ components/buffer.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/components/app.js b/components/app.js index 6cc82dc..cdd50e9 100644 --- a/components/app.js +++ b/components/app.js @@ -168,12 +168,34 @@ export default class App extends Component { var msgUnread = Unread.NONE; if (msg.command == "PRIVMSG" || msg.command == "NOTICE") { + var target = msg.params[0]; var text = msg.params[1]; + + var kind; if (msg.prefix.name != this.client.nick && irc.isHighlight(text, this.client.nick)) { msgUnread = Unread.HIGHLIGHT; + kind = "highlight"; + } else if (target == this.client.nick) { + msgUnread = Unread.HIGHLIGHT; + kind = "private message"; } else { msgUnread = Unread.MESSAGE; } + + if (msgUnread == Unread.HIGHLIGHT && window.Notification && Notification.permission === "granted") { + var title = "New " + kind + " from " + msg.prefix.name; + if (this.isChannel(target)) { + title += " in " + target; + } + var notif = new Notification(title, { + body: text, + requireInteraction: true, + }); + notif.addEventListener("click", () => { + // TODO: scroll to message + this.switchBuffer(target); + }); + } } if (msg.prefix.name != this.client.nick && msg.command != "PART") { diff --git a/components/buffer.js b/components/buffer.js index 05cafd9..d2695a8 100644 --- a/components/buffer.js +++ b/components/buffer.js @@ -90,6 +90,44 @@ function LogLine(props) { `; } +class NotificationNagger extends Component { + state = { nag: false }; + + constructor(props) { + super(props); + + this.handleClick = this.handleClick.bind(this); + + this.state.nag = this.shouldNag(); + } + + shouldNag() { + return window.Notification && Notification.permission !== "granted" && Notification.permission !== "denied"; + } + + handleClick(event) { + event.preventDefault(); + + Notification.requestPermission((permission) => { + this.setState({ nag: this.shouldNag() }); + }); + } + + render() { + if (!this.state.nag) { + return null; + } + + return html` +
+ --:--:-- + ${" "} + Turn on desktop notifications to get notified about new messages +
+ `; + } +} + export default function Buffer(props) { if (!props.buffer) { return null; @@ -97,6 +135,7 @@ export default function Buffer(props) { return html`
+ <${NotificationNagger}/> ${props.buffer.messages.map((msg) => html` <${LogLine} key=${msg.key} message=${msg} onNickClick=${props.onNickClick}/> `)}