Files
zetaloop cd469d3d54 refactor(notifications): fetch from backend API instead of local generation
Rewrite store/notifications.ts to fetch via listNotifications
API and remove local generateId-based notification creation.
The store now acts as a simple cache with fetch/invalidate methods.
Header unread count reads from this API-backed cache.
2026-05-01 17:33:05 +08:00

37 lines
1.0 KiB
TypeScript

import { listNotifications } from "@/lib/api/notifications"
import type { Notification } from "@/lib/types"
import { create } from "zustand"
interface NotificationState {
notifications: Notification[]
loading: boolean
fetchNotifications: () => Promise<void>
setNotifications: (notifications: Notification[]) => void
markAllRead: () => void
invalidate: () => void
}
export const useNotificationStore = create<NotificationState>((set) => ({
notifications: [],
loading: false,
fetchNotifications: async () => {
set({ loading: true })
try {
const items = await listNotifications({ offset: 0, limit: 50 })
set({ notifications: items, loading: false })
} catch {
set({ loading: false })
}
},
setNotifications: (notifications) => set({ notifications }),
markAllRead: () =>
set((state) => ({
notifications: state.notifications.map((notification) =>
notification.read ? notification : { ...notification, read: true },
),
})),
invalidate: () => {
set({ notifications: [] })
},
}))