@@ -9,6 +9,10 @@ import {
99 getDocument ,
1010 getSpaceData ,
1111 ContentTarget ,
12+ SiteContentPointer ,
13+ getSiteSpaceData ,
14+ getSite ,
15+ getSiteSpaces ,
1216} from '@/lib/api' ;
1317import { resolvePagePath , resolvePageId } from '@/lib/pages' ;
1418
@@ -23,7 +27,7 @@ export interface PageIdParams {
2327/**
2428 * Get the current content pointer from the params.
2529 */
26- export function getContentPointer ( ) {
30+ export function getContentPointer ( ) : ContentPointer | SiteContentPointer {
2731 const headerSet = headers ( ) ;
2832 const spaceId = headerSet . get ( 'x-gitbook-content-space' ) ;
2933 if ( ! spaceId ) {
@@ -32,22 +36,46 @@ export function getContentPointer() {
3236 ) ;
3337 }
3438
35- const content : ContentPointer = {
36- spaceId,
37- revisionId : headerSet . get ( 'x-gitbook-content-revision' ) ?? undefined ,
38- changeRequestId : headerSet . get ( 'x-gitbook-content-changerequest' ) ?? undefined ,
39- } ;
39+ const siteId = headerSet . get ( 'x-gitbook-content-site' ) ;
40+ if ( siteId ) {
41+ const organizationId = headerSet . get ( 'x-gitbook-content-organization' ) ;
42+ const siteSpaceId = headerSet . get ( 'x-gitbook-content-site-space' ) ;
43+ if ( ! organizationId || ! siteSpaceId ) {
44+ throw new Error ( 'Missing site content headers' ) ;
45+ }
4046
41- return content ;
47+ const siteContent : SiteContentPointer = {
48+ siteId,
49+ spaceId,
50+ siteSpaceId,
51+ organizationId,
52+ revisionId : headerSet . get ( 'x-gitbook-content-revision' ) ?? undefined ,
53+ changeRequestId : headerSet . get ( 'x-gitbook-content-changerequest' ) ?? undefined ,
54+ } ;
55+ return siteContent ;
56+ } else {
57+ const content : ContentPointer = {
58+ spaceId,
59+ revisionId : headerSet . get ( 'x-gitbook-content-revision' ) ?? undefined ,
60+ changeRequestId : headerSet . get ( 'x-gitbook-content-changerequest' ) ?? undefined ,
61+ } ;
62+ return content ;
63+ }
4264}
4365
4466/**
4567 * Fetch all the data needed to render the space layout.
4668 */
4769export async function fetchSpaceData ( ) {
4870 const content = getContentPointer ( ) ;
49- const { space, contentTarget, pages, customization, scripts } = await getSpaceData ( content ) ;
50- const collection = await fetchParentCollection ( space ) ;
71+
72+ const [ { space, contentTarget, pages, customization, scripts } , parentSite ] = await Promise . all (
73+ 'siteId' in content
74+ ? [ getSiteSpaceData ( content ) , fetchParentSite ( content . organizationId , content . siteId ) ]
75+ : [ getSpaceData ( content ) ] ,
76+ ) ;
77+
78+ const parent = await ( parentSite ?? fetchParentCollection ( space ) ) ;
5179
5280 return {
5381 content,
@@ -57,7 +85,7 @@ export async function fetchSpaceData() {
5785 customization,
5886 scripts,
5987 ancestors : [ ] ,
60- ...collection ,
88+ ...parent ,
6189 } ;
6290}
6391
@@ -133,12 +161,26 @@ async function resolvePage(
133161async function fetchParentCollection ( space : Space ) {
134162 const parentCollectionId =
135163 space . visibility === ContentVisibility . InCollection ? space . parent : undefined ;
136- const [ collection , collectionSpaces ] = await Promise . all ( [
164+ const [ collection , spaces ] = await Promise . all ( [
137165 parentCollectionId ? getCollection ( parentCollectionId ) : null ,
138166 parentCollectionId ? getCollectionSpaces ( parentCollectionId ) : ( [ ] as Space [ ] ) ,
139167 ] ) ;
140168
141- return { collection, collectionSpaces } ;
169+ return { parent : collection , spaces } ;
170+ }
171+
172+ async function fetchParentSite ( organizationId : string , siteId : string ) {
173+ const [ site , siteSpaces ] = await Promise . all ( [
174+ getSite ( organizationId , siteId ) ,
175+ getSiteSpaces ( organizationId , siteId ) ,
176+ ] ) ;
177+
178+ const spaces : Record < string , Space > = { } ;
179+ siteSpaces . forEach ( ( siteSpace ) => {
180+ spaces [ siteSpace . space . id ] = siteSpace . space ;
181+ } ) ;
182+
183+ return { parent : site , spaces : Object . values ( spaces ) } ;
142184}
143185
144186/**
0 commit comments