App.tsx 5.7 KB

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