A full-featured CRM built with React, shadcn/ui, and Supabase.
Add timeout on avatar fetch to avoid breaking the import on rate limited domains
François Zaninottof618108b22b1
diff --git a/src/contacts/useContactImport.tsx b/src/contacts/useContactImport.tsxindex 85e00129..b48194cb 100644--- a/src/contacts/useContactImport.tsx+++ b/src/contacts/useContactImport.tsx@@ -103,6 +103,7 @@ export function useContactImport() {tags: tagNames,linkedin_url,}) => {+ const singleEmail = email.split(',')[0];const company = companyName?.trim()? companies.get(companyName.trim()): undefined;@@ -116,7 +117,7 @@ export function useContactImport() {last_name,gender,title,- email,+ email: singleEmail,phone_1_number,phone_1_type,phone_2_number,diff --git a/src/misc/fetchWithTimeout.ts b/src/misc/fetchWithTimeout.tsnew file mode 100644index 00000000..79e6bea1--- /dev/null+++ b/src/misc/fetchWithTimeout.ts@@ -0,0 +1,19 @@+type FetchParams = Parameters<typeof fetch>;++export async function fetchWithTimeout(+ resource: string,+ options: FetchParams[1] & { timeout?: number } = {}+) {+ const { timeout = 2000 } = options;++ const controller = new AbortController();+ const id = setTimeout(() => controller.abort(), timeout);++ const response = await fetch(resource, {+ ...options,+ signal: controller.signal,+ });+ clearTimeout(id);++ return response;+}diff --git a/src/providers/commons/getCompanyAvatar.ts b/src/providers/commons/getCompanyAvatar.tsindex c8d83f05..6545377b 100644--- a/src/providers/commons/getCompanyAvatar.ts+++ b/src/providers/commons/getCompanyAvatar.ts@@ -1,4 +1,5 @@-import { Company } from '../../types';+import type { Company } from '../../types';+import { fetchWithTimeout } from '../../misc/fetchWithTimeout';// Helper function to get the favicon URLasync function getFaviconUrl(website: string): Promise<string | null> {@@ -7,7 +8,7 @@ async function getFaviconUrl(website: string): Promise<string | null> {const url = new URL(website);const domain = url.origin;const faviconUrl = `${domain}/favicon.ico`;- const response = await fetch(faviconUrl);+ const response = await fetchWithTimeout(faviconUrl);if (response.ok) {return faviconUrl;}diff --git a/src/providers/commons/getContactAvatar.ts b/src/providers/commons/getContactAvatar.tsindex b441dae0..5390961a 100644--- a/src/providers/commons/getContactAvatar.ts+++ b/src/providers/commons/getContactAvatar.ts@@ -1,4 +1,5 @@import { DOMAINS_NOT_SUPPORTING_FAVICON } from '../../misc/unsupportedDomains.const';+import { fetchWithTimeout } from '../../misc/fetchWithTimeout';import { Contact } from '../../types';export async function hash(string: string) {@@ -25,7 +26,7 @@ async function getFaviconUrl(domain: string): Promise<string | null> {try {const faviconUrl = `https://${domain}/favicon.ico`;- const response = await fetch(faviconUrl);+ const response = await fetchWithTimeout(faviconUrl);if (response.ok) {return faviconUrl;}