tooot/src/screens/Shared/Account/Header.tsx

68 lines
1.7 KiB
TypeScript
Raw Normal View History

import React, { useContext, useEffect, useRef } from 'react'
2020-11-22 00:46:23 +01:00
import { Animated, Dimensions, Image, StyleSheet } from 'react-native'
import { AccountContext } from '../Account'
2020-11-22 00:46:23 +01:00
export interface Props {
2020-12-15 00:01:48 +01:00
uri?: Mastodon.Account['header']
2020-11-22 00:46:23 +01:00
limitHeight?: boolean
}
const AccountHeader: React.FC<Props> = ({ uri, limitHeight = false }) => {
const { accountState, accountDispatch } = useContext(AccountContext)
2020-11-22 00:46:23 +01:00
useEffect(() => {
if (uri) {
if (uri.includes('/headers/original/missing.png')) {
animateNewSize(accountState.headerRatio)
2020-11-22 00:46:23 +01:00
} else {
Image.getSize(
uri,
(width, height) => {
if (!limitHeight) {
accountDispatch({ type: 'headerRatio', payload: height / width })
}
animateNewSize(
limitHeight ? accountState.headerRatio : height / width
)
2020-11-22 00:46:23 +01:00
},
() => {
animateNewSize(accountState.headerRatio)
2020-11-22 00:46:23 +01:00
}
)
}
} else {
animateNewSize(accountState.headerRatio)
2020-11-22 00:46:23 +01:00
}
}, [uri])
const windowWidth = Dimensions.get('window').width
const imageHeight = useRef(
new Animated.Value(windowWidth * accountState.headerRatio)
).current
2020-11-22 00:46:23 +01:00
const animateNewSize = (ratio: number) => {
Animated.timing(imageHeight, {
toValue: windowWidth * ratio,
duration: 350,
useNativeDriver: false
}).start()
}
return (
<Animated.View style={[styles.imageContainer, { height: imageHeight }]}>
<Image source={{ uri: uri }} style={styles.image} />
</Animated.View>
)
}
const styles = StyleSheet.create({
imageContainer: {
backgroundColor: 'lightgray'
},
image: {
width: '100%',
height: '100%'
}
})
export default AccountHeader