@@ -6,12 +6,28 @@ import { joinPath } from '@/lib/paths';
66import { useCurrentPagePath } from '../hooks' ;
77import { DropdownMenuItem } from './DropdownMenu' ;
88
9- function useVariantSpaceHref ( variantSpaceUrl : string ) {
9+ interface VariantSpace {
10+ id : Space [ 'id' ] ;
11+ title : Space [ 'title' ] ;
12+ url : string ;
13+ }
14+
15+ // When switching to a different variant space, we reconstruct the URL by swapping the space path.
16+ function useVariantSpaceHref ( variantSpaceUrl : string , currentSpacePath : string , active = false ) {
1017 const currentPathname = useCurrentPagePath ( ) ;
1118
19+ // We need to ensure that the variant space URL is not the same as the current space path.
20+ // If it is, we return only the variant space URL to redirect to the root of the variant space.
21+ // This is necessary in case the currentPathname is the same as the variantSpaceUrl,
22+ // otherwise we would redirect to the same space if the variant space that we are switching to is the default one.
23+ if ( ! active && currentPathname . startsWith ( `${ currentSpacePath } /` ) ) {
24+ return variantSpaceUrl ;
25+ }
26+
1227 if ( URL . canParse ( variantSpaceUrl ) ) {
1328 const targetUrl = new URL ( variantSpaceUrl ) ;
1429 targetUrl . pathname = joinPath ( targetUrl . pathname , currentPathname ) ;
30+
1531 targetUrl . searchParams . set ( 'fallback' , 'true' ) ;
1632
1733 return targetUrl . toString ( ) ;
@@ -22,15 +38,36 @@ function useVariantSpaceHref(variantSpaceUrl: string) {
2238}
2339
2440export function SpacesDropdownMenuItem ( props : {
25- variantSpace : { id : Space [ 'id' ] ; title : Space [ 'title' ] ; url : string } ;
41+ variantSpace : VariantSpace ;
2642 active : boolean ;
43+ currentSpacePath : string ;
2744} ) {
28- const { variantSpace, active } = props ;
29- const variantHref = useVariantSpaceHref ( variantSpace . url ) ;
45+ const { variantSpace, active, currentSpacePath } = props ;
46+ const variantHref = useVariantSpaceHref ( variantSpace . url , currentSpacePath , active ) ;
3047
3148 return (
3249 < DropdownMenuItem key = { variantSpace . id } href = { variantHref } active = { active } >
3350 { variantSpace . title }
3451 </ DropdownMenuItem >
3552 ) ;
3653}
54+
55+ export function SpacesDropdownMenuItems ( props : {
56+ slimSpaces : VariantSpace [ ] ;
57+ curPath : string ;
58+ } ) {
59+ const { slimSpaces, curPath } = props ;
60+
61+ return (
62+ < >
63+ { slimSpaces . map ( ( space ) => (
64+ < SpacesDropdownMenuItem
65+ key = { space . id }
66+ variantSpace = { space }
67+ active = { false }
68+ currentSpacePath = { curPath }
69+ />
70+ ) ) }
71+ </ >
72+ ) ;
73+ }
0 commit comments