-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathAvatarWithFallback.tsx
More file actions
57 lines (50 loc) · 1.44 KB
/
AvatarWithFallback.tsx
File metadata and controls
57 lines (50 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type React from 'react';
import { useState } from 'react';
import { Avatar, Stack, Truncate } from '@primer/react';
import { type Link, Size, type UserType } from '../../types';
import { isNonHumanUser } from '../../utils/notifications/filters/userType';
import { getDefaultUserIcon } from '../../utils/ui/icons';
export interface AvatarWithFallbackProps {
src?: Link;
alt?: string;
name?: string;
size?: number;
userType?: UserType;
}
export const AvatarWithFallback: React.FC<AvatarWithFallbackProps> = ({
src,
alt,
name,
size = Size.MEDIUM,
userType = 'User',
}) => {
const [hasBrokenAvatarSource, setHasBrokenAvatarSource] = useState(false);
const isNonHuman = isNonHumanUser(userType);
const DefaultUserIcon = getDefaultUserIcon(userType);
// TODO explore using AnchoredOverlay component (https://primer.style/components/anchored-overlay/react/alpha) to render Avatar Card on hover
return (
<Stack
align="center"
data-testid="avatar"
direction="horizontal"
gap="condensed"
>
{!src || hasBrokenAvatarSource ? (
<DefaultUserIcon size={size} />
) : (
<Avatar
alt={alt}
onError={() => setHasBrokenAvatarSource(true)}
size={size}
square={isNonHuman}
src={src}
/>
)}
{name && (
<Truncate inline maxWidth={280} title={name}>
{name}
</Truncate>
)}
</Stack>
);
};