-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.tsx
More file actions
131 lines (118 loc) · 3.79 KB
/
App.tsx
File metadata and controls
131 lines (118 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React, { useEffect, useMemo } from 'react';
import './src/sheets';
import { ThemeProvider } from 'styled-components/native';
import { darkTheme, lightTheme } from './src/theme';
import RootNavigator from './src/navigation/RootNavigator.tsx';
import { SheetProvider } from 'react-native-actions-sheet';
import { Linking, useColorScheme } from 'react-native';
import { ETheme } from './src/types/settings.ts';
import { useDispatch, useSelector } from 'react-redux';
import { getIsOnline, getTheme } from './src/store/selectors/settingsSelectors.ts';
import { SafeAreaView } from './src/theme/components.ts';
import Toast from 'react-native-toast-message';
import { toastConfig } from './src/theme/toastConfig.tsx';
import NetInfo from '@react-native-community/netinfo';
import { updateIsOnline } from './src/store/slices/settingsSlice.ts';
import { checkNetworkConnection, showToast } from './src/utils/helpers.ts';
import { setDeepLink } from './src/store/slices/pubkysSlice.ts';
import { parseInput } from './src/utils/inputParser.ts';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { useTranslation } from 'react-i18next';
const _toastConfig = toastConfig();
function App(): React.JSX.Element {
const colorScheme = useColorScheme();
const currentTheme = useSelector(getTheme);
const isOnline = useSelector(getIsOnline);
const dispatch = useDispatch();
const { t } = useTranslation();
// Handle deep linking
useEffect(() => {
// Handle deep link when app is opened from a background state
const getInitialURL = async (): Promise<void> => {
try {
let url = await Linking.getInitialURL();
if (url) {
handleDeepLink(url);
}
} catch (err) {
console.error('Error getting initial URL:', err);
}
};
// Handle the deep link using the unified input parser
const handleDeepLink = async (url: string): Promise<void> => {
const parsedInput = await parseInput(url, 'deeplink');
dispatch(setDeepLink(JSON.stringify(parsedInput)));
};
// Set up deep link listeners for when app is already running
const subscription = Linking.addEventListener('url', ({ url }) => {
handleDeepLink(url);
});
// Check for initial URL on mount
getInitialURL();
// Cleanup subscription
return (): void => {
subscription.remove();
};
}, [dispatch]);
useEffect(() => {
// Defer network check to avoid blocking initial render
const timer = setTimeout(() => {
checkNetworkConnection({
prevNetworkState: isOnline,
dispatch,
displayToastIfOnline: false,
displayToastIfOffline: true,
});
}, 500);
const unsubscribe = NetInfo.addEventListener(state => {
const isConnected = state?.isConnected ?? false;
if (isOnline !== isConnected) {
dispatch(updateIsOnline({ isOnline: isConnected }));
if (isConnected) {
showToast({
type: 'success',
title: t('network.backOnline'),
description: t('network.backOnlineDescription'),
});
} else {
showToast({
type: 'error',
title: t('network.currentlyOffline'),
description: t('network.offlineDescription'),
autoHide: false,
});
}
}
});
// Cleanup subscription on unmount
return (): void => {
clearTimeout(timer);
unsubscribe();
};
}, [dispatch, isOnline, t]);
const theme = useMemo(() => {
switch (currentTheme) {
case ETheme.system:
return colorScheme === 'dark' ? darkTheme : lightTheme;
case ETheme.dark:
return darkTheme;
case ETheme.light:
return lightTheme;
default:
return darkTheme;
}
}, [colorScheme, currentTheme]);
return (
<ThemeProvider theme={theme}>
<SafeAreaProvider>
<SafeAreaView>
<SheetProvider>
<RootNavigator />
<Toast config={_toastConfig} />
</SheetProvider>
</SafeAreaView>
</SafeAreaProvider>
</ThemeProvider>
);
}
export default App;