-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathUserImage.tsx
More file actions
92 lines (80 loc) · 2.24 KB
/
Copy pathUserImage.tsx
File metadata and controls
92 lines (80 loc) · 2.24 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
import { useUser } from '@audius/common/api'
import { useImageSize } from '@audius/common/hooks'
import type { SquareSizes, ID } from '@audius/common/models'
import { pick } from 'lodash'
import { Image, preload } from '@audius/harmony-native'
import type { ImageProps } from '@audius/harmony-native'
import profilePicEmpty from 'app/assets/images/imageProfilePicEmpty2X.png'
import { primitiveToImageSource } from './primitiveToImageSource'
type UseUserImageOptions = {
userId: ID | null | undefined
size: SquareSizes
}
export const useProfilePicture = ({
userId,
size
}: {
userId?: ID | null
size: SquareSizes
defaultImage?: string
}) => {
const { data: partialUser } = useUser(userId, {
select: (user) => pick(user, 'profile_picture', 'updatedProfilePicture')
})
const { profile_picture, updatedProfilePicture } = partialUser ?? {}
const {
imageUrl,
priorityLowResUrl,
onError: onImageError
} = useImageSize({
artwork: profile_picture,
targetSize: size,
defaultImage: '',
preloadImageFn: async (url: string) => {
await preload([{ uri: url }])
}
})
if (imageUrl === '') {
return {
source: profilePicEmpty,
isFallbackImage: true,
onError: onImageError
}
}
if (updatedProfilePicture) {
return {
source: primitiveToImageSource(updatedProfilePicture.url),
isFallbackImage: false,
onError: onImageError
}
}
return {
source: primitiveToImageSource(imageUrl),
priorityLowResSource: primitiveToImageSource(priorityLowResUrl),
isFallbackImage: false,
onError: onImageError
}
}
export type UserImageProps = UseUserImageOptions & Partial<ImageProps>
export const UserImage = (props: UserImageProps) => {
const { userId, size, onError, ...imageProps } = props
const {
source,
priorityLowResSource,
onError: onImageError
} = useProfilePicture({ userId, size })
const handleError = (error: { nativeEvent: { error: string } }) => {
if (source && typeof source === 'object' && 'uri' in source) {
onImageError?.(source.uri as string)
}
onError?.(error)
}
return (
<Image
{...imageProps}
source={source}
priorityLowResSource={priorityLowResSource}
onError={handleError}
/>
)
}