The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Sanitize url before fetching favicon and display letter avatar if it can't be retrieved (#1035)
gitstart-twenty2680289ff7c0
diff --git a/front/src/modules/ui/table/editable-cell/type/components/GenericEditableURLCell.tsx b/front/src/modules/ui/table/editable-cell/type/components/GenericEditableURLCell.tsxindex aadf2f4cfd7..7a080f78116 100644--- a/front/src/modules/ui/table/editable-cell/type/components/GenericEditableURLCell.tsx+++ b/front/src/modules/ui/table/editable-cell/type/components/GenericEditableURLCell.tsx@@ -8,6 +8,7 @@ import {ViewFieldDefinition,ViewFieldURLMetadata,} from '@/ui/table/types/ViewField';+import { sanitizeURL } from '~/utils';import { GenericEditableURLCellEditMode } from './GenericEditableURLCellEditMode';@@ -33,7 +34,9 @@ export function GenericEditableURLCell({<EditableCelleditModeHorizontalAlign={editModeHorizontalAlign}editModeContent={<GenericEditableURLCellEditMode viewField={viewField} />}- nonEditModeContent={<InplaceInputURLDisplayMode value={fieldValue} />}+ nonEditModeContent={+ <InplaceInputURLDisplayMode value={sanitizeURL(fieldValue)} />+ }></EditableCell>);}diff --git a/front/src/modules/users/components/Avatar.tsx b/front/src/modules/users/components/Avatar.tsxindex cf7b9ab4732..25718306c28 100644--- a/front/src/modules/users/components/Avatar.tsx+++ b/front/src/modules/users/components/Avatar.tsx@@ -1,3 +1,4 @@+import { useEffect, useState } from 'react';import styled from '@emotion/styled';import { isNonEmptyString } from '~/utils/isNonEmptyString';@@ -86,6 +87,20 @@ export function Avatar({type = 'squared',}: OwnProps) {const noAvatarUrl = !isNonEmptyString(avatarUrl);+ const [isInvalidAvatarUrl, setIsInvalidAvatarUrl] = useState(false);++ useEffect(() => {+ if (avatarUrl) {+ new Promise((resolve) => {+ const img = new Image();+ img.onload = () => resolve(false);+ img.onerror = () => resolve(true);+ img.src = getImageAbsoluteURIOrBase64(avatarUrl) as string;+ }).then((res) => {+ setIsInvalidAvatarUrl(res as boolean);+ });+ }+ }, [avatarUrl]);return (<StyledAvatar@@ -95,7 +110,8 @@ export function Avatar({type={type}colorId={colorId}>- {noAvatarUrl && placeholder[0]?.toLocaleUpperCase()}+ {(noAvatarUrl || isInvalidAvatarUrl) &&+ placeholder[0]?.toLocaleUpperCase()}</StyledAvatar>);}diff --git a/front/src/utils/__tests__/utils.test.ts b/front/src/utils/__tests__/utils.test.tsindex 8ac6b547528..9fa996f4989 100644--- a/front/src/utils/__tests__/utils.test.ts+++ b/front/src/utils/__tests__/utils.test.ts@@ -1,21 +1,49 @@-import { getLogoUrlFromDomainName } from '..';+import { getLogoUrlFromDomainName, sanitizeURL } from '..';++describe('sanitizeURL', () => {+ test('should sanitize the URL correctly', () => {+ expect(sanitizeURL('http://example.com/')).toBe('example.com');+ expect(sanitizeURL('https://www.example.com/')).toBe('example.com');+ expect(sanitizeURL('www.example.com')).toBe('example.com');+ expect(sanitizeURL('example.com')).toBe('example.com');+ expect(sanitizeURL('example.com/')).toBe('example.com');+ });++ test('should handle undefined input', () => {+ expect(sanitizeURL(undefined)).toBe('');+ });+});describe('getLogoUrlFromDomainName', () => {- it(`should generate logo url if undefined `, () => {- expect(getLogoUrlFromDomainName(undefined)).toBe(- 'https://api.faviconkit.com/undefined/144',+ test('should return the correct logo URL for a given domain', () => {+ expect(getLogoUrlFromDomainName('example.com')).toBe(+ 'https://favicon.twenty.com/example.com',+ );++ expect(getLogoUrlFromDomainName('http://example.com/')).toBe(+ 'https://favicon.twenty.com/example.com',+ );++ expect(getLogoUrlFromDomainName('https://www.example.com/')).toBe(+ 'https://favicon.twenty.com/example.com',+ );++ expect(getLogoUrlFromDomainName('www.example.com')).toBe(+ 'https://favicon.twenty.com/example.com',+ );++ expect(getLogoUrlFromDomainName('example.com/')).toBe(+ 'https://favicon.twenty.com/example.com',);- });- it(`should generate logo url if defined `, () => {- expect(getLogoUrlFromDomainName('test.com')).toBe(- 'https://api.faviconkit.com/test.com/144',+ expect(getLogoUrlFromDomainName('apple.com')).toBe(+ 'https://favicon.twenty.com/apple.com',);});- it(`should generate logo url if empty `, () => {- expect(getLogoUrlFromDomainName('')).toBe(- 'https://api.faviconkit.com//144',+ test('should handle undefined input', () => {+ expect(getLogoUrlFromDomainName(undefined)).toBe(+ 'https://favicon.twenty.com/',);});});diff --git a/front/src/utils/index.ts b/front/src/utils/index.tsindex fcab4d1e0b7..ed5a6745c76 100644--- a/front/src/utils/index.ts+++ b/front/src/utils/index.ts@@ -10,6 +10,15 @@ export function formatToHumanReadableDate(date: Date | string) {}).format(parsedJSDate);}-export const getLogoUrlFromDomainName = (domainName?: string): string => {- return `https://api.faviconkit.com/${domainName}/144`;-};+export function sanitizeURL(link: string | null | undefined) {+ return link+ ? link.replace(/(https?:\/\/)|(www\.)/g, '').replace(/\/$/, '')+ : '';+}++export function getLogoUrlFromDomainName(+ domainName?: string,+): string | undefined {+ const sanitizedDomain = sanitizeURL(domainName);+ return `https://favicon.twenty.com/${sanitizedDomain}`;+}