From b067b9bdb13419c558ab92177f6dcc0b04877293 Mon Sep 17 00:00:00 2001 From: xmflsct Date: Tue, 3 Jan 2023 00:10:44 +0100 Subject: [PATCH] Properly clean and reset navigators --- src/screens/Tabs/Local/Root.tsx | 2 - src/screens/Tabs/Local/index.tsx | 2 + src/screens/Tabs/Public/Root.tsx | 3 -- src/screens/Tabs/Public/index.tsx | 2 + src/screens/Tabs/Shared/Toot.tsx | 43 ++++++++----------- src/utils/navigation/usePopToTop.ts | 6 +-- .../timeline/updateStatusProperty.ts | 4 -- src/utils/storage/actions.ts | 1 + 8 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/screens/Tabs/Local/Root.tsx b/src/screens/Tabs/Local/Root.tsx index 7141ecb2..87426bea 100644 --- a/src/screens/Tabs/Local/Root.tsx +++ b/src/screens/Tabs/Local/Root.tsx @@ -178,8 +178,6 @@ const Root: React.FC() const TabLocal: React.FC = () => { + usePopToTop() return ( diff --git a/src/screens/Tabs/Public/Root.tsx b/src/screens/Tabs/Public/Root.tsx index 613e818b..5f2f3328 100644 --- a/src/screens/Tabs/Public/Root.tsx +++ b/src/screens/Tabs/Public/Root.tsx @@ -4,7 +4,6 @@ import TimelineDefault from '@components/Timeline/Default' import SegmentedControl from '@react-native-community/segmented-control' import { NativeStackScreenProps } from '@react-navigation/native-stack' import { TabPublicStackParamList } from '@utils/navigation/navigators' -import usePopToTop from '@utils/navigation/usePopToTop' import { QueryKeyTimeline } from '@utils/queryHooks/timeline' import { getGlobalStorage, setGlobalStorage } from '@utils/storage/actions' import { StorageGlobal } from '@utils/storage/global' @@ -75,8 +74,6 @@ const Root: React.FC() const TabPublic: React.FC = () => { + usePopToTop() return ( diff --git a/src/screens/Tabs/Shared/Toot.tsx b/src/screens/Tabs/Shared/Toot.tsx index 29887ac5..aa15d0ae 100644 --- a/src/screens/Tabs/Shared/Toot.tsx +++ b/src/screens/Tabs/Shared/Toot.tsx @@ -145,35 +145,26 @@ const TabSharedToot: React.FC> = ({ useQuery( queryKey.remote, async () => { - let context: - | { - ancestors: Mastodon.Status[] - descendants: Mastodon.Status[] - } - | undefined + const domain = getHost(toot.url || toot.uri) + if (!domain?.length) { + return Promise.reject('Cannot parse remote doamin') + } + const id = (toot.url || toot.uri).match(new RegExp(/\/([0-9]+)$/))?.[1] + if (!id?.length) { + return Promise.reject('Cannot parse remote toot id') + } - try { - const domain = getHost(toot.url || toot.uri) - if (!domain?.length) { - throw new Error() - } - const id = (toot.url || toot.uri).match(new RegExp(/\/([0-9]+)$/))?.[1] - if (!id?.length) { - throw new Error() - } - - context = await apiGeneral<{ - ancestors: Mastodon.Status[] - descendants: Mastodon.Status[] - }>({ - method: 'get', - domain, - url: `api/v1/statuses/${id}/context` - }).then(res => res.body) - } catch {} + const context = await apiGeneral<{ + ancestors: Mastodon.Status[] + descendants: Mastodon.Status[] + }>({ + method: 'get', + domain, + url: `api/v1/statuses/${id}/context` + }).then(res => res.body) if (!context) { - throw new Error() + return Promise.reject('Cannot retrieve remote context') } const statuses: (Mastodon.Status & { _level?: number })[] = [ diff --git a/src/utils/navigation/usePopToTop.ts b/src/utils/navigation/usePopToTop.ts index 43d89118..d16ccc49 100644 --- a/src/utils/navigation/usePopToTop.ts +++ b/src/utils/navigation/usePopToTop.ts @@ -1,4 +1,4 @@ -import { useNavigation } from '@react-navigation/native' +import { StackActions, useFocusEffect, useNavigation } from '@react-navigation/native' import { useGlobalStorage } from '@utils/storage/actions' import { useEffect } from 'react' @@ -8,8 +8,8 @@ const usePopToTop = () => { const navigation = useNavigation() const [accountActive] = useGlobalStorage.string('account.active') - return useEffect(() => { - // navigation.dispatch(StackActions.popToTop()) + useEffect(() => { + navigation.dispatch(StackActions.popToTop()) }, [accountActive]) } diff --git a/src/utils/queryHooks/timeline/updateStatusProperty.ts b/src/utils/queryHooks/timeline/updateStatusProperty.ts index 86fc60e4..62a4c713 100644 --- a/src/utils/queryHooks/timeline/updateStatusProperty.ts +++ b/src/utils/queryHooks/timeline/updateStatusProperty.ts @@ -13,7 +13,6 @@ const updateStatusProperty = ({ if (!key) continue queryClient.setQueryData | undefined>(key, old => { - console.log('key', key) if (old) { let foundToot: Mastodon.Status | undefined = undefined old.pages = old.pages.map(page => { @@ -42,7 +41,6 @@ const updateStatusProperty = ({ if (foundToot) { const toot = foundToot as Mastodon.Status - console.log('updating', toot.id) enum MapPropertyToCount { favourited = 'favourites_count', reblogged = 'reblogs_count' @@ -53,9 +51,7 @@ const updateStatusProperty = ({ toot.poll = poll break default: - console.log('11', toot[payload.type]) toot[payload.type] = payload.to - console.log('22', toot[payload.type]) switch (payload.type) { case 'favourited': case 'reblogged': diff --git a/src/utils/storage/actions.ts b/src/utils/storage/actions.ts index ef7048bc..4ab0388c 100644 --- a/src/utils/storage/actions.ts +++ b/src/utils/storage/actions.ts @@ -212,6 +212,7 @@ export const generateAccountKey = ({ export const setAccount = async (account: string) => { storage.account = new MMKV({ id: account }) setGlobalStorage('account.active', account) + await queryClient.resetQueries() queryClient.clear() }