Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/ts/server/user/daemon/contexts/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SysDispatch } from "$ts/env";
import type { IconService } from "$ts/icon";
import type { Notification } from "$types/notification";
import type { UserDaemon } from "..";
import { UserContext } from "../context";
Expand All @@ -23,6 +24,27 @@ export class NotificationsUserContext extends UserContext {
SysDispatch.dispatch("update-notifications", [this.notifications]);
SysDispatch.dispatch("send-notification", [data]);

if (!document.hasFocus()) {
Notification.requestPermission().then((perm) => {
if (perm !== "granted") return;

// there's probably a better way to do this rather than inserting a CDN link
const icon = data.icon ? `https://unpkg.com/lucide-static@latest/icons/${data.icon}.svg` : undefined;
const image = data.image
? this.serviceHost?.getService<IconService>("IconService")?.getIconCached(data.image) || data.image
: icon;

const notif = new Notification(data.title, { body: data.message, icon: image, requireInteraction: true });
notif.onclose = () => this.deleteNotification(id);
notif.onclick = () => {
window.focus();
this.deleteNotification(id);
};

if (data.timeout) setTimeout(() => notif.close(), data.timeout);
});
}

return id;
}

Expand Down