diff --git a/App.tsx b/App.tsx index 0f2a51c6..67a67c16 100644 --- a/App.tsx +++ b/App.tsx @@ -7,6 +7,7 @@ import { PersistGate } from 'redux-persist/integration/react' import { Index } from '@root/Index' import { persistor, store } from '@root/store' import ThemeManager from '@utils/styles/ThemeManager' +import { resetLocal, updateLocal } from '@root/utils/slices/instancesSlice' const queryClient = new QueryClient() @@ -49,7 +50,19 @@ const App: React.FC = () => { setAppLoaded(true)} + onBeforeLift={async () => { + const localUrl = store.getState().instances.local.url + const localToken = store.getState().instances.local.token + if (localUrl && localToken) { + const dispatchStatus = await store.dispatch( + updateLocal({ url: localUrl, token: localToken }) + ) + if (dispatchStatus.type.includes('/rejected')) { + store.dispatch(resetLocal()) + } + } + setAppLoaded(true) + }} > {bootstrapped => { if (bootstrapped) { diff --git a/src/@types/mastodon.d.ts b/src/@types/mastodon.d.ts index e28a7ae7..eeb2416d 100644 --- a/src/@types/mastodon.d.ts +++ b/src/@types/mastodon.d.ts @@ -31,6 +31,32 @@ declare namespace Mastodon { source: Source } + type Announcement = { + // Base + id: string + text: string + published: boolean + all_day: boolean + created_at: string + updated_at: string + read: boolean + reactions: AnnouncementReaction[] + + // Others + scheduled_at?: string + starts_at?: string + ends_at?: string + } + + type AnnouncementReaction = { + // Base + name: string + count: number + me: boolean + url: string + static_url: string + } + type Application = { // Base name: string diff --git a/src/Index.tsx b/src/Index.tsx index 7612e74d..320fa505 100644 --- a/src/Index.tsx +++ b/src/Index.tsx @@ -3,7 +3,7 @@ import { createBottomTabNavigator } from '@react-navigation/bottom-tabs' import { NavigationContainer } from '@react-navigation/native' import { enableScreens } from 'react-native-screens' -import React, { useEffect } from 'react' +import React from 'react' import { StatusBar } from 'react-native' import Toast from 'react-native-toast-message' import { Feather } from '@expo/vector-icons' @@ -18,12 +18,8 @@ import { useTheme } from '@utils/styles/ThemeManager' import getCurrentTab from '@utils/getCurrentTab' import { toastConfig } from '@components/toast' import { useTranslation } from 'react-i18next' -import { useDispatch, useSelector } from 'react-redux' -import { - getLocalToken, - getLocalUrl, - updateLocal -} from './utils/slices/instancesSlice' +import { useSelector } from 'react-redux' +import { getLocalUrl } from './utils/slices/instancesSlice' enableScreens() const Tab = createBottomTabNavigator() @@ -37,9 +33,7 @@ export type RootStackParamList = { } export const Index: React.FC = () => { - const dispatch = useDispatch() const localInstance = useSelector(getLocalUrl) - const localToken = useSelector(getLocalToken) const { i18n } = useTranslation() const { mode, theme } = useTheme() enum barStyle { @@ -47,12 +41,6 @@ export const Index: React.FC = () => { dark = 'light-content' } - useEffect(() => { - if (localInstance && localToken) { - dispatch(updateLocal({ url: localInstance, token: localToken })) - } - }, []) - return ( <> diff --git a/src/utils/slices/instancesSlice.ts b/src/utils/slices/instancesSlice.ts index 3e0c3f7c..ccef4e73 100644 --- a/src/utils/slices/instancesSlice.ts +++ b/src/utils/slices/instancesSlice.ts @@ -82,7 +82,11 @@ const instancesSlice = createSlice({ url: 'm.cmx.im' } } as InstancesState, - reducers: {}, + reducers: { + resetLocal: state => { + state.local = initialStateLocal + } + }, extraReducers: builder => { builder.addCase(updateLocal.fulfilled, (state, action) => { state.local = action.payload @@ -98,4 +102,6 @@ export const getLocalAccountId = (state: RootState) => export const getLocalAccountPreferences = (state: RootState) => state.instances.local.account.preferences +export const { resetLocal } = instancesSlice.actions + export default instancesSlice.reducer