2020-12-17 09:44:03 +01:00
|
|
|
import React, { useContext, useEffect, useRef } from 'react'
|
2020-11-22 00:46:23 +01:00
|
|
|
import { Animated, Dimensions, Image, StyleSheet } from 'react-native'
|
2020-12-17 09:44:03 +01:00
|
|
|
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 }) => {
|
2020-12-17 09:44:03 +01:00
|
|
|
const { accountState, accountDispatch } = useContext(AccountContext)
|
|
|
|
|
2020-11-22 00:46:23 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (uri) {
|
|
|
|
if (uri.includes('/headers/original/missing.png')) {
|
2020-12-17 09:44:03 +01:00
|
|
|
animateNewSize(accountState.headerRatio)
|
2020-11-22 00:46:23 +01:00
|
|
|
} else {
|
|
|
|
Image.getSize(
|
|
|
|
uri,
|
|
|
|
(width, height) => {
|
2020-12-17 09:44:03 +01:00
|
|
|
if (!limitHeight) {
|
|
|
|
accountDispatch({ type: 'headerRatio', payload: height / width })
|
|
|
|
}
|
|
|
|
animateNewSize(
|
|
|
|
limitHeight ? accountState.headerRatio : height / width
|
|
|
|
)
|
2020-11-22 00:46:23 +01:00
|
|
|
},
|
|
|
|
() => {
|
2020-12-17 09:44:03 +01:00
|
|
|
animateNewSize(accountState.headerRatio)
|
2020-11-22 00:46:23 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-17 09:44:03 +01:00
|
|
|
animateNewSize(accountState.headerRatio)
|
2020-11-22 00:46:23 +01:00
|
|
|
}
|
|
|
|
}, [uri])
|
|
|
|
|
|
|
|
const windowWidth = Dimensions.get('window').width
|
2020-12-17 09:44:03 +01:00
|
|
|
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
|