Skip to content

Commit 2e7ba40

Browse files
authored
feat: support integrations inside reusable content blocks (#4250)
1 parent b70b8fd commit 2e7ba40

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

bun.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"catalog": {
4444
"@tsconfig/strictest": "^2.0.6",
4545
"@tsconfig/node20": "^20.1.6",
46-
"@gitbook/api": "0.180.0",
46+
"@gitbook/api": "0.181.0",
4747
"@scalar/api-client-react": "^1.3.46",
4848
"@types/react": "^19.0.0",
4949
"@types/react-dom": "^19.0.0",

packages/gitbook/src/components/DocumentView/Integration/IntegrationBlock.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import { renderIntegrationUi } from './server-actions';
1212
export async function IntegrationBlock(props: BlockProps<DocumentBlockIntegration>) {
1313
const { block, context, style } = props;
1414

15-
if (!context.contentContext?.space) {
16-
throw new Error('integration block requires a content.spaceId');
15+
if (!context.contentContext) {
16+
throw new Error('Expected a content context to render an block');
17+
}
18+
19+
if (!context.contentContext.space && !block.meta?.spaceId) {
20+
throw new Error('integration block requires a spaceId from the context or API');
1721
}
1822

1923
const initialInput: RenderIntegrationUI = {
@@ -22,16 +26,28 @@ export async function IntegrationBlock(props: BlockProps<DocumentBlockIntegratio
2226
action: block.data.action,
2327
context: {
2428
type: 'document',
25-
spaceId: context.contentContext?.space.id,
29+
// When the block originates from a cross-space reusable content, the server adds a spaceId so the integration is
30+
// looked up in the correct source space.
31+
spaceId: block.meta?.spaceId ?? context.contentContext.space.id,
2632
editable: false,
2733
theme: 'light', // TODO: how to handle this without moving rendering to the client side?
2834
},
2935
};
3036

31-
const initialResponse = await fetchSafeIntegrationUI(context.contentContext, {
32-
integrationName: block.data.integration,
33-
request: initialInput,
34-
});
37+
const dataFetcher = block.meta?.token
38+
? context.contentContext.dataFetcher.withToken({ apiToken: block.meta.token })
39+
: context.contentContext.dataFetcher;
40+
41+
const initialResponse = await fetchSafeIntegrationUI(
42+
{
43+
...context.contentContext,
44+
dataFetcher,
45+
},
46+
{
47+
integrationName: block.data.integration,
48+
request: initialInput,
49+
}
50+
);
3551

3652
if (initialResponse.error) {
3753
if (initialResponse.error.code === 404) {

0 commit comments

Comments
 (0)