File size: 1,627 Bytes
c0a9bce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { logStore } from '~/lib/stores/logs';
import type { LogEntry } from '~/lib/stores/logs';

export interface Notification {
  id: string;
  title: string;
  message: string;
  type: 'info' | 'warning' | 'error' | 'success';
  timestamp: string;
  read: boolean;
  details?: Record<string, unknown>;
}

export interface LogEntryWithRead extends LogEntry {
  read: boolean;
}

export const getNotifications = async (): Promise<Notification[]> => {
  // Get notifications from the log store
  const logs = Object.values(logStore.logs.get());

  return logs
    .filter((log) => log.category !== 'system') // Filter out system logs
    .map((log) => ({
      id: log.id,
      title: (log.details?.title as string) || log.message.split('\n')[0],
      message: log.message,
      type: log.level as 'info' | 'warning' | 'error' | 'success',
      timestamp: log.timestamp,
      read: logStore.isRead(log.id),
      details: log.details,
    }))
    .sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
};

export const markNotificationRead = async (notificationId: string): Promise<void> => {
  logStore.markAsRead(notificationId);
};

export const clearNotifications = async (): Promise<void> => {
  logStore.clearLogs();
};

export const getUnreadCount = (): number => {
  const logs = Object.values(logStore.logs.get()) as LogEntryWithRead[];

  return logs.filter((log) => {
    if (!logStore.isRead(log.id)) {
      if (log.details?.type === 'update') {
        return true;
      }

      return log.level === 'error' || log.level === 'warning';
    }

    return false;
  }).length;
};