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