import { useTailwind } from '@/hooks'; import { useCallback, useState } from 'react'; import { Image as RNImage, ImageProps as RNImageProps } from 'react-native'; import FastImage, { FastImageProps } from 'react-native-fast-image'; import { Style } from 'twrnc/dist/esm/types'; type FactoryImageProps = C extends true ? FastImageProps : RNImageProps; export type ImageProps = Omit, 'style'> & { cached?: C; style?: Style; }; export default function Image({ style, cached, onLoad, ...props }: ImageProps) { const tw = useTailwind(); const [hasSizes, setHasSizes] = useState(false); const imageStyle = tw.style(hasSizes ? 'bg-transparent' : 'bg-zinc-300 dark:bg-zinc-700', style); const handleOnLoad = useCallback( ({ nativeEvent }: Parameters>[0]) => { // @ts-ignore onLoad?.({ nativeEvent }); if (hasSizes) return; const source = 'source' in nativeEvent ? nativeEvent.source : nativeEvent; setHasSizes(!!source.width && !!source.height); }, [hasSizes, onLoad], ); if (cached) return ( )} onLoad={handleOnLoad} style={imageStyle} /> ); return ( )} progressiveRenderingEnabled onLoad={handleOnLoad} style={imageStyle} /> ); }