ProjectDropdown.utils.ts 1.0 KB

123456789101112131415161718192021222324
  1. import type { ParsedUrlQuery } from 'querystring'
  2. /**
  3. * Sanitizes a route for project navigation (e.g. when building href for project switch).
  4. * Truncates to avoid carrying project-specific dynamic path segments.
  5. * Uses route segments (not query params) to determine truncation, so non-path query keys
  6. * (e.g. sort, filter) do not incorrectly trigger or affect truncation.
  7. */
  8. export function sanitizeRoute(_route: string, _routerQueries: ParsedUrlQuery): string {
  9. const routeSegments = _route.split('/')
  10. const hasStorageBucketSegment = routeSegments.includes('[bucketId]')
  11. const hasSecurityAdvisorSegment = routeSegments.includes('[preset]')
  12. const hasOtherDynamicSegment = routeSegments.some(
  13. (s) => s.startsWith('[') && s.endsWith(']') && s !== '[ref]'
  14. )
  15. const needsTruncation =
  16. hasStorageBucketSegment || hasSecurityAdvisorSegment || hasOtherDynamicSegment
  17. if (!needsTruncation) return _route
  18. const sliceLength = hasStorageBucketSegment || hasSecurityAdvisorSegment ? 5 : 4
  19. return routeSegments.slice(0, sliceLength).join('/')
  20. }