Skip to content

Commit 8a3910e

Browse files
authored
Experiment with hiding sections without a matching site space (#3318)
1 parent 6294bbb commit 8a3910e

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

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

Lines changed: 43 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,33 @@ 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+
// TODO: replace with a proper flag on the section
76+
const withNavigateOnlyIfEquivalent = section.id === 'sitesc_4jvEm';
77+
78+
if (!withNavigateOnlyIfEquivalent) {
79+
return true;
80+
}
81+
82+
const { siteSpace: currentSiteSpace } = context;
83+
if (section.siteSpaces.length === 1) {
84+
return true;
85+
}
86+
return section.siteSpaces.some((siteSpace) =>
87+
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
88+
);
89+
}
90+
6091
/**
6192
* Find the best default site space to navigate to for a givent section:
6293
* 1. If we are on the default, continue on the default.
@@ -70,12 +101,19 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
70101
return getSectionURL(context, section);
71102
}
72103

73-
const bestMatch = section.siteSpaces.find(
74-
(siteSpace) => siteSpace.path === currentSiteSpace.path
104+
const bestMatch = section.siteSpaces.find((siteSpace) =>
105+
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
75106
);
76107
if (bestMatch) {
77108
return getSiteSpaceURL(context, bestMatch);
78109
}
79110

80111
return getSectionURL(context, section);
81112
}
113+
114+
/**
115+
* Test if 2 site spaces are equivalent.
116+
*/
117+
function areSiteSpacesEquivalent(siteSpace1: SiteSpace, siteSpace2: SiteSpace) {
118+
return siteSpace1.path === siteSpace2.path;
119+
}

0 commit comments

Comments
 (0)