import React, {useState, useEffect} from 'react' import { Image, ImageStyle, LayoutChangeEvent, StyleProp, StyleSheet, TouchableOpacity, View, ViewStyle, } from 'react-native' import {Text} from '../text/Text' import {useTheme} from '../../../lib/ThemeContext' import {usePalette} from '../../../lib/hooks/usePalette' import {DELAY_PRESS_IN} from './constants' const MAX_HEIGHT = 300 interface Dim { width: number height: number } export function AutoSizedImage({ uri, onPress, onLongPress, style, containerStyle, }: { uri: string onPress?: () => void onLongPress?: () => void style?: StyleProp containerStyle?: StyleProp }) { const theme = useTheme() const errPal = usePalette('error') const [error, setError] = useState('') const [imgInfo, setImgInfo] = useState() const [containerInfo, setContainerInfo] = useState() useEffect(() => { let aborted = false if (!imgInfo) { Image.getSize( uri, (width: number, height: number) => { if (!aborted) { setImgInfo({width, height}) } }, (err: any) => { if (!aborted) { setError(String(err)) } }, ) } return () => { aborted = true } }, [uri, imgInfo]) const onLayout = (evt: LayoutChangeEvent) => { setContainerInfo({ width: evt.nativeEvent.layout.width, height: evt.nativeEvent.layout.height, }) } let calculatedStyle: StyleProp | undefined if (imgInfo && containerInfo) { // imgInfo.height / imgInfo.width = x / containerInfo.width // x = imgInfo.height / imgInfo.width * containerInfo.width calculatedStyle = { height: Math.min( MAX_HEIGHT, (imgInfo.height / imgInfo.width) * containerInfo.width, ), } } return ( {error ? ( {error} ) : calculatedStyle ? ( ) : ( )} ) } const styles = StyleSheet.create({ placeholder: { width: '100%', aspectRatio: 1, }, errorContainer: { paddingHorizontal: 12, paddingVertical: 8, }, container: { overflow: 'hidden', }, })