App.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import 'react-native-gesture-handler';
  2. import 'expo-splash-screen';
  3. import { QueryClientProvider } from '@tanstack/react-query';
  4. import { NavigationContainer } from '@react-navigation/native';
  5. import { queryClient } from 'src/utils/queryClient';
  6. import * as Sentry from '@sentry/react-native';
  7. import { GestureHandlerRootView } from 'react-native-gesture-handler';
  8. import { SheetProvider } from 'react-native-actions-sheet';
  9. import { SafeAreaProvider } from 'react-native-safe-area-context';
  10. import Route from './Route';
  11. import { ConnectionProvider } from 'src/contexts/ConnectionContext';
  12. import ConnectionBanner from 'src/components/ConnectionBanner/ConnectionBanner';
  13. import { RegionProvider } from 'src/contexts/RegionContext';
  14. import { ErrorProvider, useError } from 'src/contexts/ErrorContext';
  15. import { useEffect, useState } from 'react';
  16. import { setupInterceptors } from 'src/utils/request';
  17. import { ErrorModal, WarningModal } from 'src/components';
  18. import React from 'react';
  19. import { Linking, Platform } from 'react-native';
  20. import { API_HOST, API_URL, APP_VERSION } from 'src/constants';
  21. import axios from 'axios';
  22. import { API } from 'src/types';
  23. import { LogBox } from 'react-native';
  24. import { storage, StoreType } from 'src/storage';
  25. import { setupGlobalErrorHandler } from 'src/utils/globalErrorHandler';
  26. import {
  27. startAutoSyncListener,
  28. stopAutoSyncListener
  29. } from 'src/watermelondb/features/chat/networkSync';
  30. const IOS_STORE_URL = 'https://apps.apple.com/app/id6502843543';
  31. const ANDROID_STORE_URL =
  32. 'https://play.google.com/store/apps/details?id=com.nomadmania.presentation';
  33. LogBox.ignoreLogs([/defaultProps will be removed/, /IMGElement/]);
  34. const userId = (storage.get('uid', StoreType.STRING) as string) ?? 'not_logged_in';
  35. const token = (storage.get('token', StoreType.STRING) as string) ?? null;
  36. const routingInstrumentation = Sentry.reactNavigationIntegration({
  37. enableTimeToInitialDisplay: true
  38. });
  39. Sentry.init({
  40. dsn: 'https://c9b37005f4be22a17a582603ebc17598@o4507781200543744.ingest.de.sentry.io/4507781253824592',
  41. integrations: [Sentry.reactNativeTracingIntegration({ routingInstrumentation })],
  42. debug: false,
  43. enableNative: true,
  44. enableNativeCrashHandling: true,
  45. ignoreErrors: ['Network Error', 'ECONNABORTED', 'timeout of 10000ms exceeded'],
  46. beforeSend(event, hint) {
  47. if (userId) {
  48. event.user = {
  49. ...event.user,
  50. userId: userId
  51. };
  52. }
  53. const isNonError = hint?.originalException instanceof Error === false;
  54. if (isNonError || event.message?.match(/Non-Error exception captured/)) {
  55. return {
  56. ...event,
  57. message: `Processed Non-Error: ${event.message || 'No message'}`,
  58. level: 'warning',
  59. contexts: {
  60. ...event.contexts,
  61. non_error: {
  62. type: typeof hint?.originalException,
  63. value: JSON.stringify(hint?.originalException)
  64. }
  65. }
  66. };
  67. }
  68. return event;
  69. }
  70. });
  71. const linking = {
  72. prefixes: [API_HOST, 'nomadmania://'],
  73. config: {
  74. screens: {
  75. publicProfileView: '/profile/:userId',
  76. inAppEvent: '/event/:url',
  77. inAppMapTab: '/map/:lon/:lat',
  78. inAppChatsList: '/messages',
  79. inAppMasterRanking: '/master-ranking',
  80. inAppLpiRanking: '/lpi',
  81. inAppInMemoriam: '/in-memoriam',
  82. inAppInHistory: '/travellers-in-history',
  83. inAppUNMaster: '/un-masters',
  84. inAppStatistics: '/statistics',
  85. inAppTriumphs: '/triumphs',
  86. inAppSeriesRanking: '/series-ranking',
  87. inAppMyFriends: '/my-friends',
  88. inAppFixers: '/fixers',
  89. inAppEarth: '/earth',
  90. inAppSeries: '/series',
  91. inAppCountries: '/countries',
  92. inAppRegions: '/regions',
  93. inAppDare: '/dare',
  94. inAppRegionPreview: {
  95. path: 'region/:id',
  96. alias: [
  97. 'region_mqp/:id',
  98. 'region/:id/',
  99. 'region_mqp/:id/',
  100. ],
  101. },
  102. inAppPhotos: '/photos',
  103. inAppTrips2025: '/trips'
  104. }
  105. }
  106. };
  107. const App = () => {
  108. return (
  109. <GestureHandlerRootView style={{ flex: 1 }}>
  110. <SafeAreaProvider>
  111. <SheetProvider>
  112. <QueryClientProvider client={queryClient}>
  113. <ErrorProvider>
  114. <InnerApp />
  115. </ErrorProvider>
  116. </QueryClientProvider>
  117. </SheetProvider>
  118. </SafeAreaProvider>
  119. </GestureHandlerRootView>
  120. );
  121. };
  122. const InnerApp = () => {
  123. const errorContext = useError();
  124. const navigation = React.useRef(null);
  125. const [isUpdateAvailable, setIsUpdateAvailable] = useState(false);
  126. useEffect(() => {
  127. setupGlobalErrorHandler(navigation);
  128. }, []);
  129. useEffect(() => {
  130. setupInterceptors(errorContext);
  131. }, [errorContext]);
  132. useEffect(() => {
  133. const checkLatestVersion = async () => {
  134. try {
  135. const response = await axios.get(API_URL + '/' + API.LATEST_VERSION, {
  136. headers: {
  137. 'App-Version': APP_VERSION,
  138. Platform: Platform.OS
  139. }
  140. });
  141. const { version } = response.data;
  142. const formatVersion = (versionString: string) => {
  143. return parseInt(versionString.replace(/\./g, ''), 10);
  144. };
  145. const currentVersionInt = formatVersion(APP_VERSION);
  146. const latestVersionInt = formatVersion(version);
  147. if (latestVersionInt > currentVersionInt) {
  148. setIsUpdateAvailable(true);
  149. }
  150. } catch (error) {
  151. console.error('Failed to check latest version:', error);
  152. }
  153. };
  154. checkLatestVersion();
  155. }, []);
  156. useEffect(() => {
  157. if (token) startAutoSyncListener(token);
  158. return () => stopAutoSyncListener();
  159. }, [token]);
  160. const handleUpdatePress = () => {
  161. const storeUrl = Platform.OS === 'ios' ? IOS_STORE_URL : ANDROID_STORE_URL;
  162. Linking.openURL(storeUrl).catch((err) => console.error('Failed to open store URL:', err));
  163. };
  164. return (
  165. <ConnectionProvider>
  166. <RegionProvider>
  167. <NavigationContainer
  168. ref={navigation}
  169. onReady={() => {
  170. routingInstrumentation.registerNavigationContainer(navigation);
  171. }}
  172. linking={linking}
  173. navigationInChildEnabled
  174. >
  175. <Route />
  176. <ConnectionBanner />
  177. <ErrorModal />
  178. <WarningModal
  179. isVisible={isUpdateAvailable}
  180. type="success"
  181. title="Update Available"
  182. message="A new version of the NomadMania app is available. Please update to the latest version."
  183. action={handleUpdatePress}
  184. onClose={() => setIsUpdateAvailable(false)}
  185. />
  186. </NavigationContainer>
  187. </RegionProvider>
  188. </ConnectionProvider>
  189. );
  190. };
  191. export default Sentry.wrap(App);