1
+ import { useCallback } from 'react'
2
+
1
3
import { useTrack } from '@audius/common/api'
2
4
import { useImageSize } from '@audius/common/hooks'
3
5
import type { SquareSizes , ID } from '@audius/common/models'
@@ -49,7 +51,7 @@ export const useTrackImage = ({
49
51
return track . artwork
50
52
}
51
53
} )
52
- const image = useImageSize ( {
54
+ const { imageUrl , onError } = useImageSize ( {
53
55
artwork,
54
56
targetSize : size ,
55
57
defaultImage : '' ,
@@ -58,10 +60,11 @@ export const useTrackImage = ({
58
60
}
59
61
} )
60
62
61
- if ( image === '' ) {
63
+ if ( imageUrl === '' ) {
62
64
return {
63
65
source : imageEmpty ,
64
- isFallbackImage : true
66
+ isFallbackImage : true ,
67
+ onError
65
68
}
66
69
}
67
70
@@ -73,13 +76,15 @@ export const useTrackImage = ({
73
76
return {
74
77
// @ts -ignore
75
78
source : primitiveToImageSource ( artwork . url ) ,
76
- isFallbackImage : false
79
+ isFallbackImage : false ,
80
+ onError
77
81
}
78
82
}
79
83
80
84
return {
81
- source : primitiveToImageSource ( image ) ,
82
- isFallbackImage : false
85
+ source : primitiveToImageSource ( imageUrl ) ,
86
+ isFallbackImage : false ,
87
+ onError
83
88
}
84
89
}
85
90
@@ -89,6 +94,7 @@ type TrackImageProps = {
89
94
style ?: FastImageProps [ 'style' ]
90
95
borderRadius ?: CornerRadiusOptions
91
96
onLoad ?: FastImageProps [ 'onLoad' ]
97
+ onError ?: FastImageProps [ 'onError' ]
92
98
children ?: React . ReactNode
93
99
}
94
100
@@ -106,10 +112,21 @@ export const TrackImage = (props: TrackImageProps) => {
106
112
const trackImageSource = useTrackImage ( { trackId, size } )
107
113
const { cornerRadius } = useTheme ( )
108
114
const { skeleton } = useThemeColors ( )
109
- const { source : loadedSource , isFallbackImage } = trackImageSource
115
+ const { source : loadedSource , isFallbackImage, onError } = trackImageSource
110
116
111
117
const source = loadedSource ?? localTrackImageUri
112
118
119
+ const handleError = useCallback ( ( ) => {
120
+ if (
121
+ source &&
122
+ typeof source === 'object' &&
123
+ 'uri' in source &&
124
+ typeof source . uri === 'string'
125
+ ) {
126
+ onError ( source . uri )
127
+ }
128
+ } , [ source , onError ] )
129
+
113
130
return (
114
131
< FastImage
115
132
{ ...other }
@@ -120,7 +137,8 @@ export const TrackImage = (props: TrackImageProps) => {
120
137
} ,
121
138
style
122
139
] }
123
- source = { source ?? { uri : '' } }
140
+ source = { source }
141
+ onError = { handleError }
124
142
onLoad = { onLoad }
125
143
/>
126
144
)
0 commit comments