-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-js): Add usePathnameWithoutSplatRouteParams to handle splat…
… routes
- Loading branch information
Showing
2 changed files
with
25 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/remix/src/client/usePathnameWithoutSplatRouteParams.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useLocation, useParams } from '@remix-run/react'; | ||
|
||
export const usePathnameWithoutSplatRouteParams = () => { | ||
const params = useParams(); | ||
const { pathname } = useLocation(); | ||
|
||
// Get the splat route params | ||
// Remix store splat route params in an object with a key of '*' | ||
// If there are no splat route params, we fallback to an empty string | ||
const splatRouteParam = params['*'] || ''; | ||
|
||
// Remove the splat route param from the pathname | ||
// so we end up with the pathname where the components are mounted at | ||
// eg /user/123/profile/security will return /user/123/profile as the path | ||
const path = pathname.replace(splatRouteParam, '').replace(/\/$/, '').replace(/^\//, '').trim(); | ||
|
||
return `/${path}`; | ||
}; |