feat(mobile): add ErrorBoundary to ExpoImage

This commit is contained in:
DIYgod
2025-04-12 10:53:57 +08:00
parent 75778fa0b6
commit ed7a6acb39
+35 -17
View File
@@ -5,8 +5,12 @@ import type {
} from "expo-image"
import { Image as ExpoImage } from "expo-image"
import { forwardRef, useCallback, useMemo, useState } from "react"
import type { ViewStyle } from "react-native"
import { Text, View } from "react-native"
import { useColor } from "react-native-uikit-colors"
import { ErrorBoundary } from "@/src/components/common/ErrorBoundary"
import { getAllSources } from "./utils"
export type ImageProps = Omit<ExpoImageProps, "source"> & {
@@ -57,23 +61,37 @@ export const Image = forwardRef<ExpoImage, ImageProps>(
}
return (
<ExpoImage
recyclingKey={source?.uri}
{...rest}
source={isError ? safeSource : proxiesSafeSource}
onError={onError}
onLoad={onLoad}
placeholder={{
blurhash,
...(typeof rest.placeholder === "object" && { ...rest.placeholder }),
}}
style={{
aspectRatio,
...(typeof rest.style === "object" && { ...rest.style }),
...(isLoading && { backgroundColor }),
}}
ref={ref}
/>
<ErrorBoundary
fallbackRender={({ error }) => (
<View
style={{
aspectRatio,
...(typeof rest.style === "object" && ({ ...rest.style } as ViewStyle)),
backgroundColor,
}}
>
<Text className="text-red text-center text-xs">{error.message}</Text>
</View>
)}
>
<ExpoImage
recyclingKey={source?.uri}
{...rest}
source={isError ? safeSource : proxiesSafeSource}
onError={onError}
onLoad={onLoad}
placeholder={{
blurhash,
...(typeof rest.placeholder === "object" && { ...rest.placeholder }),
}}
style={{
aspectRatio,
...(typeof rest.style === "object" && { ...rest.style }),
...(isLoading && { backgroundColor }),
}}
ref={ref}
/>
</ErrorBoundary>
)
},
)