App.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. }
  76. }
  77. };
  78. const App = () => {
  79. return (
  80. <QueryClientProvider client={queryClient}>
  81. <ErrorProvider>
  82. <InnerApp />
  83. </ErrorProvider>
  84. </QueryClientProvider>
  85. );
  86. };
  87. const InnerApp = () => {
  88. const errorContext = useError();
  89. const navigation = React.useRef(null);
  90. const [isUpdateAvailable, setIsUpdateAvailable] = useState(false);
  91. useEffect(() => {
  92. setupGlobalErrorHandler(navigation);
  93. }, []);
  94. useEffect(() => {
  95. setupInterceptors(errorContext);
  96. }, [errorContext]);
  97. useEffect(() => {
  98. const checkLatestVersion = async () => {
  99. try {
  100. const response = await axios.get(API_URL + '/' + API.LATEST_VERSION, {
  101. headers: {
  102. 'App-Version': APP_VERSION,
  103. Platform: Platform.OS
  104. }
  105. });
  106. const { version } = response.data;
  107. const formatVersion = (versionString: string) => {
  108. return parseInt(versionString.replace(/\./g, ''), 10);
  109. };
  110. const currentVersionInt = formatVersion(APP_VERSION);
  111. const latestVersionInt = formatVersion(version);
  112. if (latestVersionInt > currentVersionInt) {
  113. setIsUpdateAvailable(true);
  114. }
  115. } catch (error) {
  116. console.error('Failed to check latest version:', error);
  117. }
  118. };
  119. checkLatestVersion();
  120. }, []);
  121. useEffect(() => {
  122. if (token) startAutoSyncListener(token);
  123. return () => stopAutoSyncListener();
  124. }, [token]);
  125. const handleUpdatePress = () => {
  126. const storeUrl = Platform.OS === 'ios' ? IOS_STORE_URL : ANDROID_STORE_URL;
  127. Linking.openURL(storeUrl).catch((err) => console.error('Failed to open store URL:', err));
  128. };
  129. return (
  130. <ConnectionProvider>
  131. <RegionProvider>
  132. <NavigationContainer
  133. ref={navigation}
  134. onReady={() => {
  135. routingInstrumentation.registerNavigationContainer(navigation);
  136. }}
  137. linking={linking}
  138. navigationInChildEnabled
  139. >
  140. <Route />
  141. <ConnectionBanner />
  142. <ErrorModal />
  143. <WarningModal
  144. isVisible={isUpdateAvailable}
  145. type="success"
  146. title="Update Available"
  147. message="A new version of the NomadMania app is available. Please update to the latest version."
  148. action={handleUpdatePress}
  149. onClose={() => setIsUpdateAvailable(false)}
  150. />
  151. </NavigationContainer>
  152. </RegionProvider>
  153. </ConnectionProvider>
  154. );
  155. };
  156. export default Sentry.wrap(App);