tooot/src/screens/Shared/Account.tsx

152 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-12-21 21:47:15 +01:00
import React, { useEffect, useReducer, useRef, useState } from 'react'
2020-12-26 00:53:49 +01:00
import { Animated, ScrollView } from 'react-native'
2020-10-26 00:27:53 +01:00
import { useQuery } from 'react-query'
2020-12-13 14:04:25 +01:00
import { accountFetch } from '@utils/fetches/accountFetch'
import AccountToots from '@screens/Shared/Account/Toots'
import AccountHeader from '@screens/Shared/Account/Header'
import AccountInformation from '@screens/Shared/Account/Information'
import AccountNav from './Account/Nav'
import AccountSegmentedControl from './Account/SegmentedControl'
2020-12-21 21:47:15 +01:00
import { HeaderRight } from '@root/components/Header'
import BottomSheet from '@root/components/BottomSheet'
import { useSelector } from 'react-redux'
2020-12-21 22:08:29 +01:00
import { getLocalAccountId } from '@root/utils/slices/instancesSlice'
2020-12-21 21:47:15 +01:00
import HeaderDefaultActionsAccount from '@root/components/Timelines/Timeline/Shared/HeaderDefault/ActionsAccount'
2020-10-28 00:02:37 +01:00
// Moved account example: https://m.cmx.im/web/accounts/27812
2020-10-31 21:04:46 +01:00
export interface Props {
route: {
params: {
account: Pick<Mastodon.Account, 'id' | 'username' | 'acct' | 'url'>
2020-10-31 21:04:46 +01:00
}
}
2020-12-21 21:47:15 +01:00
navigation: any
2020-10-31 21:04:46 +01:00
}
export type AccountState = {
headerRatio: number
informationLayout?: {
y: number
height: number
}
segmentedIndex: number
}
export type AccountAction =
| {
type: 'headerRatio'
payload: AccountState['headerRatio']
}
| {
type: 'informationLayout'
payload: AccountState['informationLayout']
}
| {
type: 'segmentedIndex'
payload: AccountState['segmentedIndex']
}
const AccountInitialState: AccountState = {
headerRatio: 0.4,
informationLayout: { height: 0, y: 100 },
segmentedIndex: 0
}
const accountReducer = (
state: AccountState,
action: AccountAction
): AccountState => {
switch (action.type) {
case 'headerRatio':
return { ...state, headerRatio: action.payload }
case 'informationLayout':
return { ...state, informationLayout: action.payload }
case 'segmentedIndex':
return { ...state, segmentedIndex: action.payload }
default:
throw new Error('Unexpected action')
}
}
2020-11-21 13:19:05 +01:00
const ScreenSharedAccount: React.FC<Props> = ({
2020-10-26 00:27:53 +01:00
route: {
2020-12-21 21:47:15 +01:00
params: { account }
},
navigation
2020-10-31 21:04:46 +01:00
}) => {
2020-12-21 21:47:15 +01:00
const localAccountId = useSelector(getLocalAccountId)
const { data } = useQuery(['Account', { id: account.id }], accountFetch)
const scrollY = useRef(new Animated.Value(0)).current
const [accountState, accountDispatch] = useReducer(
accountReducer,
AccountInitialState
)
2020-10-26 00:27:53 +01:00
2020-12-21 21:47:15 +01:00
const [modalVisible, setBottomSheetVisible] = useState(false)
useEffect(() => {
const updateHeaderRight = () =>
navigation.setOptions({
headerRight: () => (
<HeaderRight
2020-12-26 23:27:53 +01:00
content='more-horizontal'
2020-12-21 21:47:15 +01:00
onPress={() => setBottomSheetVisible(true)}
/>
)
})
return updateHeaderRight()
}, [])
2020-11-22 00:46:23 +01:00
return (
2020-12-18 00:00:45 +01:00
<>
<AccountNav
accountState={accountState}
scrollY={scrollY}
account={data}
/>
2020-12-21 22:58:53 +01:00
{accountState.informationLayout?.height &&
accountState.informationLayout.y ? (
<AccountSegmentedControl
accountState={accountState}
accountDispatch={accountDispatch}
scrollY={scrollY}
/>
) : null}
<ScrollView
2021-01-01 16:48:16 +01:00
scrollEventThrottle={16}
showsVerticalScrollIndicator={false}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: scrollY } } }],
{ useNativeDriver: false }
)}
>
2020-12-18 00:00:45 +01:00
<AccountHeader
accountState={accountState}
accountDispatch={accountDispatch}
account={data}
/>
<AccountInformation accountDispatch={accountDispatch} account={data} />
<AccountToots
accountState={accountState}
accountDispatch={accountDispatch}
2020-12-21 21:47:15 +01:00
id={account.id}
2020-12-18 00:00:45 +01:00
/>
</ScrollView>
2020-12-21 21:47:15 +01:00
<BottomSheet
visible={modalVisible}
handleDismiss={() => setBottomSheetVisible(false)}
>
{/* 添加到列表 */}
{localAccountId !== account.id && (
<HeaderDefaultActionsAccount
account={account}
setBottomSheetVisible={setBottomSheetVisible}
/>
)}
</BottomSheet>
2020-12-18 00:00:45 +01:00
</>
2020-10-26 00:27:53 +01:00
)
}
2020-10-28 00:02:37 +01:00
2020-11-21 13:19:05 +01:00
export default ScreenSharedAccount