bolt.diy/app/lib/api/notifications.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

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;
type: 'info' | 'warning' | 'error' | 'success';
2025-01-20 08:53:15 +00:00
timestamp: string;
read: boolean;
details?: Record<string, unknown>;
2025-01-21 10:55:26 +00:00
}
export interface LogEntryWithRead extends LogEntry {
read: boolean;
2025-01-20 08:53:15 +00:00
}
export const getNotifications = async (): Promise<Notification[]> => {
// Get notifications from the log store
const logs = Object.values(logStore.logs.get());
2025-01-21 10:55:26 +00:00
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,
}))
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) => {
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
};