Files
leocrm/frontend/src/utils/notifications.ts
T

42 lines
1.3 KiB
TypeScript

/**
* Notification permission helper — request and check notification permissions (Task 5.24).
*/
export type NotificationPermissionState = 'default' | 'granted' | 'denied' | 'unsupported';
export function getNotificationPermission(): NotificationPermissionState {
if (!('Notification' in window)) return 'unsupported';
return Notification.permission as NotificationPermissionState;
}
export async function requestNotificationPermission(): Promise<NotificationPermissionState> {
if (!('Notification' in window)) return 'unsupported';
if (Notification.permission === 'granted') return 'granted';
if (Notification.permission === 'denied') return 'denied';
try {
const result = await Notification.requestPermission();
return result as NotificationPermissionState;
} catch {
return 'denied';
}
}
export function showNotification(title: string, options?: NotificationOptions): void {
if (!('Notification' in window) || Notification.permission !== 'granted') return;
try {
new Notification(title, options);
} catch {
// Notification creation can fail in some browsers
}
}
export function isPWAInstalled(): boolean {
try {
return window.matchMedia('(display-mode: standalone)').matches ||
(window.navigator as unknown as { standalone?: boolean }).standalone === true;
} catch {
return false;
}
}