Skip to content

Commit b51bee9

Browse files
committed
Experiment with hiding sections without a matching site space
1 parent 28a9ee7 commit b51bee9

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

packages/gitbook/src/components/SiteSections/encodeClientSiteSections.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getSectionURL, getSiteSpaceURL } from '@/lib/sites';
2-
import type { SiteSection, SiteSectionGroup } from '@gitbook/api';
2+
import type { SiteSection, SiteSectionGroup, SiteSpace } from '@gitbook/api';
33
import type { GitBookSiteContext, SiteSections } from '@v2/lib/context';
44

55
export type ClientSiteSections = {
@@ -33,10 +33,14 @@ export function encodeClientSiteSections(context: GitBookSiteContext, sections:
3333
title: item.title,
3434
icon: item.icon,
3535
object: item.object,
36-
sections: item.sections.map((section) => encodeSection(context, section)),
36+
sections: item.sections
37+
.filter((section) => shouldIncludeSection(context, section))
38+
.map((section) => encodeSection(context, section)),
3739
});
3840
} else {
39-
clientSections.push(encodeSection(context, item));
41+
if (shouldIncludeSection(context, item)) {
42+
clientSections.push(encodeSection(context, item));
43+
}
4044
}
4145
}
4246

@@ -57,6 +61,28 @@ function encodeSection(context: GitBookSiteContext, section: SiteSection) {
5761
};
5862
}
5963

64+
/**
65+
* Test if a section should be included in the list of sections.
66+
*/
67+
function shouldIncludeSection(context: GitBookSiteContext, section: SiteSection) {
68+
if (context.site.id !== 'site_JOVzv') {
69+
return true;
70+
}
71+
72+
// Testing for a new mode of navigation where the multi-variants section are hidden
73+
// if they do not include an equivalent of the current site space.
74+
75+
const { siteSpace: currentSiteSpace } = context;
76+
77+
if (section.siteSpaces.length === 1) {
78+
return true;
79+
}
80+
81+
return section.siteSpaces.some((siteSpace) =>
82+
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
83+
);
84+
}
85+
6086
/**
6187
* Find the best default site space to navigate to for a givent section:
6288
* 1. If we are on the default, continue on the default.
@@ -70,12 +96,19 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
7096
return getSectionURL(context, section);
7197
}
7298

73-
const bestMatch = section.siteSpaces.find(
74-
(siteSpace) => siteSpace.path === currentSiteSpace.path
99+
const bestMatch = section.siteSpaces.find((siteSpace) =>
100+
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
75101
);
76102
if (bestMatch) {
77103
return getSiteSpaceURL(context, bestMatch);
78104
}
79105

80106
return getSectionURL(context, section);
81107
}
108+
109+
/**
110+
* Test if 2 site spaces are equivalent.
111+
*/
112+
function areSiteSpacesEquivalent(siteSpace1: SiteSpace, siteSpace2: SiteSpace) {
113+
return siteSpace1.path === siteSpace2.path;
114+
}

0 commit comments

Comments
 (0)