2025-02-02 00:42:30 +00:00
|
|
|
import { logStore } from '~/lib/stores/logs';
|
|
|
|
import type { LogEntry } from '~/lib/stores/logs';
|
2025-01-21 10:55:26 +00:00
|
|
|
|
2025-01-20 08:53:15 +00:00
|
|
|
export interface Notification {
|
|
|
|
id: string;
|
|
|
|
title: string;
|
|
|
|
message: string;
|
2025-02-02 00:42:30 +00:00
|
|
|
type: 'info' | 'warning' | 'error' | 'success';
|
2025-01-20 08:53:15 +00:00
|
|
|
timestamp: string;
|
2025-02-02 00:42:30 +00:00
|
|
|
read: boolean;
|
|
|
|
details?: Record<string, unknown>;
|
2025-01-21 10:55:26 +00:00
|
|
|
}
|
|
|
|
|
2025-02-02 00:42:30 +00:00
|
|
|
export interface LogEntryWithRead extends LogEntry {
|
|
|
|
read: boolean;
|
2025-01-20 08:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getNotifications = async (): Promise<Notification[]> => {
|
2025-02-02 00:42:30 +00:00
|
|
|
// Get notifications from the log store
|
|
|
|
const logs = Object.values(logStore.logs.get());
|
2025-01-21 10:55:26 +00:00
|
|
|
|
|
|
|
return logs
|
2025-02-02 00:42:30 +00:00
|
|
|
.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,
|
|
|
|
}))
|
2025-01-21 10:55:26 +00:00
|
|
|
.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
|
2025-01-20 08:53:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const markNotificationRead = async (notificationId: string): Promise<void> => {
|
2025-01-21 10:55:26 +00:00
|
|
|
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) => {
|
2025-02-02 00:42:30 +00:00
|
|
|
if (!logStore.isRead(log.id)) {
|
2025-01-21 10:55:26 +00:00
|
|
|
if (log.details?.type === 'update') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return log.level === 'error' || log.level === 'warning';
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}).length;
|
2025-01-20 08:53:15 +00:00
|
|
|
};
|