Skip to content
Snippets Groups Projects
RootStack.tsx 3.21 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexey Lunin's avatar
    Alexey Lunin committed
    import React, { useEffect } from 'react';
    
    import { Linking } from 'react-native';
    import UserInactivity from 'react-native-user-inactivity';
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    import {observer} from "mobx-react";
    import {useNavigation} from "@react-navigation/core";
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    import AgentProvider from '@aries-framework/react-hooks';
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    import rootStore from "src/store/rootStore";
    
    import MainStack from './MainStack';
    import OnboardingStack from './OnboardingStack';
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    import {useRealm} from "@realm/react";
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    import notifee, { EventType } from '@notifee/react-native';
    
    import {Screens, TabStackParams, TabStacks} from 'src/type/navigators';
    import { StackNavigationProp } from '@react-navigation/stack';
    import { ro } from 'date-fns/locale';
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    const RootStack: React.FC = observer(() => {
    
      const navigation = useNavigation<StackNavigationProp<TabStackParams>>();
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const realm = useRealm();
    
      useEffect(() => {
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        rootStore.agentStore.createAgent();
        rootStore.urlStore.injectNavigation(navigation);
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        rootStore.agentStore.injectNavigation(navigation);
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        rootStore.injectRealm(realm);
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        rootStore.agentStore.injectRealm(realm);
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        notifee.requestPermission();
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      }, []);
    
      const handleDeepLinking = (url: string) => {
        rootStore.urlStore.setDeepLink(url);
    
        if (rootStore.authenticated) {
          rootStore.urlStore.processDeepLinkIfAny();
        }
      };
    
      //if the app is not opened
    
      useEffect(() => {
        (async () => {
          const initialUrl = await Linking.getInitialURL();
    
    
          if(initialUrl) {
            const supported = await Linking.canOpenURL(initialUrl);
    
            if(supported) {
              handleDeepLinking(initialUrl);
            }
    
      //if the app is opened in the background
      useEffect(() => {
        Linking.addEventListener('url', async ({url}) => {
          if(url){
            const supported = await Linking.canOpenURL(url);
    
            if(supported) {
              handleDeepLinking(url);
            }
          }
        });
        return () => {
          Linking.removeAllListeners('url');
        }
      }, []);
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      // Subscribe to events
      useEffect(() => {
        if (!rootStore.authenticated) return;
    
        return notifee.onForegroundEvent(({ type, detail }) => {
          switch (type) {
            case EventType.PRESS:
              console.log('User pressed notification', detail.notification);
              if (detail.notification?.data?.type === 'new-email-received') {
                const realmId = detail.notification?.data?.realmId as string;
                if (realmId) {
                  navigation.navigate(TabStacks.EmailStack, {
                    screen: Screens.EmailDetails,
    
                    params: { realmId: realmId},
    
    Alexey Lunin's avatar
    Alexey Lunin committed
                  });
                }
              }
    
              break;
          }
        });
      }, [rootStore.authenticated]);
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      useEffect(() => {
        if (rootStore.authenticated) {
          rootStore.urlStore.processDeepLinkIfAny();
        }
      }, [rootStore.authenticated]);
    
      return rootStore.agentStore.agentCreated && rootStore.authenticated ? (
    
        <UserInactivity
    
    Alexey Lunin's avatar
    Alexey Lunin committed
          isActive={rootStore.agentStore.active}
    
          timeForInactivity={300000}
    
    Alexey Lunin's avatar
    Alexey Lunin committed
          onAction={isActive => {
            rootStore.setAuthenticated(isActive);
          }}
    
    Alexey Lunin's avatar
    Alexey Lunin committed
          <AgentProvider agent={rootStore.agentStore.agent}>
            <MainStack />
          </AgentProvider>
    
        </UserInactivity>
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      ) : <OnboardingStack />;
    });
    
    
    export default RootStack;