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

70 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-12-21 22:58:53 +01:00
import React, { Dispatch, useEffect, useState } from 'react'
import { Dimensions, Image, StyleSheet, View } from 'react-native'
2020-12-18 00:00:45 +01:00
import { AccountAction, AccountState } from '../Account'
2020-11-22 00:46:23 +01:00
export interface Props {
2020-12-18 00:00:45 +01:00
accountState: AccountState
accountDispatch?: Dispatch<AccountAction>
account?: Mastodon.Account
2020-11-22 00:46:23 +01:00
limitHeight?: boolean
}
2020-12-18 00:00:45 +01:00
const AccountHeader: React.FC<Props> = ({
accountState,
accountDispatch,
account,
limitHeight = false
}) => {
2020-12-21 22:58:53 +01:00
const [ratio, setRatio] = useState(accountState.headerRatio)
let isMounted = false
useEffect(() => {
isMounted = true
return () => {
isMounted = false
}
})
2020-11-22 00:46:23 +01:00
useEffect(() => {
2020-12-21 22:58:53 +01:00
if (
account?.header &&
!account.header.includes('/headers/original/missing.png')
) {
isMounted &&
Image.getSize(account.header, (width, height) => {
if (!limitHeight) {
accountDispatch &&
accountDispatch({
type: 'headerRatio',
payload: height / width
})
}
isMounted &&
setRatio(limitHeight ? accountState.headerRatio : height / width)
})
} else {
isMounted && setRatio(1 / 5)
2020-11-22 00:46:23 +01:00
}
}, [account, isMounted])
2020-12-18 00:00:45 +01:00
2020-11-22 00:46:23 +01:00
const windowWidth = Dimensions.get('window').width
return (
2020-12-21 22:58:53 +01:00
<View style={[styles.imageContainer, { height: windowWidth * ratio }]}>
<Image source={{ uri: account?.header }} style={styles.image} />
</View>
2020-11-22 00:46:23 +01:00
)
}
const styles = StyleSheet.create({
imageContainer: {
backgroundColor: 'lightgray'
},
image: {
width: '100%',
height: '100%'
}
})
export default AccountHeader