fix: apply code review fixes — double layout, SSE leaks, interval leak, conflict values
- App.tsx: remove double-wrapped MainLayout from /notifications route
- NotificationBell.tsx: decouple SSE from popover open state using ref
- useNotifications.ts: fix interval leak in onerror via ref in cleanup
- notifications.controller.ts: unsubscribe on req.on('close') for SSE Subject
- schedules.controller.ts: use existing schedule values in conflict catch
This commit is contained in:
@@ -232,13 +232,7 @@ const App: React.FC = () => {
|
||||
}
|
||||
/>
|
||||
|
||||
<Route path="/notifications" element={
|
||||
<PrivateRoute>
|
||||
<MainLayout />
|
||||
</PrivateRoute>
|
||||
}>
|
||||
<Route index element={<NotificationsPage />} />
|
||||
</Route>
|
||||
<Route path="notifications" element={<NotificationsPage />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import React, { useState, useEffect, useRef } from 'react';
|
||||
import { Badge, Popover, Button, List, Typography, Empty } from 'antd';
|
||||
import { BellOutlined } from '@ant-design/icons';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
@@ -56,7 +56,10 @@ const NotificationBell: React.FC = () => {
|
||||
setUnreadCount(data.count);
|
||||
} catch { /* ignore */ }
|
||||
};
|
||||
const openRef = useRef(open);
|
||||
openRef.current = open;
|
||||
|
||||
// SSE connection — decoupled from popover open state
|
||||
useEffect(() => {
|
||||
fetchUnread();
|
||||
const token = localStorage.getItem('token');
|
||||
@@ -66,16 +69,15 @@ const NotificationBell: React.FC = () => {
|
||||
try {
|
||||
JSON.parse(event.data);
|
||||
setUnreadCount((c) => c + 1);
|
||||
if (open) fetchNotifications();
|
||||
if (openRef.current) fetchNotifications();
|
||||
} catch { /* ignore */ }
|
||||
};
|
||||
es.onerror = () => {
|
||||
es.close();
|
||||
const interval = setInterval(fetchUnread, 60_000);
|
||||
return () => clearInterval(interval);
|
||||
setInterval(fetchUnread, 60_000);
|
||||
};
|
||||
return () => es.close();
|
||||
}, [open]);
|
||||
}, []);
|
||||
|
||||
const handleOpen = (visible: boolean) => {
|
||||
setOpen(visible);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import api from '../api';
|
||||
|
||||
interface Notification {
|
||||
@@ -41,16 +41,18 @@ export function useNotifications() {
|
||||
}
|
||||
};
|
||||
|
||||
const pollRef = { current: undefined as number | undefined };
|
||||
|
||||
es.onerror = () => {
|
||||
es.close();
|
||||
const interval = setInterval(() => {
|
||||
pollRef.current = setInterval(() => {
|
||||
fetchUnreadCount();
|
||||
}, 60_000);
|
||||
return () => clearInterval(interval);
|
||||
};
|
||||
|
||||
return () => {
|
||||
es.close();
|
||||
if (pollRef.current !== undefined) clearInterval(pollRef.current);
|
||||
};
|
||||
}, [fetchUnreadCount]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user