App.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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://', 'nm://'],
  73. config: {
  74. screens: {
  75. publicProfileView: '/profile/:userId',
  76. inAppEvent: '/event/:url',
  77. inAppSeriesShare: '/series-item/:id',
  78. inAppMapTab: '/map/:lon/:lat',
  79. inAppChatsList: '/messages',
  80. inAppMasterRanking: '/master-ranking',
  81. inAppLpiRanking: '/lpi',
  82. inAppInMemoriam: '/in-memoriam',
  83. inAppInHistory: '/travellers-in-history',
  84. inAppUNMaster: '/un-masters',
  85. inAppStatistics: '/statistics',
  86. inAppTriumphs: '/triumphs',
  87. inAppSeriesRanking: '/series-ranking',
  88. inAppMyFriends: '/my-friends',
  89. inAppFixers: '/fixers',
  90. inAppEarth: '/earth',
  91. inAppSeries: '/series',
  92. inAppCountries: '/countries',
  93. inAppRegions: '/regions',
  94. inAppDare: '/dare',
  95. inAppRegionPreview: {
  96. path: 'region/:id',
  97. alias: [
  98. 'region_mqp/:id',
  99. 'region/:id/',
  100. 'region_mqp/:id/',
  101. ],
  102. },
  103. inAppPhotos: '/photos',
  104. inAppTrips2025: '/trips'
  105. }
  106. }
  107. };
  108. const App = () => {
  109. return (
  110. <GestureHandlerRootView style={{ flex: 1 }}>
  111. <SafeAreaProvider>
  112. <SheetProvider>
  113. <QueryClientProvider client={queryClient}>
  114. <ErrorProvider>
  115. <InnerApp />
  116. </ErrorProvider>
  117. </QueryClientProvider>
  118. </SheetProvider>
  119. </SafeAreaProvider>
  120. </GestureHandlerRootView>
  121. );
  122. };
  123. const InnerApp = () => {
  124. const errorContext = useError();
  125. const navigation = React.useRef<any>(null);
  126. const [isUpdateAvailable, setIsUpdateAvailable] = useState(false);
  127. const [latestVersion, setLatestVersion] = useState<string | null>(null);
  128. useEffect(() => {
  129. setupGlobalErrorHandler(navigation);
  130. }, []);
  131. useEffect(() => {
  132. setupInterceptors(errorContext);
  133. }, [errorContext]);
  134. useEffect(() => {
  135. const checkLatestVersion = async () => {
  136. try {
  137. const response = await axios.get(API_URL + '/' + API.LATEST_VERSION, {
  138. headers: {
  139. 'App-Version': APP_VERSION,
  140. Platform: Platform.OS
  141. }
  142. });
  143. const { version } = response.data;
  144. const formatVersion = (versionString: string) => {
  145. return parseInt(versionString.replace(/\./g, ''), 10);
  146. };
  147. const currentVersionInt = formatVersion(APP_VERSION);
  148. const latestVersionInt = formatVersion(version);
  149. if (latestVersionInt > currentVersionInt) {
  150. const dismissedVersion = storage.get('dismissed_update_version', StoreType.STRING) as string | null;
  151. if (dismissedVersion !== version) {
  152. setIsUpdateAvailable(true);
  153. setLatestVersion(version);
  154. }
  155. }
  156. } catch (error) {
  157. console.error('Failed to check latest version:', error);
  158. }
  159. };
  160. checkLatestVersion();
  161. }, []);
  162. useEffect(() => {
  163. if (token) startAutoSyncListener(token);
  164. return () => stopAutoSyncListener();
  165. }, [token]);
  166. const handleUpdatePress = () => {
  167. const storeUrl = Platform.OS === 'ios' ? IOS_STORE_URL : ANDROID_STORE_URL;
  168. Linking.openURL(storeUrl).catch((err) => console.error('Failed to open store URL:', err));
  169. };
  170. return (
  171. <ConnectionProvider>
  172. <RegionProvider>
  173. <NavigationContainer
  174. ref={navigation}
  175. onReady={() => {
  176. routingInstrumentation.registerNavigationContainer(navigation);
  177. }}
  178. linking={linking}
  179. navigationInChildEnabled
  180. >
  181. <Route />
  182. <ConnectionBanner />
  183. <ErrorModal />
  184. <WarningModal
  185. isVisible={isUpdateAvailable}
  186. type="success"
  187. title="Update Available"
  188. message="A new version of the NomadMania app is available. Please update to the latest version."
  189. action={handleUpdatePress}
  190. onClose={() => {
  191. if (latestVersion) {
  192. storage.set('dismissed_update_version', latestVersion);
  193. }
  194. setIsUpdateAvailable(false);
  195. }}
  196. />
  197. </NavigationContainer>
  198. </RegionProvider>
  199. </ConnectionProvider>
  200. );
  201. };
  202. export default Sentry.wrap(App);