Advertisement
Guest User

App.tsx

a guest
Oct 13th, 2023
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect } from 'react';
  2. import { NavigationContainer } from '@react-navigation/native';
  3. import { ThemeProvider } from '@rneui/themed';
  4. import { theme } from './src/config/theme';
  5. import SplashScreen from 'react-native-splash-screen';
  6. import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client';
  7. import { persister, queryClient } from './src/services/queryClient';
  8. import NetInfo from '@react-native-community/netinfo';
  9. import { onlineManager, QueryErrorResetBoundary } from '@tanstack/react-query';
  10. import ConnectionLostAlert from './src/components/Alerts/ConnectionLostAlert/ConnectionLostAlert';
  11. import ErrorBoundary from './src/components/Alerts/ErrorBoundary/ErrorBoundary';
  12. import { ActivityIndicator, Linking, View } from 'react-native';
  13. import branch from 'react-native-branch';
  14. import RootNavigator from './src/navigation/RootNavigator';
  15. import CodePush from 'react-native-code-push';
  16. import { nicelog } from './src/utils/nicelog';
  17. import { navigationRef } from '.';
  18. import EnvFlag from './src/components/Common/EnvFlag/EnvFlag';
  19. import { DevToolProvider } from './src/hooks/useDev';
  20.  
  21. const codepushConfig = {
  22.   checkFrequency: CodePush.CheckFrequency.ON_APP_RESUME,
  23.   mandatoryInstallMode: CodePush.InstallMode.IMMEDIATE
  24. };
  25.  
  26. const linking = {
  27.   prefixes: ['native-contractor://'],
  28.   subscribe(listener) {
  29.     // First, you may want to do the default deep link handling
  30.     const onReceiveURL = ({ url }: { url: string }) => listener(url);
  31.  
  32.     // Listen to incoming links from deep linking
  33.     Linking.addEventListener('url', onReceiveURL);
  34.  
  35.     // Next, you would need to subscribe to incoming links from your third-party integration
  36.     // For example, to get to subscribe to incoming links from branch.io:
  37.     const branchUnsubscribe = branch.subscribe(({ error, params, uri }) => {
  38.       nicelog('ON RECEIVE URL', { error, params, uri });
  39.  
  40.       if (error) {
  41.         console.error('Error from Branch: ' + error);
  42.         return;
  43.       }
  44.  
  45.       if (params['+non_branch_link']) {
  46.         const nonBranchUrl = params['+non_branch_link'];
  47.         // Route non-Branch URL if appropriate.
  48.         return;
  49.       }
  50.  
  51.       if (!params['+clicked_branch_link']) {
  52.         // Indicates initialization success and some other conditions.
  53.         // No link was opened.
  54.         return;
  55.       }
  56.  
  57.       // A Branch link was opened
  58.       // attach any query params to deeplink_path
  59.  
  60.       let url = params.$deeplink_path;
  61.  
  62.       if (params?.data && params?.projectJobId) {
  63.         url = `${params.$deeplink_path}/${params.data}/${params.projectJobId}`;
  64.       } else if (params?.data) {
  65.         url = `${params.$deeplink_path}/${params.data}`;
  66.       }
  67.  
  68.       console.log('DEEPLINK PATH', url);
  69.  
  70.       listener(url);
  71.     });
  72.  
  73.     return () => {
  74.       // Clean up the event listeners
  75.       Linking.removeAllListeners('url');
  76.       branchUnsubscribe();
  77.     };
  78.   },
  79.   config: {
  80.     screens: {
  81.       // Authenticated Stack
  82.       Authenticated: {
  83.         screens: {
  84.           'Home Navigator': {
  85.             screens: {
  86.               Projects: 'projects',
  87.               Account: 'account'
  88.             }
  89.           },
  90.           'Check Out': 'checkout/:projectId',
  91.           'Crew Approval': 'approval/:projectId?',
  92.           'Check In Out Photo': 'upload/:projectId/:projectJobId',
  93.           Projects: 'jobs/:projectJobId',
  94.           'Job Details': 'job/:projectJobId',
  95.           'Open Jobs': 'bids/:projectJobId',
  96.           'Job Description': 'bid/:projectJobId'
  97.         }
  98.       },
  99.       // Un-authenticated Stacks
  100.       Login: {
  101.         screens: {
  102.           'Register Number': 'register',
  103.           'Register Code': 'code'
  104.         }
  105.       }
  106.     }
  107.   }
  108. };
  109.  
  110. function App(): JSX.Element {
  111.   useEffect(() => {
  112.     SplashScreen.hide();
  113.  
  114.     onlineManager.setEventListener((setOnline) => {
  115.       return NetInfo.addEventListener((state) => {
  116.         setOnline(!!state.isConnected);
  117.       });
  118.     });
  119.   }, []);
  120.  
  121.   return (
  122.     <ThemeProvider theme={theme}>
  123.       <DevToolProvider>
  124.         <QueryErrorResetBoundary>
  125.           {({ reset }) => (
  126.             <ErrorBoundary onReset={reset}>
  127.               <PersistQueryClientProvider
  128.                 client={queryClient}
  129.                 persistOptions={{
  130.                   persister,
  131.                   maxAge: Infinity
  132.                 }}
  133.                 onSuccess={() => {
  134.                   // resume mutations after initial restore from localStorage was successful
  135.                   queryClient.resumePausedMutations().then(() => {
  136.                     queryClient.invalidateQueries();
  137.                   });
  138.                 }}>
  139.                 <NavigationContainer
  140.                   ref={navigationRef}
  141.                   linking={linking}
  142.                   fallback={
  143.                     <View
  144.                       style={{
  145.                         justifyContent: 'center',
  146.                         alignItems: 'center'
  147.                       }}>
  148.                       <ActivityIndicator animating={true} />
  149.                     </View>
  150.                   }>
  151.                   <RootNavigator />
  152.                 </NavigationContainer>
  153.                 <ConnectionLostAlert />
  154.                 <EnvFlag />
  155.               </PersistQueryClientProvider>
  156.             </ErrorBoundary>
  157.           )}
  158.         </QueryErrorResetBoundary>
  159.       </DevToolProvider>
  160.     </ThemeProvider>
  161.   );
  162. }
  163.  
  164. export default CodePush(codepushConfig)(App);
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement