Skip to content

Commit

Permalink
fix(active-route): Navigation.isActiveRoute function updated to add t…
Browse files Browse the repository at this point in the history
…he current route logic
  • Loading branch information
mananjadhav committed Aug 2, 2021
1 parent dc79a3f commit 7fb86db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'underscore';
import React from 'react';
import {StackActions, DrawerActions} from '@react-navigation/native';
import {StackActions, DrawerActions, useLinkBuilder} from '@react-navigation/native';
import PropTypes from 'prop-types';
import Onyx from 'react-native-onyx';
import linkTo from './linkTo';
Expand Down Expand Up @@ -125,13 +125,22 @@ function dismissModal(shouldOpenDrawer = false) {
/**
* Check whether the passed route is currently Active or not.
*
* Previous implementation used navigationRef.current.getCurrentRoute().path, which was
* undefined in the first navigation. Hence, in our current solution, we're rebuilding
* the active route using the useLinkBuilder hook that takes routeName and params as the arguments.
*
* @param {String} routePath Path to check
* @return {Boolean} is active
*/
function isActive(routePath) {
function isActiveRoute(routePath) {
const buildLink = useLinkBuilder();

// We remove First forward slash from the URL before matching
const path = navigationRef.current && navigationRef.current.getCurrentRoute().path
? navigationRef.current.getCurrentRoute().path.substring(1)
const path = navigationRef.current && navigationRef.current.getCurrentRoute().name
? buildLink(
navigationRef.current.getCurrentRoute().name,
navigationRef.current.getCurrentRoute().params,
).substring(1)
: '';
return path === routePath;
}
Expand Down Expand Up @@ -167,7 +176,7 @@ DismissModal.defaultProps = {
export default {
navigate,
dismissModal,
isActive,
isActiveRoute,
goBack,
DismissModal,
closeDrawer,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/WorkspaceSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ const WorkspaceSidebar = ({translate, isSmallScreenWidth, policy}) => {
action: () => {
Navigation.navigate(ROUTES.getWorkspaceCardRoute(policy.id));
},
isActive: Navigation.isActive(ROUTES.getWorkspaceCardRoute(policy.id)),
isActive: Navigation.isActiveRoute(ROUTES.getWorkspaceCardRoute(policy.id)),
},
{
translationKey: 'common.people',
icon: Users,
action: () => {
Navigation.navigate(ROUTES.getWorkspacePeopleRoute(policy.id));
},
isActive: Navigation.isActive(ROUTES.getWorkspacePeopleRoute(policy.id)),
isActive: Navigation.isActiveRoute(ROUTES.getWorkspacePeopleRoute(policy.id)),
},
];

Expand Down

0 comments on commit 7fb86db

Please sign in to comment.