1+ import { useCallback } from 'react'
2+
13import { useTrack } from '@audius/common/api'
24import { useImageSize } from '@audius/common/hooks'
35import type { SquareSizes , ID } from '@audius/common/models'
@@ -49,7 +51,7 @@ export const useTrackImage = ({
4951 return track . artwork
5052 }
5153 } )
52- const image = useImageSize ( {
54+ const { imageUrl , onError } = useImageSize ( {
5355 artwork,
5456 targetSize : size ,
5557 defaultImage : '' ,
@@ -58,10 +60,11 @@ export const useTrackImage = ({
5860 }
5961 } )
6062
61- if ( image === '' ) {
63+ if ( imageUrl === '' ) {
6264 return {
6365 source : imageEmpty ,
64- isFallbackImage : true
66+ isFallbackImage : true ,
67+ onError
6568 }
6669 }
6770
@@ -73,13 +76,15 @@ export const useTrackImage = ({
7376 return {
7477 // @ts -ignore
7578 source : primitiveToImageSource ( artwork . url ) ,
76- isFallbackImage : false
79+ isFallbackImage : false ,
80+ onError
7781 }
7882 }
7983
8084 return {
81- source : primitiveToImageSource ( image ) ,
82- isFallbackImage : false
85+ source : primitiveToImageSource ( imageUrl ) ,
86+ isFallbackImage : false ,
87+ onError
8388 }
8489}
8590
@@ -89,6 +94,7 @@ type TrackImageProps = {
8994 style ?: FastImageProps [ 'style' ]
9095 borderRadius ?: CornerRadiusOptions
9196 onLoad ?: FastImageProps [ 'onLoad' ]
97+ onError ?: FastImageProps [ 'onError' ]
9298 children ?: React . ReactNode
9399}
94100
@@ -106,10 +112,21 @@ export const TrackImage = (props: TrackImageProps) => {
106112 const trackImageSource = useTrackImage ( { trackId, size } )
107113 const { cornerRadius } = useTheme ( )
108114 const { skeleton } = useThemeColors ( )
109- const { source : loadedSource , isFallbackImage } = trackImageSource
115+ const { source : loadedSource , isFallbackImage, onError } = trackImageSource
110116
111117 const source = loadedSource ?? localTrackImageUri
112118
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+
113130 return (
114131 < FastImage
115132 { ...other }
@@ -120,7 +137,8 @@ export const TrackImage = (props: TrackImageProps) => {
120137 } ,
121138 style
122139 ] }
123- source = { source ?? { uri : '' } }
140+ source = { source }
141+ onError = { handleError }
124142 onLoad = { onLoad }
125143 />
126144 )
0 commit comments