marmar-context / web / context / src / clinician / notify.ts
notify.ts
Raw
// NTF-2: optional browser notifications for urgent notes and finished check-ins.
// They name the patient at most, never health details (YCI-29).

export const notificationsSupported = () => typeof window !== "undefined" && "Notification" in window;
export const notificationsAllowed = () => notificationsSupported() && Notification.permission === "granted";

export async function askForNotifications(): Promise<boolean> {
  if (!notificationsSupported()) return false;
  if (Notification.permission === "granted") return true;
  if (Notification.permission === "denied") return false;
  return (await Notification.requestPermission()) === "granted";
}

export function notify(title: string, body: string, tag: string, onOpen?: () => void) {
  if (!notificationsAllowed()) return;
  try {
    const n = new Notification(title, { body, tag, icon: "/assets/logo-mark.svg" });
    n.onclick = () => {
      window.focus();
      onOpen?.();
      n.close();
    };
  } catch {
    // Some browsers only allow notifications from a service worker; Context shows its own banner anyway.
  }
}