/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2019
 */

/**
 * Two main purposes:
 *
 * rootNavigatorRef: This is a ref created that is set as a reference to the
 * main root navigator, in main.js. This is the main navigator of the entire app.
 *
 * RootNavigator: An export object of functions which control the main navigator which
 * can be imported and used anywhere.
 */

import React from 'react';
import { StackActions } from '@react-navigation/native';

export const rootNavigatorRef = React.createRef();

const navigate = (screenName, screenProps) =>
  rootNavigatorRef.current?.navigate(screenName, screenProps);

const replace = (screenName, screenProps) =>
  rootNavigatorRef.current?.dispatch(StackActions.replace(screenName, screenProps));

const goBack = () => rootNavigatorRef.current?.dispatch(StackActions.pop());

const canGoBack = () => !!rootNavigatorRef.current?.canGoBack();

const getCurrentRouteName = () => {
  if (!rootNavigatorRef.current?.getRootState()) return '';

  const routes = rootNavigatorRef.current?.getRootState()?.routes ?? [];

  return routes[routes.length - 1]?.name ?? '';
};

const getPrevRouteName = () => {
  if (!rootNavigatorRef.current?.getRootState()) return '';

  const routes = rootNavigatorRef.current?.getRootState()?.routes ?? [];

  return routes[routes.length - 2]?.name ?? '';
};

export const RootNavigator = {
  navigate,
  replace,
  goBack,
  canGoBack,
  getCurrentRouteName,
  getPrevRouteName,
};

@@ -408,16 +408,61 @@
 react';%0A
+import Bugsnag from '@bugsnag/react-native';%0A
 import %7B
@@ -600,32 +600,123 @@
  screenProps) =%3E
+ %7B%0A  const breadcrumb = %60Navigating: $%7BscreenName%7D%60;%0A  Bugsnag.leaveBreadcrumb(breadcrumb);
 %0A  rootNavigator
@@ -762,16 +762,19 @@
 nProps);
+%0A%7D;
 %0A%0Aconst 
@@ -811,16 +811,107 @@
 rops) =%3E
+ %7B%0A  const breadcrumb = %60Navigating: $%7BscreenName%7D%60;%0A  Bugsnag.leaveBreadcrumb(breadcrumb);
 %0A  rootN
@@ -987,16 +987,19 @@
 Props));
+%0A%7D;
 %0A%0Aconst 
@@ -1012,16 +1012,99 @@
  = () =%3E
+ %7B%0A  const breadcrumb = 'Navigating: Pop';%0A  Bugsnag.leaveBreadcrumb(breadcrumb);%0A 
  rootNav
@@ -1151,16 +1151,19 @@
 .pop());
+%0A%7D;
 %0A%0Aconst 
@@ -1310,33 +1310,32 @@
 e()) return '';%0A
-%0A
   const routes =
@@ -1383,33 +1383,32 @@
 ?.routes ?? %5B%5D;%0A
-%0A
   return routes%5B
@@ -1536,17 +1536,16 @@
 urn '';%0A
-%0A
   const 
@@ -1609,17 +1609,16 @@
  ?? %5B%5D;%0A
-%0A
   return

Add navigation breadcrumbs