Files
Samy Pessé 70c6c9df42 First pass at implementing ContentKit (#69)
* Start

* Try webframe

* Lint and format

* Move to package

* Continue

* Implement card and cleanup contexts

* Start code blocks

* Start rendering image

* Continue with card

* Start stack

* Start text

* Start modal

* Format

* Continue

* Fix TS errors

* Cleanup

* Make markdown work with server components

* Format

* Add visual test

* Lint and format

* Fix lifecycle

* Implement textinput

* Divider

* Remove console logs

* Remove listSpaceIntegrationsBlocks
2024-02-28 14:46:47 +01:00

58 lines
1.5 KiB
TypeScript

import { ContentKitAction, RequestRenderIntegrationUI } from '@gitbook/api';
import * as React from 'react';
/**
* Security restrictions for elements.
*/
export interface ContentKitSecurity {
/**
* Domains allowed for webframe communication.
* These domains will be allowed to send messages to ContentKit,
* and receive messages from ContentKit.
*/
firstPartyDomains: string[];
}
export type ContentKitRenderUpdate = Partial<
Pick<RequestRenderIntegrationUI, 'action' | 'props' | 'state'>
>;
export interface ContentKitClientContextType {
security: ContentKitSecurity;
/**
* Current value of the state.
*/
state: object;
/**
* Update the state.
*/
setState: React.Dispatch<React.SetStateAction<object>>;
/**
* Re-render the ContentKit component with a new set of props/state or an action.
*/
update: (newState: ContentKitRenderUpdate) => Promise<void>;
/**
* Dispatch an action.
*/
dispatchAction: (action: ContentKitAction, bubble?: boolean) => Promise<void>;
}
export const ContentKitClientContext = React.createContext<ContentKitClientContextType | null>(
null,
);
/**
* Get the current contentkit context.
*/
export function useContentKitClientContext(): ContentKitClientContextType {
const context = React.useContext(ContentKitClientContext);
if (!context) {
throw new Error('ContentKit component should be wrapped in <ContentKit>');
}
return context;
}