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

95 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-01-21 10:55:26 +00:00
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;
}
2025-01-20 08:53:15 +00:00
export interface Notification {
id: string;
title: string;
message: string;
2025-01-21 10:55:26 +00:00
type: NotificationType;
2025-01-20 08:53:15 +00:00
read: boolean;
timestamp: string;
2025-01-21 10:55:26 +00:00
details?: NotificationDetails;
}
interface LogEntryWithRead extends LogEntry {
read?: boolean;
2025-01-20 08:53:15 +00:00
}
2025-01-21 10:55:26 +00:00
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;
};
2025-01-20 08:53:15 +00:00
export const getNotifications = async (): Promise<Notification[]> => {
2025-01-21 10:55:26 +00:00
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());
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 (!log.read) {
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
};