Files
twenty/front/src/modules/users/components/Avatar.tsx
gitstart-twenty 2680289ff7 Sanitize url before fetching favicon and display letter avatar if it can't be retrieved (#1035)
* Sanitize url before fetching favicon and display letter avatar if it can't be retrieved

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

* Priorotise www for apple.com domain

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

* Add requested changes

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

* Fix the tests

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

* Change avatar generation strategy

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

---------

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>
2023-08-02 11:53:56 -07:00

118 lines
2.8 KiB
TypeScript

import { useEffect, useState } from 'react';
import styled from '@emotion/styled';
import { isNonEmptyString } from '~/utils/isNonEmptyString';
import { stringToHslColor } from '~/utils/string-to-hsl';
import { getImageAbsoluteURIOrBase64 } from '../utils/getProfilePictureAbsoluteURI';
export type AvatarType = 'squared' | 'rounded';
export type AvatarSize = 'xl' | 'lg' | 'md' | 'sm' | 'xs';
type OwnProps = {
avatarUrl: string | null | undefined;
size: AvatarSize;
placeholder: string;
colorId?: string;
type?: AvatarType;
};
export const StyledAvatar = styled.div<OwnProps & { colorId: string }>`
align-items: center;
background-color: ${({ avatarUrl, colorId }) =>
!isNonEmptyString(avatarUrl) ? stringToHslColor(colorId, 75, 85) : 'none'};
${({ avatarUrl }) =>
isNonEmptyString(avatarUrl) ? `background-image: url(${avatarUrl});` : ''}
background-size: cover;
border-radius: ${(props) => (props.type === 'rounded' ? '50%' : '2px')};
color: ${({ colorId }) => stringToHslColor(colorId, 75, 25)};
display: flex;
flex-shrink: 0;
font-size: ${({ size }) => {
switch (size) {
case 'xl':
return '16px';
case 'lg':
return '13px';
case 'md':
default:
return '12px';
case 'sm':
return '10px';
case 'xs':
return '8px';
}
}};
font-weight: ${({ theme }) => theme.font.weight.medium};
height: ${({ size }) => {
switch (size) {
case 'xl':
return '40px';
case 'lg':
return '24px';
case 'md':
default:
return '16px';
case 'sm':
return '14px';
case 'xs':
return '12px';
}
}};
justify-content: center;
width: ${({ size }) => {
switch (size) {
case 'xl':
return '40px';
case 'lg':
return '24px';
case 'md':
default:
return '16px';
case 'sm':
return '14px';
case 'xs':
return '12px';
}
}};
`;
export function Avatar({
avatarUrl,
size,
placeholder,
colorId = placeholder,
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
avatarUrl={getImageAbsoluteURIOrBase64(avatarUrl)}
placeholder={placeholder}
size={size}
type={type}
colorId={colorId}
>
{(noAvatarUrl || isInvalidAvatarUrl) &&
placeholder[0]?.toLocaleUpperCase()}
</StyledAvatar>
);
}