App.tsx 4.7 KB

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