ui fix
This commit is contained in:
@@ -1,34 +1,17 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { getNotifications, markNotificationRead, type Notification } from '~/lib/api/notifications';
|
||||
|
||||
const READ_NOTIFICATIONS_KEY = 'bolt_read_notifications';
|
||||
|
||||
const getReadNotifications = (): string[] => {
|
||||
try {
|
||||
const stored = localStorage.getItem(READ_NOTIFICATIONS_KEY);
|
||||
return stored ? JSON.parse(stored) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const setReadNotifications = (notificationIds: string[]) => {
|
||||
try {
|
||||
localStorage.setItem(READ_NOTIFICATIONS_KEY, JSON.stringify(notificationIds));
|
||||
} catch (error) {
|
||||
console.error('Failed to persist read notifications:', error);
|
||||
}
|
||||
};
|
||||
import { logStore } from '~/lib/stores/logs';
|
||||
import { useStore } from '@nanostores/react';
|
||||
|
||||
export const useNotifications = () => {
|
||||
const [hasUnreadNotifications, setHasUnreadNotifications] = useState(false);
|
||||
const [unreadNotifications, setUnreadNotifications] = useState<Notification[]>([]);
|
||||
const [readNotificationIds, setReadNotificationIds] = useState<string[]>(() => getReadNotifications());
|
||||
const logs = useStore(logStore.logs);
|
||||
|
||||
const checkNotifications = async () => {
|
||||
try {
|
||||
const notifications = await getNotifications();
|
||||
const unread = notifications.filter((n) => !readNotificationIds.includes(n.id));
|
||||
const unread = notifications.filter((n) => !logStore.isRead(n.id));
|
||||
setUnreadNotifications(unread);
|
||||
setHasUnreadNotifications(unread.length > 0);
|
||||
} catch (error) {
|
||||
@@ -43,17 +26,12 @@ export const useNotifications = () => {
|
||||
const interval = setInterval(checkNotifications, 60 * 1000);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [readNotificationIds]);
|
||||
}, [logs]); // Re-run when logs change
|
||||
|
||||
const markAsRead = async (notificationId: string) => {
|
||||
try {
|
||||
await markNotificationRead(notificationId);
|
||||
|
||||
const newReadIds = [...readNotificationIds, notificationId];
|
||||
setReadNotificationIds(newReadIds);
|
||||
setReadNotifications(newReadIds);
|
||||
setUnreadNotifications((prev) => prev.filter((n) => n.id !== notificationId));
|
||||
setHasUnreadNotifications(unreadNotifications.length > 1);
|
||||
await checkNotifications();
|
||||
} catch (error) {
|
||||
console.error('Failed to mark notification as read:', error);
|
||||
}
|
||||
@@ -61,13 +39,9 @@ export const useNotifications = () => {
|
||||
|
||||
const markAllAsRead = async () => {
|
||||
try {
|
||||
await Promise.all(unreadNotifications.map((n) => markNotificationRead(n.id)));
|
||||
|
||||
const newReadIds = [...readNotificationIds, ...unreadNotifications.map((n) => n.id)];
|
||||
setReadNotificationIds(newReadIds);
|
||||
setReadNotifications(newReadIds);
|
||||
setUnreadNotifications([]);
|
||||
setHasUnreadNotifications(false);
|
||||
const notifications = await getNotifications();
|
||||
await Promise.all(notifications.map((n) => markNotificationRead(n.id)));
|
||||
await checkNotifications();
|
||||
} catch (error) {
|
||||
console.error('Failed to mark all notifications as read:', error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user