MobileMenuContent.utils.ts64 lines · main
| 1 | /** |
| 2 | * Derives the current section key from the project pathname. |
| 3 | * e.g. /project/[ref]/database/schemas → 'database', /project/[ref] → null (home). |
| 4 | */ |
| 5 | export function getSectionKeyFromPathname(pathname: string): string | null { |
| 6 | const segments = pathname.split('/').filter(Boolean) |
| 7 | |
| 8 | const projectIndex = segments.indexOf('project') |
| 9 | if (projectIndex === -1 || segments.length <= projectIndex + 1) return null |
| 10 | |
| 11 | const refSegment = segments[projectIndex + 1] |
| 12 | if (!refSegment || refSegment.startsWith('[')) return null |
| 13 | |
| 14 | const sectionSegment = segments[projectIndex + 2] |
| 15 | if (!sectionSegment) return null |
| 16 | |
| 17 | return sectionSegment |
| 18 | } |
| 19 | |
| 20 | export interface ResolveSectionDisplayParams { |
| 21 | viewLevel: 'top' | 'section' |
| 22 | selectedSectionKey: string | null |
| 23 | currentSectionKey: string | null |
| 24 | currentProduct: string |
| 25 | routes: Array<{ key: string; label: string }> |
| 26 | } |
| 27 | |
| 28 | export interface SectionDisplay { |
| 29 | sectionKey: string | null |
| 30 | sectionLabel: string | null |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Resolves which section to show and its label for the mobile menu. |
| 35 | * When in section view: uses selectedSectionKey (user clicked) or falls back to currentSectionKey. |
| 36 | * Label resolves from currentProduct when matching the current section, otherwise from route labels. |
| 37 | */ |
| 38 | export function resolveSectionDisplay({ |
| 39 | viewLevel, |
| 40 | selectedSectionKey, |
| 41 | currentSectionKey, |
| 42 | currentProduct, |
| 43 | routes, |
| 44 | }: ResolveSectionDisplayParams): SectionDisplay { |
| 45 | if (viewLevel !== 'section') { |
| 46 | return { sectionKey: null, sectionLabel: null } |
| 47 | } |
| 48 | |
| 49 | const sectionKey = selectedSectionKey ?? currentSectionKey |
| 50 | |
| 51 | if (!sectionKey) { |
| 52 | return { sectionKey: null, sectionLabel: null } |
| 53 | } |
| 54 | |
| 55 | const isCurrentSection = sectionKey === currentSectionKey |
| 56 | if (isCurrentSection) { |
| 57 | return { sectionKey, sectionLabel: currentProduct } |
| 58 | } |
| 59 | |
| 60 | const matchingRoute = routes.find((r) => r.key === sectionKey) |
| 61 | const sectionLabel = matchingRoute?.label ?? sectionKey |
| 62 | |
| 63 | return { sectionKey, sectionLabel } |
| 64 | } |