MobileMenuContent.utils.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const projectIndex = segments.indexOf('project')
  8. if (projectIndex === -1 || segments.length <= projectIndex + 1) return null
  9. const refSegment = segments[projectIndex + 1]
  10. if (!refSegment || refSegment.startsWith('[')) return null
  11. const sectionSegment = segments[projectIndex + 2]
  12. if (!sectionSegment) return null
  13. return sectionSegment
  14. }
  15. export interface ResolveSectionDisplayParams {
  16. viewLevel: 'top' | 'section'
  17. selectedSectionKey: string | null
  18. currentSectionKey: string | null
  19. currentProduct: string
  20. routes: Array<{ key: string; label: string }>
  21. }
  22. export interface SectionDisplay {
  23. sectionKey: string | null
  24. sectionLabel: string | null
  25. }
  26. /**
  27. * Resolves which section to show and its label for the mobile menu.
  28. * When in section view: uses selectedSectionKey (user clicked) or falls back to currentSectionKey.
  29. * Label resolves from currentProduct when matching the current section, otherwise from route labels.
  30. */
  31. export function resolveSectionDisplay({
  32. viewLevel,
  33. selectedSectionKey,
  34. currentSectionKey,
  35. currentProduct,
  36. routes,
  37. }: ResolveSectionDisplayParams): SectionDisplay {
  38. if (viewLevel !== 'section') {
  39. return { sectionKey: null, sectionLabel: null }
  40. }
  41. const sectionKey = selectedSectionKey ?? currentSectionKey
  42. if (!sectionKey) {
  43. return { sectionKey: null, sectionLabel: null }
  44. }
  45. const isCurrentSection = sectionKey === currentSectionKey
  46. if (isCurrentSection) {
  47. return { sectionKey, sectionLabel: currentProduct }
  48. }
  49. const matchingRoute = routes.find((r) => r.key === sectionKey)
  50. const sectionLabel = matchingRoute?.label ?? sectionKey
  51. return { sectionKey, sectionLabel }
  52. }