-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.tsx
More file actions
93 lines (84 loc) · 2.16 KB
/
index.tsx
File metadata and controls
93 lines (84 loc) · 2.16 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { EnsIdentity } from "@lib/api/types/get-ens";
import { Box, Skeleton } from "@livepeer/design-system";
import { QRCodeCanvas } from "qrcode.react";
import { useEffect, useState } from "react";
type Identity = Pick<EnsIdentity, "avatar" | "isLoading"> | null | undefined;
type Props = {
identity: Identity;
address: string;
size?: number;
css?: object;
};
const IdentityAvatar = ({ identity, address, size = 24, css = {} }: Props) => {
const [imageLoaded, setImageLoaded] = useState(false);
const [hasAvatarError, setHasAvatarError] = useState(false);
useEffect(() => {
setImageLoaded(false);
setHasAvatarError(false);
}, [identity?.avatar, address]);
const avatarSrc = identity?.avatar ?? null;
const awaitingEnsData = identity?.isLoading && !avatarSrc;
const baseStyles = {
borderRadius: 1000,
width: size,
height: size,
maxWidth: size,
maxHeight: size,
overflow: "hidden",
position: "relative",
...css,
};
if (!avatarSrc || hasAvatarError || awaitingEnsData) {
return (
<Box css={baseStyles}>
{awaitingEnsData ? (
<Skeleton
css={{
width: "100%",
height: "100%",
borderRadius: 1000,
}}
/>
) : (
<QRCodeCanvas
fgColor={`#${address.slice(2, 8)}`}
size={size}
value={address}
/>
)}
</Box>
);
}
return (
<Box css={baseStyles}>
{!imageLoaded && (
<Skeleton
css={{
width: "100%",
height: "100%",
borderRadius: 1000,
}}
/>
)}
<Box
as="img"
css={{
width: "100%",
height: "100%",
borderRadius: 1000,
position: "absolute",
top: 0,
left: 0,
objectFit: "cover",
opacity: imageLoaded ? 1 : 0,
transition: "opacity 150ms ease",
}}
src={avatarSrc}
alt={`${address} avatar`}
onLoad={() => setImageLoaded(true)}
onError={() => setHasAvatarError(true)}
/>
</Box>
);
};
export default IdentityAvatar;