App.tsx 5.4 KB

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