@@ -12,8 +12,12 @@ import { renderIntegrationUi } from './server-actions';
1212export 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