2022-02-14 22:10:07 +01:00
|
|
|
import analytics from '@components/analytics'
|
2021-08-21 01:45:43 +02:00
|
|
|
import { HeaderLeft } from '@components/Header'
|
2022-02-01 22:28:12 +01:00
|
|
|
import { displayMessage, Message } from '@components/Message'
|
2021-05-12 15:40:55 +02:00
|
|
|
import navigationRef from '@helpers/navigationRef'
|
|
|
|
import { NavigationContainer } from '@react-navigation/native'
|
2021-08-21 01:45:43 +02:00
|
|
|
import { createNativeStackNavigator } from '@react-navigation/native-stack'
|
2021-01-30 01:29:15 +01:00
|
|
|
import ScreenActions from '@screens/Actions'
|
|
|
|
import ScreenAnnouncements from '@screens/Announcements'
|
|
|
|
import ScreenCompose from '@screens/Compose'
|
|
|
|
import ScreenImagesViewer from '@screens/ImagesViewer'
|
|
|
|
import ScreenTabs from '@screens/Tabs'
|
2022-01-30 22:51:03 +01:00
|
|
|
import initQuery from '@utils/initQuery'
|
2021-08-29 15:25:38 +02:00
|
|
|
import { RootStackParamList } from '@utils/navigation/navigators'
|
2021-03-04 01:03:53 +01:00
|
|
|
import pushUseConnect from '@utils/push/useConnect'
|
|
|
|
import pushUseReceive from '@utils/push/useReceive'
|
|
|
|
import pushUseRespond from '@utils/push/useRespond'
|
2021-02-11 23:42:13 +01:00
|
|
|
import { updatePreviousTab } from '@utils/slices/contextsSlice'
|
2022-02-13 22:14:16 +01:00
|
|
|
import { checkEmojis } from '@utils/slices/instances/checkEmojis'
|
2021-02-20 19:12:44 +01:00
|
|
|
import { updateAccountPreferences } from '@utils/slices/instances/updateAccountPreferences'
|
2021-11-15 22:34:43 +01:00
|
|
|
import { updateConfiguration } from '@utils/slices/instances/updateConfiguration'
|
2021-05-30 23:39:07 +02:00
|
|
|
import { updateFilters } from '@utils/slices/instances/updateFilters'
|
2022-01-30 22:51:03 +01:00
|
|
|
import { getInstanceActive, getInstances } from '@utils/slices/instancesSlice'
|
2021-01-30 01:29:15 +01:00
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
|
|
|
import { themes } from '@utils/styles/themes'
|
2022-01-29 16:37:58 +01:00
|
|
|
import * as Linking from 'expo-linking'
|
2021-02-27 16:33:54 +01:00
|
|
|
import { addScreenshotListener } from 'expo-screen-capture'
|
2022-01-29 16:37:58 +01:00
|
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
2021-01-30 01:29:15 +01:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-02-28 17:41:21 +01:00
|
|
|
import { Alert, Platform, StatusBar } from 'react-native'
|
2021-01-30 01:29:15 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux'
|
2021-02-27 16:33:54 +01:00
|
|
|
import * as Sentry from 'sentry-expo'
|
2021-01-30 01:29:15 +01:00
|
|
|
|
2021-08-29 15:25:38 +02:00
|
|
|
const Stack = createNativeStackNavigator<RootStackParamList>()
|
2021-01-30 01:29:15 +01:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
localCorrupt?: string
|
|
|
|
}
|
|
|
|
|
2021-02-08 23:19:55 +01:00
|
|
|
const Screens: React.FC<Props> = ({ localCorrupt }) => {
|
2021-03-28 23:31:10 +02:00
|
|
|
const { t } = useTranslation('screens')
|
2021-01-30 01:29:15 +01:00
|
|
|
const dispatch = useDispatch()
|
2021-02-20 19:12:44 +01:00
|
|
|
const instanceActive = useSelector(getInstanceActive)
|
2022-02-17 00:09:19 +01:00
|
|
|
const { colors, theme } = useTheme()
|
2021-01-30 01:29:15 +01:00
|
|
|
|
2021-03-21 13:15:43 +01:00
|
|
|
const routeRef = useRef<{ name?: string; params?: {} }>()
|
2021-01-30 01:29:15 +01:00
|
|
|
|
2021-03-04 01:03:53 +01:00
|
|
|
// Push hooks
|
|
|
|
const instances = useSelector(
|
|
|
|
getInstances,
|
|
|
|
(prev, next) => prev.length === next.length
|
|
|
|
)
|
2022-01-30 22:51:03 +01:00
|
|
|
pushUseConnect({ t, instances })
|
|
|
|
pushUseReceive({ instances })
|
|
|
|
pushUseRespond({ instances })
|
2021-02-28 17:41:21 +01:00
|
|
|
|
2021-02-05 01:13:57 +01:00
|
|
|
// Prevent screenshot alert
|
2021-02-27 16:33:54 +01:00
|
|
|
useEffect(() => {
|
|
|
|
const screenshotListener = addScreenshotListener(() =>
|
|
|
|
Alert.alert(t('screenshot.title'), t('screenshot.message'), [
|
|
|
|
{ text: t('screenshot.button'), style: 'destructive' }
|
|
|
|
])
|
|
|
|
)
|
|
|
|
Platform.select({ ios: screenshotListener })
|
|
|
|
return () => screenshotListener.remove()
|
|
|
|
}, [])
|
2021-02-05 01:13:57 +01:00
|
|
|
|
2021-01-30 01:29:15 +01:00
|
|
|
// On launch display login credentials corrupt information
|
|
|
|
useEffect(() => {
|
2021-02-11 23:42:13 +01:00
|
|
|
const showLocalCorrect = () => {
|
|
|
|
if (localCorrupt) {
|
2021-02-28 22:49:55 +01:00
|
|
|
displayMessage({
|
2021-03-28 23:31:10 +02:00
|
|
|
message: t('localCorrupt.message'),
|
2021-01-30 01:29:15 +01:00
|
|
|
description: localCorrupt.length ? localCorrupt : undefined,
|
2021-02-28 22:49:55 +01:00
|
|
|
type: 'error',
|
2022-02-12 14:51:01 +01:00
|
|
|
theme
|
2021-01-30 01:29:15 +01:00
|
|
|
})
|
2022-02-12 14:51:01 +01:00
|
|
|
// @ts-ignore
|
2021-08-29 15:25:38 +02:00
|
|
|
navigationRef.navigate('Screen-Tabs', {
|
2021-02-11 23:42:13 +01:00
|
|
|
screen: 'Tab-Me'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return showLocalCorrect()
|
2021-01-30 01:29:15 +01:00
|
|
|
}, [localCorrupt])
|
|
|
|
|
|
|
|
// Lazily update users's preferences, for e.g. composing default visibility
|
|
|
|
useEffect(() => {
|
2021-02-20 19:12:44 +01:00
|
|
|
if (instanceActive !== -1) {
|
2021-11-15 22:34:43 +01:00
|
|
|
dispatch(updateConfiguration())
|
2021-05-30 23:39:07 +02:00
|
|
|
dispatch(updateFilters())
|
2021-02-20 19:12:44 +01:00
|
|
|
dispatch(updateAccountPreferences())
|
2022-02-13 22:14:16 +01:00
|
|
|
dispatch(checkEmojis())
|
2021-01-30 01:29:15 +01:00
|
|
|
}
|
2021-02-27 17:25:01 +01:00
|
|
|
}, [instanceActive])
|
2021-01-30 01:29:15 +01:00
|
|
|
|
|
|
|
// Callbacks
|
2021-03-21 13:15:43 +01:00
|
|
|
const navigationContainerOnReady = useCallback(() => {
|
2021-08-29 15:25:38 +02:00
|
|
|
const currentRoute = navigationRef.getCurrentRoute()
|
2021-03-21 13:15:43 +01:00
|
|
|
routeRef.current = {
|
|
|
|
name: currentRoute?.name,
|
|
|
|
params: currentRoute?.params
|
2021-03-23 23:16:01 +01:00
|
|
|
? JSON.stringify(currentRoute.params)
|
|
|
|
: undefined
|
2021-03-21 13:15:43 +01:00
|
|
|
}
|
|
|
|
}, [])
|
2021-01-30 01:29:15 +01:00
|
|
|
const navigationContainerOnStateChange = useCallback(() => {
|
2021-03-21 13:15:43 +01:00
|
|
|
const previousRoute = routeRef.current
|
2021-08-29 15:25:38 +02:00
|
|
|
const currentRoute = navigationRef.getCurrentRoute()
|
2021-01-30 01:29:15 +01:00
|
|
|
|
2021-03-21 13:15:43 +01:00
|
|
|
const matchTabName = currentRoute?.name?.match(/(Tab-.*)-Root/)
|
2021-02-11 23:42:13 +01:00
|
|
|
if (matchTabName) {
|
|
|
|
//@ts-ignore
|
|
|
|
dispatch(updatePreviousTab(matchTabName[1]))
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:15:43 +01:00
|
|
|
if (previousRoute?.name !== currentRoute?.name) {
|
2022-02-14 22:10:07 +01:00
|
|
|
analytics('screen_view', { screen_name: currentRoute?.name })
|
2021-02-27 16:33:54 +01:00
|
|
|
Sentry.Native.setContext('page', {
|
2021-03-21 13:15:43 +01:00
|
|
|
previous: previousRoute,
|
|
|
|
current: currentRoute
|
2021-02-27 16:33:54 +01:00
|
|
|
})
|
2021-01-30 01:29:15 +01:00
|
|
|
}
|
|
|
|
|
2021-03-21 13:15:43 +01:00
|
|
|
routeRef.current = currentRoute
|
2021-01-30 01:29:15 +01:00
|
|
|
}, [])
|
|
|
|
|
2022-01-29 16:37:58 +01:00
|
|
|
// Deep linking for compose
|
|
|
|
const [deeplinked, setDeeplinked] = useState(false)
|
|
|
|
useEffect(() => {
|
|
|
|
const getUrlAsync = async () => {
|
|
|
|
setDeeplinked(true)
|
|
|
|
|
|
|
|
const initialUrl = await Linking.parseInitialURLAsync()
|
|
|
|
|
|
|
|
if (initialUrl.path) {
|
|
|
|
const paths = initialUrl.path.split('/')
|
|
|
|
|
|
|
|
if (paths && paths.length) {
|
|
|
|
const instanceIndex = instances.findIndex(
|
|
|
|
instance => paths[0] === `@${instance.account.acct}@${instance.uri}`
|
|
|
|
)
|
|
|
|
if (instanceIndex !== -1 && instanceActive !== instanceIndex) {
|
2022-01-30 22:51:03 +01:00
|
|
|
initQuery({
|
|
|
|
instance: instances[instanceIndex],
|
|
|
|
prefetch: { enabled: true }
|
|
|
|
})
|
2022-01-29 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initialUrl.hostname === 'compose') {
|
|
|
|
navigationRef.navigate('Screen-Compose')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!deeplinked) {
|
|
|
|
getUrlAsync()
|
|
|
|
}
|
|
|
|
}, [instanceActive, instances, deeplinked])
|
|
|
|
|
2021-01-30 01:29:15 +01:00
|
|
|
return (
|
|
|
|
<>
|
2022-02-17 00:09:19 +01:00
|
|
|
<StatusBar backgroundColor={colors.backgroundDefault} />
|
2021-01-30 01:29:15 +01:00
|
|
|
<NavigationContainer
|
|
|
|
ref={navigationRef}
|
2022-02-12 14:51:01 +01:00
|
|
|
theme={themes[theme]}
|
2021-01-30 01:29:15 +01:00
|
|
|
onReady={navigationContainerOnReady}
|
|
|
|
onStateChange={navigationContainerOnStateChange}
|
|
|
|
>
|
2021-02-10 00:40:44 +01:00
|
|
|
<Stack.Navigator initialRouteName='Screen-Tabs'>
|
2021-01-30 01:29:15 +01:00
|
|
|
<Stack.Screen
|
|
|
|
name='Screen-Tabs'
|
|
|
|
component={ScreenTabs}
|
|
|
|
options={{ headerShown: false }}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Stack.Screen
|
|
|
|
name='Screen-Actions'
|
|
|
|
component={ScreenActions}
|
|
|
|
options={{
|
2021-08-21 01:45:43 +02:00
|
|
|
presentation: 'transparentModal',
|
|
|
|
animation: 'fade',
|
2021-05-09 21:59:03 +02:00
|
|
|
headerShown: false
|
2021-01-30 01:29:15 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Stack.Screen
|
|
|
|
name='Screen-Announcements'
|
|
|
|
component={ScreenAnnouncements}
|
2021-05-12 22:45:51 +02:00
|
|
|
options={({ navigation }) => ({
|
2021-08-21 01:45:43 +02:00
|
|
|
presentation: 'transparentModal',
|
|
|
|
animation: 'fade',
|
2021-05-12 22:45:51 +02:00
|
|
|
headerShown: true,
|
2021-08-21 01:45:43 +02:00
|
|
|
headerShadowVisible: false,
|
|
|
|
headerTransparent: true,
|
2021-05-12 22:45:51 +02:00
|
|
|
headerStyle: { backgroundColor: 'transparent' },
|
|
|
|
headerLeft: () => (
|
|
|
|
<HeaderLeft content='X' onPress={() => navigation.goBack()} />
|
|
|
|
),
|
2021-12-18 23:44:08 +01:00
|
|
|
title: t('screenAnnouncements:heading')
|
2021-05-12 22:45:51 +02:00
|
|
|
})}
|
2021-01-30 01:29:15 +01:00
|
|
|
/>
|
|
|
|
<Stack.Screen
|
|
|
|
name='Screen-Compose'
|
|
|
|
component={ScreenCompose}
|
2021-02-10 00:40:44 +01:00
|
|
|
options={{
|
2021-08-21 01:45:43 +02:00
|
|
|
headerShown: false,
|
|
|
|
presentation: 'fullScreenModal'
|
2021-02-10 00:40:44 +01:00
|
|
|
}}
|
2021-01-30 01:29:15 +01:00
|
|
|
/>
|
|
|
|
<Stack.Screen
|
|
|
|
name='Screen-ImagesViewer'
|
|
|
|
component={ScreenImagesViewer}
|
|
|
|
options={{
|
2021-08-21 01:45:43 +02:00
|
|
|
headerShown: false,
|
|
|
|
presentation: 'fullScreenModal',
|
|
|
|
animation: 'fade'
|
2021-01-30 01:29:15 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Stack.Navigator>
|
|
|
|
|
2021-02-28 22:49:55 +01:00
|
|
|
<Message />
|
2021-01-30 01:29:15 +01:00
|
|
|
</NavigationContainer>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-08 23:19:55 +01:00
|
|
|
export default React.memo(Screens, () => true)
|