-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12db4c3
commit 7ecefd7
Showing
4 changed files
with
55 additions
and
5 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
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
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,47 @@ | ||
import { useParams, usePathname, useSearchParams } from 'next/navigation' | ||
|
||
// https://github.com/vercel/speed-insights/blob/main/packages/web/src/nextjs/utils.ts | ||
export const useRoute = (): string | null => { | ||
const params = useParams() | ||
const searchParams = useSearchParams() | ||
const path = usePathname() | ||
|
||
const finalParams = { | ||
...Object.fromEntries(searchParams.entries()), | ||
...(params || {}) | ||
} | ||
|
||
return params ? computeRoute(path, finalParams) : path | ||
} | ||
|
||
function computeRoute( | ||
pathname: string | null, | ||
pathParams: Record<string, string | string[]> | null | ||
): string | null { | ||
if (!pathname || !pathParams) { | ||
return pathname | ||
} | ||
|
||
let result = pathname | ||
|
||
try { | ||
for (const [key, valueOrArray] of Object.entries(pathParams)) { | ||
const isValueArray = Array.isArray(valueOrArray) | ||
const value = isValueArray ? valueOrArray.join('/') : valueOrArray | ||
const expr = isValueArray ? `...${key}` : key | ||
|
||
const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`) | ||
if (matcher.test(result)) { | ||
result = result.replace(matcher, `/[${expr}]`) | ||
} | ||
} | ||
|
||
return result | ||
} catch (e) { | ||
return pathname | ||
} | ||
} | ||
|
||
function escapeRegExp(string: string): string { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
} |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
export type TrackableEvents = { | ||
'Opened a modal': { Extra?: Record<string, unknown>; Modal: string } | ||
'Signed In': { Provider?: string; 'First Time': boolean } | ||
'Signed in': { Provider?: string; 'First Time': boolean } | ||
'Pressed a button': { Button: string; Extra?: Record<string, unknown> } | ||
'Viewed a page': { | ||
Path: string | ||
Ref?: string | null | ||
Route: string | ||
Search?: Record<string, unknown> | ||
} | ||
} |