Skip to content

Commit 5db3cbc

Browse files
authored
Render Embed blocks that can be handled by an integration in the Space. (#2302)
1 parent f8d148b commit 5db3cbc

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

bun.lockb

-384 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
],
2121
"dependencies": {
2222
"@geist-ui/icons": "^1.0.2",
23-
"@gitbook/api": "^0.43.0",
23+
"@gitbook/api": "^0.46.0",
2424
"@radix-ui/react-checkbox": "^1.0.4",
2525
"@radix-ui/react-popover": "^1.0.7",
2626
"@sentry/nextjs": "^7.94.1",

packages/react-contentkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"exports": "./src/index.ts",
44
"dependencies": {
55
"classnames": "^2.5.1",
6-
"@gitbook/api": "^0.41.0",
6+
"@gitbook/api": "^0.46.0",
77
"assert-never": "^1.2.1"
88
},
99
"peerDependencies": {

src/components/DocumentView/Embed.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DocumentBlockEmbed } from '@gitbook/api';
1+
import * as gitbookAPI from '@gitbook/api';
22
import Script from 'next/script';
33

44
import { Card } from '@/components/primitives';
@@ -7,11 +7,14 @@ import { tcls } from '@/lib/tailwind';
77

88
import { BlockProps } from './Block';
99
import { Caption } from './Caption';
10+
import { IntegrationBlock } from './Integration';
1011

11-
export async function Embed(props: BlockProps<DocumentBlockEmbed>) {
12-
const { block } = props;
12+
export async function Embed(props: BlockProps<gitbookAPI.DocumentBlockEmbed>) {
13+
const { block, context, ...otherProps } = props;
1314

14-
const { data: embed } = await api().urls.getEmbedByUrl({ url: block.data.url });
15+
const { data: embed } = await (context.content
16+
? api().spaces.getEmbedByUrlInSpace(context.content.spaceId, { url: block.data.url })
17+
: api().urls.getEmbedByUrl({ url: block.data.url }));
1518

1619
return (
1720
<Caption {...props}>
@@ -25,6 +28,12 @@ export async function Embed(props: BlockProps<DocumentBlockEmbed>) {
2528
{/* We load the iframely script to resize the embed iframes dynamically */}
2629
<Script src="https://cdn.iframe.ly/embed.js" defer async />
2730
</>
31+
) : embed.type === 'integration' ? (
32+
<IntegrationBlock
33+
{...otherProps}
34+
context={context}
35+
block={createIntegrationBlock(block.data.url, embed.integration, embed.block)}
36+
/>
2837
) : (
2938
<Card
3039
leadingIcon={
@@ -40,3 +49,28 @@ export async function Embed(props: BlockProps<DocumentBlockEmbed>) {
4049
</Caption>
4150
);
4251
}
52+
53+
/**
54+
* Create an integration block with an unfurl action from the GitBook Embed response.
55+
*/
56+
function createIntegrationBlock(
57+
url: string,
58+
integration: string,
59+
block: gitbookAPI.IntegrationBlock,
60+
): gitbookAPI.DocumentBlockIntegration {
61+
return {
62+
object: 'block',
63+
type: 'integration',
64+
isVoid: true,
65+
data: {
66+
integration,
67+
block: block.id,
68+
props: {},
69+
action: {
70+
action: '@link.unfurl',
71+
url,
72+
},
73+
url,
74+
},
75+
};
76+
}

src/components/DocumentView/Integration/IntegrationBlock.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export async function IntegrationBlock(props: BlockProps<DocumentBlockIntegratio
7070
const initialInput = {
7171
componentId: block.data.block,
7272
props: block.data.props,
73+
action: block.data.action,
7374
context: contentKitContext,
7475
};
7576

0 commit comments

Comments
 (0)