Skip to content

Commit c4ab0e4

Browse files
committed
Resolving comments and improving types
1 parent fe79cdf commit c4ab0e4

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

packages/gitbook/src/app/(site)/(content)/layout.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export default async function ContentLayout(props: { children: React.ReactNode }
4040
ancestors,
4141
scripts,
4242
sections,
43-
section,
4443
} = await fetchContentData();
4544

4645
ReactDOM.preconnect(api().endpoint);
@@ -69,7 +68,6 @@ export default async function ContentLayout(props: { children: React.ReactNode }
6968
site={site}
7069
spaces={spaces}
7170
sections={sections}
72-
section={section}
7371
customization={customization}
7472
pages={pages}
7573
ancestors={ancestors}

packages/gitbook/src/app/(site)/fetch.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { RevisionPage, SiteSpace, Space } from '@gitbook/api';
1+
import { RevisionPage, SiteSection, SiteSpace, Space } from '@gitbook/api';
2+
import { assert } from 'ts-essentials';
23

34
import {
45
getRevisionPageByPath,
@@ -20,6 +21,8 @@ export interface PageIdParams {
2021
pageId: string;
2122
}
2223

24+
type SectionsList = { list: SiteSection[]; section: SiteSection; };
25+
2326
/**
2427
* Fetch all the data needed to render the content layout.
2528
*/
@@ -33,17 +36,15 @@ export async function fetchContentData() {
3336
organizationId: content.organizationId,
3437
siteId: content.siteId,
3538
siteShareKey: content.siteShareKey,
36-
siteSpaceId: content.siteSpaceId,
3739
}),
3840
]);
3941

4042
const site = siteStructure.site;
4143

42-
const sections = siteStructure.sections;
43-
const section = siteStructure.sections?.find((section) => section.id === content.siteSectionId);
44-
44+
const siteSections = getSiteSectionsList(content.siteSectionId, siteStructure.sections);
45+
4546
const spaces =
46-
siteStructure.spaces ?? (section ? parseSpacesFromSiteSpaces(section.siteSpaces) : []);
47+
siteStructure.spaces ?? (siteSections ? parseSpacesFromSiteSpaces(siteSections.section.siteSpaces) : []);
4748

4849
// we grab the space attached to the parent as it contains overriden customizations
4950
const spaceRelativeToParent = spaces?.find((space) => space.id === content.spaceId);
@@ -53,8 +54,7 @@ export async function fetchContentData() {
5354
contentTarget,
5455
space: spaceRelativeToParent ?? space,
5556
pages,
56-
sections,
57-
section,
57+
sections: siteSections,
5858
site,
5959
spaces,
6060
customization,
@@ -63,6 +63,19 @@ export async function fetchContentData() {
6363
};
6464
}
6565

66+
function getSiteSectionsList(siteSectionId?: string, sections?: SiteSection[] | null) {
67+
if (!sections) {
68+
return null;
69+
}
70+
const section = sections?.find((section) => section.id === siteSectionId);
71+
assert(sectionIsDefined(section), "A section must be defined when there are multiple sections");
72+
return { list: sections, section } satisfies SectionsList;
73+
}
74+
75+
function sectionIsDefined(section?: SiteSection): section is NonNullable<SiteSection> {
76+
return typeof section !== 'undefined' && section !== null;
77+
}
78+
6679
/**
6780
* Fetch all the data needed to render the content.
6881
* Optimized to fetch in parallel as much as possible.
@@ -77,7 +90,6 @@ export async function fetchPageData(params: PagePathParams | PageIdParams) {
7790
organizationId: content.organizationId,
7891
siteId: content.siteId,
7992
siteShareKey: content.siteShareKey,
80-
siteSpaceId: content.siteSpaceId,
8193
}),
8294
page?.page.documentId ? getDocument(space.id, page.page.documentId) : null,
8395
]);
@@ -139,13 +151,12 @@ async function resolvePage(
139151

140152
/**
141153
* Fetch the structure of an organization site.
142-
* This includes the site and its sections and spaces.
154+
* This includes the site and its sections or spaces.
143155
*/
144156
async function fetchSiteStructure(args: {
145157
organizationId: string;
146158
siteId: string;
147159
siteShareKey: string | undefined;
148-
siteSpaceId: string | undefined;
149160
}) {
150161
const { organizationId, siteId, siteShareKey } = args;
151162
const [orgSite, siteStructure, siteParentCustomizations] = await Promise.all([

packages/gitbook/src/components/SiteSectionTabs/SiteSectionTab.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { tcls } from '@/lib/tailwind';
77

88
import { Button, Link } from '../primitives';
99

10-
export function SiteSectionTabs(props: { sections: SiteSection[]; section: SiteSection }) {
10+
/**
11+
* A set of tabs representing site sections for multi-section sites
12+
*/
13+
export function SiteSectionTabs(props: { sections: SiteSection[], section: SiteSection }) {
1114
const { sections, section: currentSection } = props;
1215

1316
const tabs = sections.map((section) => ({
@@ -89,6 +92,9 @@ export function SiteSectionTabs(props: { sections: SiteSection[]; section: SiteS
8992
) : null;
9093
}
9194

95+
/**
96+
* The tab item - a link to a site section
97+
*/
9298
const Tab = React.forwardRef<
9399
HTMLAnchorElement,
94100
{ active: boolean; href: string; label: string; onClick: any }
@@ -114,6 +120,9 @@ const Tab = React.forwardRef<
114120
);
115121
});
116122

123+
/**
124+
* Dropdown trigger for when there are too many sections to show them all
125+
*/
117126
function MoreSectionsButton() {
118127
return (
119128
<div>

packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ export function SpaceLayout(props: {
3131
contentTarget: ContentTarget;
3232
space: Space;
3333
site: Site | null;
34-
sections: SiteSection[] | null;
35-
section?: SiteSection;
34+
sections: { list: SiteSection[], section: SiteSection } | null;
3635
spaces: Space[];
3736
customization: CustomizationSettings | SiteCustomizationSettings;
3837
pages: Revision['pages'];
@@ -44,7 +43,6 @@ export function SpaceLayout(props: {
4443
contentTarget,
4544
site,
4645
sections,
47-
section,
4846
spaces,
4947
content,
5048
pages,
@@ -74,9 +72,9 @@ export function SpaceLayout(props: {
7472
customization={customization}
7573
/>
7674
<div className={tcls('scroll-nojump')}>
77-
{sections && section ? (
75+
{sections ? (
7876
<div className={tcls(CONTAINER_STYLE)}>
79-
<SiteSectionTabs sections={sections} section={section} />
77+
<SiteSectionTabs sections={sections.list} section={sections.section} />
8078
</div>
8179
) : null}
8280
<div

0 commit comments

Comments
 (0)