use-top-nav-links.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { useMemo } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useAuthStore } from '@/stores/auth-store'
  4. import { useStatus } from '@/hooks/use-status'
  5. export type TopNavLink = {
  6. title: string
  7. href: string
  8. disabled?: boolean
  9. external?: boolean
  10. }
  11. // Default navigation configuration
  12. const DEFAULT_HEADER_NAV_MODULES = {
  13. home: true,
  14. console: true,
  15. pricing: { enabled: true, requireAuth: false },
  16. rankings: { enabled: true, requireAuth: false },
  17. docs: true,
  18. about: true,
  19. }
  20. function parseAccessModule(
  21. raw: unknown,
  22. fallback: { enabled: boolean; requireAuth: boolean }
  23. ) {
  24. if (
  25. typeof raw === 'boolean' ||
  26. typeof raw === 'string' ||
  27. typeof raw === 'number'
  28. ) {
  29. return {
  30. enabled: raw === true || raw === 'true' || raw === '1' || raw === 1,
  31. requireAuth: fallback.requireAuth,
  32. }
  33. }
  34. if (raw && typeof raw === 'object') {
  35. const record = raw as Record<string, unknown>
  36. return {
  37. enabled:
  38. typeof record.enabled === 'boolean' ? record.enabled : fallback.enabled,
  39. requireAuth:
  40. typeof record.requireAuth === 'boolean'
  41. ? record.requireAuth
  42. : fallback.requireAuth,
  43. }
  44. }
  45. return { ...fallback }
  46. }
  47. function parseHeaderNavModules(
  48. raw: unknown
  49. ): typeof DEFAULT_HEADER_NAV_MODULES {
  50. if (!raw || String(raw).trim() === '') {
  51. return DEFAULT_HEADER_NAV_MODULES
  52. }
  53. try {
  54. const parsed = JSON.parse(String(raw)) as Record<string, unknown>
  55. return {
  56. ...DEFAULT_HEADER_NAV_MODULES,
  57. ...parsed,
  58. pricing: parseAccessModule(
  59. parsed.pricing,
  60. DEFAULT_HEADER_NAV_MODULES.pricing
  61. ),
  62. rankings: parseAccessModule(
  63. parsed.rankings,
  64. DEFAULT_HEADER_NAV_MODULES.rankings
  65. ),
  66. }
  67. } catch {
  68. return DEFAULT_HEADER_NAV_MODULES
  69. }
  70. }
  71. /**
  72. * Generate top navigation links based on HeaderNavModules configuration from backend /api/status
  73. * Backend format example (stringified JSON):
  74. * {
  75. * home: true,
  76. * console: true,
  77. * pricing: { enabled: true, requireAuth: false },
  78. * rankings: { enabled: true, requireAuth: false },
  79. * docs: true,
  80. * about: true
  81. * }
  82. */
  83. export function useTopNavLinks(): TopNavLink[] {
  84. const { t } = useTranslation()
  85. const { status } = useStatus()
  86. const { auth } = useAuthStore()
  87. // Parse HeaderNavModules
  88. const modules = useMemo(() => {
  89. return parseHeaderNavModules(status?.HeaderNavModules)
  90. }, [status?.HeaderNavModules])
  91. // Documentation link (may be external)
  92. const docsLink: string | undefined = status?.docs_link as string | undefined
  93. const isAuthed = !!auth?.user
  94. const links: TopNavLink[] = []
  95. // Home
  96. if (modules?.home !== false) {
  97. links.push({ title: t('Home'), href: '/' })
  98. }
  99. // Console -> /dashboard (new console path)
  100. if (modules?.console !== false) {
  101. links.push({ title: t('Console'), href: '/dashboard' })
  102. }
  103. // Pricing
  104. const pricing = modules?.pricing
  105. if (pricing && typeof pricing === 'object' && pricing.enabled) {
  106. const disabled = pricing.requireAuth && !isAuthed
  107. links.push({ title: t('Model Square'), href: '/pricing', disabled })
  108. }
  109. // Rankings
  110. const rankings = modules?.rankings
  111. if (rankings && typeof rankings === 'object' && rankings.enabled) {
  112. const disabled = rankings.requireAuth && !isAuthed
  113. links.push({ title: t('Rankings'), href: '/rankings', disabled })
  114. }
  115. // Docs (supports external links)
  116. if (modules?.docs !== false) {
  117. if (docsLink) {
  118. links.push({ title: t('Docs'), href: docsLink, external: true })
  119. } else {
  120. links.push({ title: t('Docs'), href: '/docs' })
  121. }
  122. }
  123. // About
  124. if (modules?.about !== false) {
  125. links.push({ title: t('About'), href: '/about' })
  126. }
  127. return links
  128. }