Skip to content

Commit aca512e

Browse files
authored
Merge branch 'main' into stevenh/tooltip-link
2 parents 667a0cb + b4918f6 commit aca512e

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

packages/gitbook/src/components/Header/SpacesDropdown.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getSiteSpaceURL } from '@/lib/sites';
44
import { tcls } from '@/lib/tailwind';
55
import type { GitBookSiteContext } from '@v2/lib/context';
66
import { DropdownChevron, DropdownMenu } from './DropdownMenu';
7-
import { SpacesDropdownMenuItem } from './SpacesDropdownMenuItem';
7+
import { SpacesDropdownMenuItems } from './SpacesDropdownMenuItem';
88

99
export function SpacesDropdown(props: {
1010
context: GitBookSiteContext;
@@ -65,17 +65,14 @@ export function SpacesDropdown(props: {
6565
</div>
6666
}
6767
>
68-
{siteSpaces.map((otherSiteSpace, index) => (
69-
<SpacesDropdownMenuItem
70-
key={`${otherSiteSpace.id}-${index}`}
71-
variantSpace={{
72-
id: otherSiteSpace.id,
73-
title: otherSiteSpace.title,
74-
url: getSiteSpaceURL(context, otherSiteSpace),
75-
}}
76-
active={otherSiteSpace.id === siteSpace.id}
77-
/>
78-
))}
68+
<SpacesDropdownMenuItems
69+
slimSpaces={siteSpaces.map((space) => ({
70+
id: space.id,
71+
title: space.title,
72+
url: getSiteSpaceURL(context, space),
73+
}))}
74+
curPath={siteSpace.path}
75+
/>
7976
</DropdownMenu>
8077
);
8178
}

packages/gitbook/src/components/Header/SpacesDropdownMenuItem.tsx

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,28 @@ import { joinPath } from '@/lib/paths';
66
import { useCurrentPagePath } from '../hooks';
77
import { 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

2440
export 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

Comments
 (0)