File size: 1,832 Bytes
bd2b21f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
export async function requestNotificationPermission(): Promise<boolean> {
  if (!('Notification' in window)) {
    console.warn('This browser does not support notifications');
    return false;
  }

  if (Notification.permission === 'granted') {
    return true;
  }

  if (Notification.permission !== 'denied') {
    const permission = await Notification.requestPermission();
    return permission === 'granted';
  }

  return false;
}

export function showNotification(title: string, options?: NotificationOptions): Notification | null {
  if (!('Notification' in window) || Notification.permission !== 'granted') {
    return null;
  }

  const notification = new Notification(title, {
    icon: '/favicon.ico',
    badge: '/favicon.ico',
    ...options
  });

  setTimeout(() => notification.close(), 5000);

  return notification;
}

export function showWeatherAlert(weather: string, temp: number): void {
  const badWeather = ['Rain', 'Drizzle', 'Snow', 'Thunderstorm', 'Fog', 'Mist'];
  if (!badWeather.includes(weather)) return;

  showNotification('Weather Alert', {
    body: `${weather} detected! Temperature: ${Math.round(temp - 273.15)}°C. Drive safely!`,
    tag: 'weather-alert',
    requireInteraction: false
  });
}

export function showRouteAlert(distance: string, duration: string, safetyScore: number): void {
  const emoji = safetyScore >= 70 ? '✅' : safetyScore >= 40 ? '⚠️' : '🚨';
  showNotification('Route Ready', {
    body: `${emoji} ${distance}${duration} • Safety: ${safetyScore}%`,
    tag: 'route-ready'
  });
}

export function showIncidentAlert(incidentType: string, severity: string): void {
  showNotification('Incident Nearby', {
    body: `${incidentType} reported - ${severity} severity. Please drive carefully.`,
    tag: 'incident-alert',
    requireInteraction: true
  });
}