Skip to content

Commit 3daaec9

Browse files
committed
feat: Enhance API call robustness in NotificationCenter with try/catch for silent error handling and response status validation.
1 parent 8377632 commit 3daaec9

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/components/DashboardViews/NotificationCenter.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,27 @@ export default function NotificationCenter({ isDark }: NotificationCenterProps)
6262
const dropdownRef = useRef<HTMLDivElement>(null);
6363

6464
const apiCall = useCallback(async (path: string, options?: RequestInit) => {
65-
const session = await supabase.auth.getSession();
66-
const token = session.data.session?.access_token;
67-
if (!token) return null;
68-
const res = await fetch(`${import.meta.env.VITE_SUPABASE_URL}/functions/v1/api-notifications/${path}`, {
69-
...options,
70-
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...(options?.headers || {}) },
71-
});
72-
return res.json();
65+
try {
66+
const session = await supabase.auth.getSession();
67+
const token = session.data.session?.access_token;
68+
if (!token) return null;
69+
const res = await fetch(`${import.meta.env.VITE_SUPABASE_URL}/functions/v1/api-notifications/${path}`, {
70+
...options,
71+
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...(options?.headers || {}) },
72+
});
73+
if (!res.ok) return null;
74+
return res.json();
75+
} catch {
76+
// Silently handle network errors — edge function may not be deployed
77+
return null;
78+
}
7379
}, []);
7480

7581
const fetchUnreadCount = useCallback(async () => {
7682
try {
7783
const data = await apiCall('unread-count');
7884
if (data) setUnreadCount(data.count ?? 0);
79-
} catch {}
85+
} catch { }
8086
}, [apiCall]);
8187

8288
const fetchNotifications = useCallback(async () => {
@@ -119,22 +125,22 @@ export default function NotificationCenter({ isDark }: NotificationCenterProps)
119125
await apiCall('mark-read', { method: 'POST', body: JSON.stringify({ ids: [] }) });
120126
setNotifications(prev => prev.map(n => ({ ...n, is_read: true })));
121127
setUnreadCount(0);
122-
} catch {}
128+
} catch { }
123129
};
124130

125131
const markRead = async (id: string) => {
126132
try {
127133
await apiCall('mark-read', { method: 'POST', body: JSON.stringify({ ids: [id] }) });
128134
setNotifications(prev => prev.map(n => n.id === id ? { ...n, is_read: true } : n));
129135
setUnreadCount(prev => Math.max(0, prev - 1));
130-
} catch {}
136+
} catch { }
131137
};
132138

133139
const clearRead = async () => {
134140
try {
135141
await apiCall('', { method: 'DELETE' });
136142
setNotifications(prev => prev.filter(n => !n.is_read));
137-
} catch {}
143+
} catch { }
138144
};
139145

140146
return (

0 commit comments

Comments
 (0)