Tangled notifications browser extension
1const BASE_URL = "https://tangled.org";
2
3export const DEFAULT_POLL_INTERVAL = 60;
4
5export const getNotificationsUrl = (): string => `${BASE_URL}/notifications`;
6
7export const getNotificationsUrlPattern = (): string =>
8 `${BASE_URL}/notifications*`;
9
10export interface NotificationResult {
11 count: number;
12 pollInterval: number;
13}
14
15export const fetchNotificationCount = async (): Promise<NotificationResult> => {
16 const response = await fetch(`${BASE_URL}/notifications/count`, {
17 credentials: "include",
18 });
19 // FIX: requires backend to return 401 when not logged in (currently returns 200 even if not logged in)
20 if (!response.ok) throw new Error(`HTTP ${response.status}`);
21 const html = await response.text();
22 const match = html.match(/(\d+)/);
23 const pollInterval =
24 Number(response.headers.get("X-Poll-Interval")) || DEFAULT_POLL_INTERVAL;
25 return {
26 count: match ? parseInt(match[1], 10) : 0,
27 pollInterval,
28 };
29};