Skip to content

Commit

Permalink
Fix navigation for preview links
Browse files Browse the repository at this point in the history
The navigation did not work in preview links because the branch name was in the
url. This resulted in `window.location.pathname` returning wrong pathname.
  • Loading branch information
michalsmiarowski committed Jul 26, 2023
1 parent 780e859 commit 3113a7e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/components/SubNavigationPills/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,22 @@ const getPathMatches = (pills: RouteProps[], parentPathBase: string = "") => {
for (let i = 0; i < pills.length; i++) {
const { path, pathOverride } = pills[i]
const location = window.location.pathname
// This is a workaround for preview links. We have to remove the branch name
// from the pathname
const currentPathname =
location.includes(parentPathBase) &&
location.indexOf(parentPathBase) !== 0
? location.substring(location.indexOf(parentPathBase), location.length)
: location
const resolved = resolvePath(
pathOverride
? `${parentPathBase}/${pathOverride}`
: `${parentPathBase}/${path}`
)
const match = matchPath({ path: resolved.pathname, end: true }, location)
const match = matchPath(
{ path: resolved.pathname, end: true },
currentPathname
)
pathMatches.push({
index: i,
path,
Expand All @@ -130,7 +140,6 @@ const getActivePillIndex = (pills: RouteProps[], parentPathBase: string) => {
const matchedPaths = pathMatches.filter((_) => {
return !!_.match
})

if (matchedPaths.length === 0) return undefined
if (matchedPaths.length === 1) return matchedPaths[0].index

Expand Down

0 comments on commit 3113a7e

Please sign in to comment.