Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,17 @@ export const fetchUserProfile = async (username: string, token: string) => {
};

export const fetchWeeklyTabUsage = async (token: string) => {
const res = await fetch(`${BACKEND_URL}/api/analytics/tab-usage/weekly`, {
headers: { Authorization: `Bearer ${token}` }
});
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

Comment on lines +191 to +192
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timezone retrieval using Intl.DateTimeFormat().resolvedOptions().timeZone lacks error handling. If this API fails or returns undefined in some browsers or environments, the function will silently pass an undefined or malformed timezone parameter. Consider adding a try-catch block with a fallback timezone (e.g., 'UTC') or validation to ensure the timezone is valid before using it.

Suggested change
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
let timezone = 'UTC';
try {
const options = typeof Intl !== 'undefined' && typeof Intl.DateTimeFormat === 'function'
? Intl.DateTimeFormat().resolvedOptions()
: null;
if (options && typeof options.timeZone === 'string' && options.timeZone.trim() !== '') {
timezone = options.timeZone;
}
} catch {
// Fallback to default 'UTC' timezone if retrieval fails
}

Copilot uses AI. Check for mistakes.
const res = await fetch(
`${BACKEND_URL}/api/analytics/tab-usage/weekly?timezone=${encodeURIComponent(timezone)}`,
{ headers: { Authorization: `Bearer ${token}` } }
);

return await res.json();
};


export const updatePrivacySettings = async (data: any, token: string) => {
const res = await fetch(`${BACKEND_URL}/api/profile/privacy`, {
method: 'PATCH',
Expand All @@ -215,12 +220,17 @@ export const flushAnalytics = async (token: string) => {
};

export async function fetchHourlyPresence(token: string, days = 7) {
const res = await fetch(`${BACKEND_URL}/api/analytics/presence/hourly?days=${days}`, {
headers: { Authorization: `Bearer ${token}` }
});
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timezone retrieval using Intl.DateTimeFormat().resolvedOptions().timeZone lacks error handling. If this API fails or returns undefined in some browsers or environments, the function will silently pass an undefined or malformed timezone parameter. Consider adding a try-catch block with a fallback timezone (e.g., 'UTC') or validation to ensure the timezone is valid before using it.

Copilot uses AI. Check for mistakes.
const res = await fetch(
`${BACKEND_URL}/api/analytics/presence/hourly?days=${days}&timezone=${encodeURIComponent(timezone)}`,
{
headers: { Authorization: `Bearer ${token}` }
}
);
return res.json();
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing await keyword before res.json(). While this doesn't break functionality (the promise is still returned correctly), it's inconsistent with other similar functions in this file (see lines 135, 153, 160, 168, 176, 211, 219, 261) that use return await res.json(). For consistency and better error handling within this function's scope, add the await keyword.

Copilot uses AI. Check for mistakes.
}


export async function fetchLeaderboard(token: string, page = 1, limit = 10, month?: string) {
const params = new URLSearchParams({
page: page.toString(),
Expand Down