activeRoute.js 983 B

1234567891011121314151617181920212223242526272829303132
  1. import { isValidBranchParam } from "@/lib/frontend/params";
  2. export const PRIMARY_NAV = Object.freeze({
  3. EXPLORER: "explorer",
  4. SEARCH: "search",
  5. });
  6. /**
  7. * Determine which primary navigation item should be active based on pathname.
  8. *
  9. * Rules:
  10. * - Search is active when pathname matches "/:branch/search" (or below).
  11. * - Explorer is active for any "/:branch/..." route that is not search.
  12. * - If the first segment is not a valid branch, no nav item is active.
  13. *
  14. * @param {string} pathname
  15. * @returns {{ active: "explorer"|"search", branch: string } | null}
  16. */
  17. export function getPrimaryNavFromPathname(pathname) {
  18. if (typeof pathname !== "string" || !pathname.startsWith("/")) return null;
  19. const parts = pathname.split("/").filter(Boolean);
  20. if (parts.length === 0) return null;
  21. const branch = parts[0];
  22. if (!isValidBranchParam(branch)) return null;
  23. const active =
  24. parts[1] === "search" ? PRIMARY_NAV.SEARCH : PRIMARY_NAV.EXPLORER;
  25. return { active, branch };
  26. }