App.tsx 4.9 KB

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