Skip to content
Snippets Groups Projects
RootStack.tsx 2.55 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, TabStacks} from 'src/type/navigators';
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    const RootStack: React.FC = observer(() => {
      const navigation = useNavigation();
    
    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.agentStore.injectRealm(realm);
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        notifee.requestPermission();
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      }, []);
    
    
    
      useEffect(() => {
        (async () => {
    
    Alexey Lunin's avatar
    Alexey Lunin committed
          const handleDeepLinking = (url: string) => {
            rootStore.urlStore.setDeepLink(url);
    
          };
    
          Linking.addEventListener('url', ({ url }) => handleDeepLinking(url));
          const initialUrl = await Linking.getInitialURL();
          if (initialUrl) {
            handleDeepLinking(initialUrl);
          }
        })();
      }, []);
    
    
    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 },
                  });
                }
              }
    
              break;
          }
        });
      }, [rootStore.authenticated]);
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      useEffect(() => {
        if (rootStore.authenticated) {
          rootStore.urlStore.processDeepLinkIfAny();
        }
      }, [rootStore.authenticated]);
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      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;