From dd4a9671bab86086759869212a6a95be3604fecd Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Sat, 24 Apr 2021 15:03:17 +0200 Subject: [PATCH] Improve account fetching --- src/screens/Tabs/Me/Root.tsx | 21 ++++++-- src/screens/Tabs/Me/Root/MyInfo.tsx | 25 ++-------- .../Shared/Account/Information/Account.tsx | 2 +- .../Shared/Account/Information/Created.tsx | 2 +- .../Tabs/Shared/Account/Information/Name.tsx | 50 +++++++++---------- 5 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/screens/Tabs/Me/Root.tsx b/src/screens/Tabs/Me/Root.tsx index 52050a34..b73915a0 100644 --- a/src/screens/Tabs/Me/Root.tsx +++ b/src/screens/Tabs/Me/Root.tsx @@ -8,8 +8,12 @@ import AccountNav from '@screens/Tabs/Shared/Account/Nav' import AccountContext from '@screens/Tabs/Shared/Account/utils/createContext' import accountInitialState from '@screens/Tabs/Shared/Account/utils/initialState' import accountReducer from '@screens/Tabs/Shared/Account/utils/reducer' -import { getInstanceActive } from '@utils/slices/instancesSlice' -import React, { useReducer, useRef, useState } from 'react' +import { useAccountQuery } from '@utils/queryHooks/account' +import { + getInstanceAccount, + getInstanceActive +} from '@utils/slices/instancesSlice' +import React, { useReducer, useRef } from 'react' import Animated, { useAnimatedScrollHandler, useSharedValue @@ -18,12 +22,19 @@ import { useSelector } from 'react-redux' const ScreenMeRoot: React.FC = () => { const instanceActive = useSelector(getInstanceActive) + const instanceAccount = useSelector( + getInstanceAccount, + (prev, next) => prev?.id === next?.id + ) + const { data } = useAccountQuery({ + // @ts-ignore + id: instanceAccount?.id, + options: { enabled: instanceActive !== -1, keepPreviousData: false } + }) const scrollRef = useRef(null) useScrollToTop(scrollRef) - const [data, setData] = useState() - const [accountState, accountDispatch] = useReducer( accountReducer, accountInitialState @@ -46,7 +57,7 @@ const ScreenMeRoot: React.FC = () => { scrollEventThrottle={16} > {instanceActive !== -1 ? ( - + ) : ( )} diff --git a/src/screens/Tabs/Me/Root/MyInfo.tsx b/src/screens/Tabs/Me/Root/MyInfo.tsx index 1ab72177..ce5e2b57 100644 --- a/src/screens/Tabs/Me/Root/MyInfo.tsx +++ b/src/screens/Tabs/Me/Root/MyInfo.tsx @@ -1,31 +1,16 @@ import AccountHeader from '@screens/Tabs/Shared/Account/Header' import AccountInformation from '@screens/Tabs/Shared/Account/Information' -import { useAccountQuery } from '@utils/queryHooks/account' -import { getInstanceAccount } from '@utils/slices/instancesSlice' -import React, { useEffect } from 'react' -import { useSelector } from 'react-redux' +import React from 'react' export interface Props { - setData: React.Dispatch> + account: Mastodon.Account | undefined } -const MyInfo: React.FC = ({ setData }) => { - const instanceAccount = useSelector( - getInstanceAccount, - (prev, next) => prev?.id === next?.id - ) - const { data } = useAccountQuery({ id: instanceAccount!.id }) - - useEffect(() => { - if (data) { - setData(data) - } - }, [data]) - +const MyInfo: React.FC = ({ account }) => { return ( <> - - + + ) } diff --git a/src/screens/Tabs/Shared/Account/Information/Account.tsx b/src/screens/Tabs/Shared/Account/Information/Account.tsx index d3c1e036..2e604217 100644 --- a/src/screens/Tabs/Shared/Account/Information/Account.tsx +++ b/src/screens/Tabs/Shared/Account/Information/Account.tsx @@ -88,7 +88,7 @@ const AccountInformationAccount: React.FC = ({ account, myInfo }) => { } else { return ( = ({ account }) => { } else { return ( = ({ account }) => { } }, [account?.moved]) - if (account) { - return ( - - - - - {movedContent} - - ) - } else { - return ( - - ) - } + return ( + + {account ? ( + <> + + + + {movedContent} + + ) : ( + + )} + + ) } const styles = StyleSheet.create({