File size: 2,260 Bytes
78d4e1b 436a8e5 78d4e1b 436a8e5 78d4e1b 436a8e5 78d4e1b 436a8e5 78d4e1b 436a8e5 78d4e1b 436a8e5 |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import { logStore, type LogEntry } from '~/lib/stores/logs';
export type NotificationType = 'info' | 'warning' | 'error' | 'success' | 'update';
export interface NotificationDetails {
type?: string;
message?: string;
currentVersion?: string;
latestVersion?: string;
branch?: string;
updateUrl?: string;
}
export interface Notification {
id: string;
title: string;
message: string;
type: NotificationType;
read: boolean;
timestamp: string;
details?: NotificationDetails;
}
interface LogEntryWithRead extends LogEntry {
read?: boolean;
}
const mapLogToNotification = (log: LogEntryWithRead): Notification => {
const type: NotificationType =
log.details?.type === 'update'
? 'update'
: log.level === 'error'
? 'error'
: log.level === 'warning'
? 'warning'
: 'info';
const baseNotification: Notification = {
id: log.id,
title: log.category.charAt(0).toUpperCase() + log.category.slice(1),
message: log.message,
type,
read: log.read || false,
timestamp: log.timestamp,
};
if (log.details) {
return {
...baseNotification,
details: log.details as NotificationDetails,
};
}
return baseNotification;
};
export const getNotifications = async (): Promise<Notification[]> => {
const logs = Object.values(logStore.logs.get()) as LogEntryWithRead[];
return logs
.filter((log) => {
if (log.details?.type === 'update') {
return true;
}
return log.level === 'error' || log.level === 'warning';
})
.map(mapLogToNotification)
.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 (!log.read) {
if (log.details?.type === 'update') {
return true;
}
return log.level === 'error' || log.level === 'warning';
}
return false;
}).length;
};
|