App.tsx 4.7 KB

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