149 lines
5.1 KiB
React
149 lines
5.1 KiB
React
import { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
|
|
import api from "@/utils/api.util";
|
|
|
|
const ClientNotificationContext = createContext(null);
|
|
|
|
const POLL_INTERVAL_NORMAL = 60_000;
|
|
const POLL_INTERVAL_FAST = 10_000;
|
|
|
|
export function useClientNotifications() {
|
|
const ctx = useContext(ClientNotificationContext);
|
|
if (!ctx) throw new Error("useClientNotifications must be used within ClientNotificationProvider");
|
|
return ctx;
|
|
}
|
|
|
|
const DEFAULT_PAGINATION = { page: 1, limit: 10, pages: 1, total: 0 };
|
|
|
|
export function ClientNotificationProvider({ children }) {
|
|
const [notifications, setNotifications] = useState([]);
|
|
const [unseenCount, setUnseenCount] = useState(0);
|
|
const [stickyAnnouncements, setStickyAnnouncements] = useState([]);
|
|
const [bannerImage, setBannerImage] = useState(null);
|
|
const [loading, setLoading] = useState(false);
|
|
const [pagination, setPagination] = useState(DEFAULT_PAGINATION);
|
|
const intervalRef = useRef(null);
|
|
const pollSpeedRef = useRef(POLL_INTERVAL_NORMAL);
|
|
|
|
const fetchUnseen = useCallback(async () => {
|
|
try {
|
|
const res = await api.get("/client/notifications/unseen");
|
|
setUnseenCount(res.data?.data?.count ?? 0);
|
|
} catch {
|
|
// silent — badge stays at last known count
|
|
}
|
|
}, []);
|
|
|
|
const fetchStickyAnnouncement = useCallback(async () => {
|
|
try {
|
|
const res = await api.get("/client/notifications/sticky");
|
|
setStickyAnnouncements(res.data?.data?.announcements ?? []);
|
|
setBannerImage(res.data?.data?.bannerImage ?? null);
|
|
} catch {
|
|
// silent
|
|
}
|
|
}, []);
|
|
|
|
const poll = useCallback(async () => {
|
|
await Promise.allSettled([fetchUnseen(), fetchStickyAnnouncement()]);
|
|
}, [fetchUnseen, fetchStickyAnnouncement]);
|
|
|
|
const fetchNotifications = useCallback(async (page = 1, limit = 10) => {
|
|
setLoading(true);
|
|
try {
|
|
const res = await api.get(`/client/notifications?limit=${limit}&page=${page}`);
|
|
const rows = res.data?.data?.notifications ?? [];
|
|
const pag = res.data?.data?.pagination ?? { page, limit, pages: 1, total: rows.length };
|
|
setNotifications(rows);
|
|
setPagination({ ...pag, pages: Math.max(1, pag.pages) });
|
|
} catch {
|
|
// silent
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
const clearAll = useCallback(async () => {
|
|
try {
|
|
await api.delete("/client/notifications/clear-all");
|
|
setNotifications([]);
|
|
setUnseenCount(0);
|
|
setPagination(DEFAULT_PAGINATION);
|
|
setStickyAnnouncements([]);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}, []);
|
|
|
|
const markSeen = useCallback(async (id) => {
|
|
try {
|
|
await api.patch(`/client/notifications/${id}/seen`);
|
|
setNotifications(prev =>
|
|
prev.map(n => n.notification_id === id ? { ...n, seen: true } : n)
|
|
);
|
|
|
|
setStickyAnnouncements(prev => prev.filter(a => a.notification_id !== id));
|
|
|
|
// Re-sync badge + sticky immediately (handles cases where the marked
|
|
// row isn't present in the currently loaded notifications page).
|
|
await Promise.allSettled([fetchUnseen(), fetchStickyAnnouncement()]);
|
|
} catch {
|
|
// silent
|
|
}
|
|
}, [fetchUnseen, fetchStickyAnnouncement]);
|
|
|
|
const markAllSeen = useCallback(async () => {
|
|
try {
|
|
await api.patch("/client/notifications/seen-all");
|
|
setNotifications(prev => prev.map(n => ({ ...n, seen: true })));
|
|
setUnseenCount(0);
|
|
setStickyAnnouncements([]);
|
|
} catch {
|
|
// silent
|
|
}
|
|
}, []);
|
|
|
|
const restartPoll = useCallback((interval) => {
|
|
clearInterval(intervalRef.current);
|
|
intervalRef.current = setInterval(poll, interval);
|
|
}, [poll]);
|
|
|
|
// Speed up polling while a student is mid-assessment; restore when done
|
|
const accelerate = useCallback(() => {
|
|
if (pollSpeedRef.current === POLL_INTERVAL_FAST) return;
|
|
pollSpeedRef.current = POLL_INTERVAL_FAST;
|
|
restartPoll(POLL_INTERVAL_FAST);
|
|
}, [restartPoll]);
|
|
|
|
const decelerate = useCallback(() => {
|
|
if (pollSpeedRef.current === POLL_INTERVAL_NORMAL) return;
|
|
pollSpeedRef.current = POLL_INTERVAL_NORMAL;
|
|
restartPoll(POLL_INTERVAL_NORMAL);
|
|
}, [restartPoll]);
|
|
|
|
useEffect(() => {
|
|
poll();
|
|
intervalRef.current = setInterval(poll, POLL_INTERVAL_NORMAL);
|
|
return () => clearInterval(intervalRef.current);
|
|
}, [poll]);
|
|
|
|
return (
|
|
<ClientNotificationContext.Provider value={{
|
|
notifications,
|
|
unseenCount,
|
|
stickyAnnouncements,
|
|
bannerImage,
|
|
loading,
|
|
pagination,
|
|
fetchNotifications,
|
|
markSeen,
|
|
markAllSeen,
|
|
clearAll,
|
|
accelerate,
|
|
decelerate,
|
|
}}>
|
|
{children}
|
|
</ClientNotificationContext.Provider>
|
|
);
|
|
}
|