fix(notifications): preserve action state and accessible recovery
This commit is contained in:
@@ -198,7 +198,7 @@ function Shell({ jobPageSize, setJobPageSize, jobColumns, setJobColumns, themeMo
|
||||
const [me, setMe] = useState<MeResponse | null>(null);
|
||||
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
|
||||
const [reminderCount, setReminderCount] = useState(0);
|
||||
const [notificationCount, setNotificationCount] = useState(0);
|
||||
const [notificationCount, setNotificationCount] = useState<number | undefined>(undefined);
|
||||
const [notificationAnchor, setNotificationAnchor] = useState<HTMLElement | null>(null);
|
||||
|
||||
const path = location.pathname;
|
||||
@@ -255,13 +255,27 @@ function Shell({ jobPageSize, setJobPageSize, jobColumns, setJobColumns, themeMo
|
||||
return () => window.clearInterval(id);
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
let generation = 0;
|
||||
let controller: AbortController | null = null;
|
||||
const load = () => {
|
||||
api.get<{ count: number }>("/notifications/unread-count").then((r) => setNotificationCount(Math.max(0, Number(r.data?.count) || 0))).catch(() => setNotificationCount(0));
|
||||
controller?.abort();
|
||||
const current = ++generation;
|
||||
const request = new AbortController();
|
||||
controller = request;
|
||||
api.get<{ count: number }>("/notifications/unread-count", { signal: request.signal }).then((r) => {
|
||||
if (request.signal.aborted || current !== generation) return;
|
||||
const count = r.data?.count;
|
||||
setNotificationCount(typeof count === "number" && Number.isSafeInteger(count) && count >= 0 ? count : undefined);
|
||||
}).catch(() => {
|
||||
if (!request.signal.aborted && current === generation) setNotificationCount(undefined);
|
||||
});
|
||||
};
|
||||
load();
|
||||
const id = window.setInterval(load, 60000);
|
||||
window.addEventListener("notifications-changed", load);
|
||||
return () => {
|
||||
generation += 1;
|
||||
controller?.abort();
|
||||
window.clearInterval(id);
|
||||
window.removeEventListener("notifications-changed", load);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user