mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 09:20:21 -04:00
23c827bdda
- consolidate `top level menu` and `contextual menu` into nested menu on mobile - remove legacy mobile submenu
29 lines
996 B
TypeScript
29 lines
996 B
TypeScript
/**
|
|
* Pathname utilities for safe URL/path parsing.
|
|
* Use these instead of direct array indexing (e.g. pathname.split('/')[3]) to avoid undefined access.
|
|
*/
|
|
|
|
/**
|
|
* Extracts the pathname without query string or hash.
|
|
* Use with Next.js router: getPathnameWithoutQuery(router.asPath, router.pathname)
|
|
*/
|
|
export function getPathnameWithoutQuery(
|
|
asPath: string | undefined,
|
|
fallbackPathname: string
|
|
): string {
|
|
if (asPath === undefined || asPath === null) return fallbackPathname
|
|
const withoutQuery = asPath.split(/[?#]/)[0]
|
|
return withoutQuery ?? fallbackPathname
|
|
}
|
|
|
|
/**
|
|
* Returns the path segment at the given index, or undefined if out of bounds.
|
|
* Segments are from splitting on '/', e.g. '/org/my-org/team' → ['', 'org', 'my-org', 'team']
|
|
* Index 0 = '', 1 = 'org', 2 = 'my-org', 3 = 'team'
|
|
*/
|
|
export function getPathSegment(pathname: string, index: number): string | undefined {
|
|
const segments = pathname.split('/')
|
|
const segment = segments[index]
|
|
return segment
|
|
}
|