tooot/src/Screens.tsx

355 lines
11 KiB
TypeScript
Raw Normal View History

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'
import { createNativeStackNavigator } from '@react-navigation/native-stack'
2022-08-12 16:44:28 +02:00
import ScreenAccountSelection from '@screens/AccountSelection'
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'
import * as Sentry from '@sentry/react-native'
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'
import * as Linking from 'expo-linking'
2021-02-27 16:33:54 +01:00
import { addScreenshotListener } from 'expo-screen-capture'
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'
2022-05-02 22:31:22 +02:00
import ShareMenu from 'react-native-share-menu'
2022-04-30 23:47:52 +02:00
import { useSelector } from 'react-redux'
import { useAppDispatch } from './store'
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
}
const Screens: React.FC<Props> = ({ localCorrupt }) => {
const { i18n, t } = useTranslation('screens')
2022-04-30 23:47:52 +02:00
const dispatch = useAppDispatch()
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
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)
pushUseConnect()
pushUseReceive()
pushUseRespond()
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
}
}, [instanceActive])
2021-01-30 01:29:15 +01:00
// Callbacks
const navigationContainerOnReady = useCallback(() => {
2021-08-29 15:25:38 +02:00
const currentRoute = navigationRef.getCurrentRoute()
routeRef.current = {
name: currentRoute?.name,
params: currentRoute?.params ? JSON.stringify(currentRoute.params) : undefined
}
}, [])
2021-01-30 01:29:15 +01:00
const navigationContainerOnStateChange = useCallback(() => {
const previousRoute = routeRef.current
2021-08-29 15:25:38 +02:00
const currentRoute = navigationRef.getCurrentRoute()
2021-01-30 01:29:15 +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]))
}
if (previousRoute?.name !== currentRoute?.name) {
Sentry.setContext('page', {
previous: previousRoute,
current: currentRoute
2021-02-27 16:33:54 +01:00
})
2021-01-30 01:29:15 +01:00
}
routeRef.current = currentRoute
2021-01-30 01:29:15 +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 }
})
}
}
}
if (initialUrl.hostname === 'compose') {
navigationRef.navigate('Screen-Compose')
}
}
if (!deeplinked) {
getUrlAsync()
}
}, [instanceActive, instances, deeplinked])
2022-05-02 22:31:22 +02:00
// Share Extension
const handleShare = useCallback(
2022-05-05 23:03:00 +02:00
(
item?:
| {
data: { mimeType: string; data: string }[]
mimeType: undefined
}
| { data: string | string[]; mimeType: string }
) => {
2022-05-02 22:31:22 +02:00
if (instanceActive < 0) {
return
}
2022-05-05 23:03:00 +02:00
if (!item || !item.data) {
2022-05-02 22:31:22 +02:00
return
}
let text: string | undefined = undefined
2022-06-09 21:33:10 +02:00
let media: { uri: string; mime: string }[] = []
2022-06-05 17:58:18 +02:00
const typesImage = ['png', 'jpg', 'jpeg', 'gif']
const typesVideo = ['mp4', 'm4v', 'mov', 'webm', 'mpeg']
2022-06-09 21:33:10 +02:00
const filterMedia = ({ uri, mime }: { uri: string; mime: string }) => {
2022-06-05 17:58:18 +02:00
if (mime.startsWith('image/')) {
if (!typesImage.includes(mime.split('/')[1])) {
console.warn('Image type not supported:', mime.split('/')[1])
displayMessage({
message: t('shareError.imageNotSupported', {
type: mime.split('/')[1]
}),
type: 'error',
theme
})
return
}
2022-06-09 21:33:10 +02:00
media.push({ uri, mime })
2022-06-05 17:58:18 +02:00
} else if (mime.startsWith('video/')) {
if (!typesVideo.includes(mime.split('/')[1])) {
console.warn('Video type not supported:', mime.split('/')[1])
displayMessage({
message: t('shareError.videoNotSupported', {
type: mime.split('/')[1]
}),
type: 'error',
theme
})
return
}
2022-06-09 21:33:10 +02:00
media.push({ uri, mime })
2022-06-05 17:58:18 +02:00
} else {
2022-06-09 21:33:10 +02:00
if (typesImage.includes(uri.split('.').pop() || '')) {
media.push({ uri, mime: 'image/jpg' })
2022-06-05 17:58:18 +02:00
return
}
2022-06-09 21:33:10 +02:00
if (typesVideo.includes(uri.split('.').pop() || '')) {
media.push({ uri, mime: 'video/mp4' })
2022-06-05 17:58:18 +02:00
return
}
2022-06-09 21:33:10 +02:00
text = !text ? uri : text.concat(text, `\n${uri}`)
2022-06-05 17:58:18 +02:00
}
}
2022-05-05 23:03:00 +02:00
switch (Platform.OS) {
case 'ios':
if (!Array.isArray(item.data) || !item.data) {
2022-05-02 22:31:22 +02:00
return
}
2022-05-05 23:03:00 +02:00
2022-06-05 17:58:18 +02:00
for (const d of item.data) {
if (typeof d !== 'string') {
2022-06-09 21:33:10 +02:00
filterMedia({ uri: d.data, mime: d.mimeType })
2022-05-05 23:03:00 +02:00
}
2022-06-05 17:58:18 +02:00
}
2022-05-05 23:03:00 +02:00
break
2022-11-20 22:42:09 +01:00
case 'android':
if (!item.mimeType) {
return
}
if (Array.isArray(item.data)) {
for (const d of item.data) {
filterMedia({ uri: d, mime: item.mimeType })
}
} else {
filterMedia({ uri: item.data, mime: item.mimeType })
}
break
2022-05-05 23:03:00 +02:00
}
2022-06-05 17:58:18 +02:00
if (!text && !media.length) {
2022-05-05 23:03:00 +02:00
return
} else {
2022-08-12 16:44:28 +02:00
if (instances.length > 1) {
navigationRef.navigate('Screen-AccountSelection', {
share: { text, media }
})
} else {
navigationRef.navigate('Screen-Compose', {
type: 'share',
text,
media
})
}
2022-05-05 23:03:00 +02:00
}
2022-05-02 22:31:22 +02:00
},
[]
2022-05-02 22:31:22 +02:00
)
useEffect(() => {
2022-05-05 23:03:00 +02:00
ShareMenu.getInitialShare(handleShare)
2022-05-02 22:31:22 +02:00
}, [])
useEffect(() => {
2022-05-05 23:03:00 +02:00
const listener = ShareMenu.addNewShareListener(handleShare)
2022-05-02 22:31:22 +02:00
return () => {
listener.remove()
}
}, [])
2021-01-30 01:29:15 +01:00
return (
<>
<StatusBar
2022-06-15 00:00:33 +02:00
backgroundColor={colors.backgroundDefault}
{...(Platform.OS === 'android' && {
barStyle: theme === 'light' ? 'dark-content' : 'light-content'
})}
/>
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={{
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 }) => ({
presentation: 'transparentModal',
animation: 'fade',
2021-05-12 22:45:51 +02:00
headerShown: true,
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={{
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}
2022-10-30 15:05:40 +01:00
options={{ headerShown: false, animation: 'fade' }}
2021-01-30 01:29:15 +01:00
/>
2022-08-12 16:44:28 +02:00
<Stack.Screen
name='Screen-AccountSelection'
component={ScreenAccountSelection}
options={({ navigation }) => ({
title: t('screenAccountSelection:heading'),
headerShadowVisible: false,
presentation: 'modal',
gestureEnabled: false,
headerLeft: () => (
<HeaderLeft
type='text'
content={t('common:buttons.cancel')}
onPress={() => navigation.goBack()}
/>
)
})}
/>
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-01-30 01:29:15 +01:00
)
}
export default React.memo(Screens, () => true)