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

118 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
import React, { createRef, useCallback, useContext, useEffect } from 'react'
2020-12-27 16:25:29 +01:00
import { Animated, StyleSheet, View } from 'react-native'
import AccountInformationAccount from './Information/Account'
import AccountInformationActions from './Information/Actions'
2021-01-07 19:13:09 +01:00
import AccountInformationAvatar from './Information/Avatar'
import AccountInformationCreated from './Information/Created'
2020-12-27 16:25:29 +01:00
import AccountInformationFields from './Information/Fields'
2021-01-07 19:13:09 +01:00
import AccountInformationName from './Information/Name'
2020-12-27 16:25:29 +01:00
import AccountInformationNotes from './Information/Notes'
2021-01-07 19:13:09 +01:00
import AccountInformationStats from './Information/Stats'
import AccountInformationSwitch from './Information/Switch'
import AccountContext from './utils/createContext'
2020-11-22 00:46:23 +01:00
export interface Props {
account: Mastodon.Account | undefined
2021-01-07 19:13:09 +01:00
ownAccount?: boolean
2020-11-22 00:46:23 +01:00
}
2020-12-21 22:58:53 +01:00
const AccountInformation: React.FC<Props> = ({
account,
2021-01-07 19:13:09 +01:00
ownAccount = false
2020-12-21 22:58:53 +01:00
}) => {
const { accountDispatch } = useContext(AccountContext)
const shimmerAvatarRef = createRef<any>()
const shimmerNameRef = createRef<any>()
const shimmerAccountRef = createRef<any>()
const shimmerCreatedRef = createRef<any>()
2020-12-27 16:25:29 +01:00
const shimmerStatsRef = createRef<any>()
2020-12-14 23:44:57 +01:00
useEffect(() => {
const informationAnimated = Animated.stagger(400, [
Animated.parallel([
2020-12-27 16:25:29 +01:00
shimmerAvatarRef.current?.getAnimated(),
shimmerNameRef.current?.getAnimated(),
shimmerAccountRef.current?.getAnimated(),
shimmerCreatedRef.current?.getAnimated(),
shimmerStatsRef.current?.ref1.getAnimated(),
shimmerStatsRef.current?.ref2.getAnimated(),
shimmerStatsRef.current?.ref3.getAnimated()
2020-12-14 23:44:57 +01:00
])
])
Animated.loop(informationAnimated).start()
}, [])
2020-12-27 16:25:29 +01:00
const onLayout = useCallback(
({ nativeEvent }) =>
accountDispatch &&
accountDispatch({
type: 'informationLayout',
payload: {
y: nativeEvent.layout.y,
height: nativeEvent.layout.height
2020-12-21 21:47:15 +01:00
}
2020-12-27 16:25:29 +01:00
}),
[]
2020-12-21 21:47:15 +01:00
)
2020-11-22 00:46:23 +01:00
return (
2020-12-27 16:25:29 +01:00
<View style={styles.base} onLayout={onLayout}>
2020-11-22 00:46:23 +01:00
{/* <Text>Moved or not: {account.moved}</Text> */}
2020-12-21 21:47:15 +01:00
<View style={styles.avatarAndActions}>
2020-12-27 16:25:29 +01:00
<AccountInformationAvatar ref={shimmerAvatarRef} account={account} />
2021-01-07 19:13:09 +01:00
<View style={styles.actions}>
{ownAccount ? (
<AccountInformationSwitch />
) : (
<AccountInformationActions account={account} />
)}
</View>
2020-12-21 21:47:15 +01:00
</View>
2020-11-22 00:46:23 +01:00
2020-12-27 16:25:29 +01:00
<AccountInformationName ref={shimmerNameRef} account={account} />
2020-12-14 23:44:57 +01:00
2020-12-27 16:25:29 +01:00
<AccountInformationAccount ref={shimmerAccountRef} account={account} />
2020-11-22 00:46:23 +01:00
2021-01-13 01:03:46 +01:00
{!ownAccount ? (
<>
{account?.fields && account.fields.length > 0 ? (
<AccountInformationFields account={account} />
) : null}
{account?.note &&
account.note.length > 0 &&
account.note !== '<p></p>' ? (
// Empty notes might generate empty p tag
<AccountInformationNotes account={account} />
) : null}
<AccountInformationCreated
ref={shimmerCreatedRef}
account={account}
/>
</>
) : null}
2020-11-23 00:07:32 +01:00
2020-12-27 16:25:29 +01:00
<AccountInformationStats ref={shimmerStatsRef} account={account} />
2020-11-22 00:46:23 +01:00
</View>
)
}
const styles = StyleSheet.create({
2020-12-21 21:47:15 +01:00
base: {
marginTop: -StyleConstants.Spacing.Global.PagePadding * 3,
2020-11-30 00:24:53 +01:00
padding: StyleConstants.Spacing.Global.PagePadding
2020-11-23 00:07:32 +01:00
},
2020-12-21 21:47:15 +01:00
avatarAndActions: {
flexDirection: 'row',
justifyContent: 'space-between'
2021-01-07 19:13:09 +01:00
},
actions: {
alignSelf: 'flex-end',
flexDirection: 'row'
2020-11-22 00:46:23 +01:00
}
})
export default React.memo(
AccountInformation,
(_, next) => next.account === undefined
)