53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
const PERMISSION_KEY = 'prediqo_support_notify_permission_asked';
|
|
|
|
export async function requestSupportNotificationPermission(): Promise<NotificationPermission | 'unsupported'> {
|
|
if (typeof window === 'undefined' || !('Notification' in window)) {
|
|
return 'unsupported';
|
|
}
|
|
if (Notification.permission === 'granted' || Notification.permission === 'denied') {
|
|
return Notification.permission;
|
|
}
|
|
try {
|
|
localStorage.setItem(PERMISSION_KEY, '1');
|
|
return await Notification.requestPermission();
|
|
} catch {
|
|
return Notification.permission;
|
|
}
|
|
}
|
|
|
|
export function shouldAskNotificationPermission(): boolean {
|
|
if (typeof window === 'undefined' || !('Notification' in window)) return false;
|
|
if (Notification.permission !== 'default') return false;
|
|
try {
|
|
return localStorage.getItem(PERMISSION_KEY) !== '1';
|
|
} catch {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
export function notifySupportMessage(options: {
|
|
title: string;
|
|
body: string;
|
|
tag?: string;
|
|
onClick?: () => void;
|
|
}): void {
|
|
if (typeof window === 'undefined' || !('Notification' in window)) return;
|
|
if (Notification.permission !== 'granted') return;
|
|
if (document.visibilityState === 'visible') return;
|
|
|
|
try {
|
|
const n = new Notification(options.title, {
|
|
body: options.body,
|
|
tag: options.tag ?? 'prediqo-support',
|
|
icon: '/brand/prediqo-mark.png',
|
|
});
|
|
n.onclick = () => {
|
|
window.focus();
|
|
options.onClick?.();
|
|
n.close();
|
|
};
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|