Skip to content

Commit

Permalink
fix: use correct params type
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagebakers committed Jul 17, 2022
1 parent 021538c commit 7bb5d3f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-starfishes-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@saas-ui/provider': patch
---

Use correct params type
28 changes: 25 additions & 3 deletions packages/saas-ui-provider/src/router-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import type { ParsedUrlQuery } from 'querystring'

interface NavigateOptions {
replace?: boolean
Expand All @@ -13,7 +14,7 @@ interface RouterLocation {
export interface RouterContextValue {
navigate: (path: string, options?: NavigateOptions) => void
back: () => void
params?: URLSearchParams
params?: ParsedUrlQuery
location?: RouterLocation
}

Expand Down Expand Up @@ -49,7 +50,28 @@ export const useLocation = () => {
return null
}

export function useActivePath(path: string) {
export interface UseActivePathOptions {
/**
* Set to false to match the first parth of the path.
* eg: /contacts will match /contacts/people
*/
end?: boolean
}

/**
* Matches the given path to the current active path.
* @param path string
* @param options UseActivePathOptions
* @returns boolean
*/
export function useActivePath(
path: string,
options: UseActivePathOptions = {}
) {
const { end = true } = options
const location = useLocation()
return location?.pathname === path
return !!React.useMemo(
() => location?.pathname.match(new RegExp(`${path}${end ? '$' : ''}`)),
[location?.pathname, path, options]
)
}

0 comments on commit 7bb5d3f

Please sign in to comment.