-
Notifications
You must be signed in to change notification settings - Fork 2
/
Avatar.tsx
191 lines (180 loc) · 5.4 KB
/
Avatar.tsx
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React, { ComponentProps } from "react";
import {
Image,
ImageRequireSource,
ImageURISource,
StyleSheet,
View
} from "react-native";
import { Icon } from "../../components/icons";
import {
IOColors,
IOSpacingScale,
IOVisualCostants,
hexToRgba,
useIOTheme
} from "../../core";
import { addCacheTimestampToUri } from "../../utils/image";
import avatarSearchPlaceholder from "./placeholder/avatar-placeholder.png";
type Avatar = {
size: "small" | "medium";
logoUri?: ImageRequireSource | ImageURISource | ReadonlyArray<ImageURISource>;
};
const internalSpaceDefaultSize: number = 6;
const internalSpaceLargeSize: number = 9;
const internalSpacePlaceholderDefaultSize: IOSpacingScale = 12;
const internalSpacePlaceholderLargeSize: IOSpacingScale = 16;
const avatarBorderLightMode = hexToRgba(IOColors.black, 0.1);
const dimensionsMap = {
small: {
size: IOVisualCostants.avatarSizeSmall,
internalSpace: internalSpaceDefaultSize,
internalSpacePlaceholder: internalSpacePlaceholderDefaultSize,
radius: IOVisualCostants.avatarRadiusSizeSmall
},
medium: {
size: IOVisualCostants.avatarSizeMedium,
internalSpace: internalSpaceLargeSize,
internalSpacePlaceholder: internalSpacePlaceholderLargeSize,
radius: IOVisualCostants.avatarRadiusSizeMedium
}
};
const styles = StyleSheet.create({
avatarWrapper: {
overflow: "hidden",
borderColor: avatarBorderLightMode,
borderWidth: 1,
borderCurve: "continuous"
},
avatarInnerWrapper: {
overflow: "hidden",
backgroundColor: IOColors.white,
borderCurve: "continuous"
},
avatarImage: {
resizeMode: "contain",
height: "100%",
width: "100%"
}
});
/**
* Avatar component is used to display the logo of an organization. It accepts the following props:
* - `logoUri`: the uri of the image to display. If not provided, a placeholder icon will be displayed. It can be a single uri or an array of uris, in which case the first one that is available will be used.
* - `shape`: the shape of the avatar, can be "circle" or "square"
* - `size`: the size of the avatar, can be "small" or "medium"
* @param AvatarProps
* @returns
*/
export const Avatar = ({ logoUri, size }: Avatar) => {
const theme = useIOTheme();
const indexValue = React.useRef<number>(0);
const [imageSource, setImageSource] = React.useState(
logoUri === undefined
? undefined
: Array.isArray(logoUri)
? addCacheTimestampToUri(logoUri[0])
: typeof logoUri === "number"
? logoUri
: addCacheTimestampToUri(logoUri as ImageURISource)
);
const onError = () => {
if (Array.isArray(logoUri) && indexValue.current + 1 < logoUri.length) {
// eslint-disable-next-line functional/immutable-data
indexValue.current = indexValue.current + 1;
setImageSource(addCacheTimestampToUri(logoUri[indexValue.current]));
return;
}
setImageSource(undefined);
};
return (
<View
accessibilityIgnoresInvertColors
style={[
styles.avatarWrapper,
{
height: dimensionsMap[size].size,
width: dimensionsMap[size].size,
borderRadius: dimensionsMap[size].radius,
backgroundColor:
imageSource === undefined ? IOColors["grey-50"] : IOColors.white,
padding:
imageSource === undefined
? dimensionsMap[size].internalSpacePlaceholder
: dimensionsMap[size].internalSpace
}
]}
>
{imageSource === undefined ? (
<Icon
name="institution"
color={theme["icon-decorative"]}
size={"100%"}
/>
) : (
<View
style={[
styles.avatarInnerWrapper,
{
borderRadius:
dimensionsMap[size].radius - dimensionsMap[size].internalSpace
}
]}
>
<Image
accessibilityIgnoresInvertColors
source={imageSource}
style={styles.avatarImage}
onError={onError}
/>
</View>
)}
</View>
);
};
export type AvatarSearchProps = Pick<
ComponentProps<typeof Image>,
"source" | "defaultSource"
>;
/**
* AvatarSearch component is used to display the logo of an institution in the search results.
* A placeholder is displayed if the logo is not available.
* Note: On Android, the default source prop is ignored on debug builds.
*
* @param AvatarSearchProps
* @returns
*/
export const AvatarSearch = React.memo(
({ defaultSource, source }: AvatarSearchProps) => {
// Visual attributes
const avatarSize = dimensionsMap.small.size;
const borderRadius = dimensionsMap.small.radius;
const internalSpace = dimensionsMap.small.internalSpace;
const innerRadius = borderRadius - internalSpace;
return (
<View
accessibilityIgnoresInvertColors
style={[
styles.avatarWrapper,
{
borderRadius,
height: avatarSize,
width: avatarSize,
backgroundColor: IOColors.white,
padding: internalSpace
}
]}
>
<View
style={[styles.avatarInnerWrapper, { borderRadius: innerRadius }]}
>
<Image
accessibilityIgnoresInvertColors
source={source}
style={styles.avatarImage}
defaultSource={defaultSource ?? avatarSearchPlaceholder}
/>
</View>
</View>
);
}
);