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

57 lines
1.4 KiB
TypeScript
Raw Normal View History

import { useTheme } from '@utils/styles/ThemeManager'
import React, { useContext, useEffect } from 'react'
import { Dimensions, Image } from 'react-native'
import Animated, {
useAnimatedStyle,
useSharedValue,
withTiming
} from 'react-native-reanimated'
import AccountContext from './utils/createContext'
2020-11-22 00:46:23 +01:00
export interface Props {
2020-12-18 00:00:45 +01:00
account?: Mastodon.Account
2020-11-22 00:46:23 +01:00
limitHeight?: boolean
}
const AccountHeader: React.FC<Props> = ({ account, limitHeight = false }) => {
const { accountState, accountDispatch } = useContext(AccountContext)
2020-12-27 16:25:29 +01:00
const { theme } = useTheme()
const height = useSharedValue(
Dimensions.get('screen').width * accountState.headerRatio
)
const styleHeight = useAnimatedStyle(() => {
return {
height: withTiming(height.value)
}
})
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')
) {
Image.getSize(account.header, (width, height) => {
if (!limitHeight) {
accountDispatch({
type: 'headerRatio',
payload: height / width
})
}
})
2020-11-22 00:46:23 +01:00
}
}, [account])
2020-11-22 00:46:23 +01:00
return (
<Animated.Image
source={{ uri: account?.header }}
style={[styleHeight, { backgroundColor: theme.disabled }]}
/>
2020-11-22 00:46:23 +01:00
)
}
export default React.memo(
AccountHeader,
(_, next) => next.account === undefined
)