import * as React from "react"; import { Image, View } from "react-native"; import { Text } from "@/components/ui/Text"; import { useAssetUrl } from "@/lib/hooks"; import { cn } from "@/lib/utils"; interface AvatarProps { image?: string | null; name?: string | null; size?: number; className?: string; imageClassName?: string; fallbackClassName?: string; } function isExternalUrl(url: string) { return url.startsWith("http://") || url.startsWith("https://"); } export function Avatar({ image, name, size = 40, className, imageClassName, fallbackClassName, }: AvatarProps) { const [imageError, setImageError] = React.useState(false); const assetUrl = useAssetUrl(image ?? ""); const imageUrl = React.useMemo(() => { if (!image) return null; return isExternalUrl(image) ? { uri: image, } : assetUrl; }, [image]); React.useEffect(() => { setImageError(false); }, [image]); const initials = React.useMemo(() => { if (!name) return "U"; return name.charAt(0).toUpperCase(); }, [name]); const showFallback = !imageUrl || imageError; return ( {showFallback ? ( {initials} ) : ( setImageError(true)} /> )} ); }