import * as React from 'react'; import type { ContentKitAction, RequestRenderIntegrationUI } from '@gitbook/api'; /** * 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 >; /** * The current page exposed to a webframe through the client-only webframe state. */ export type ContentKitWebframePage = { id: string; /** Path of the page relative to the site root. */ path: string; title: string; }; export type ContentKitClientContextData = { /** * Client-only visitor claims, merged into the webframe state. * Gated by the integration's visitor-claims scope. */ getVisitorContext?: () => | Record | null | undefined | Promise | null | undefined>; /** * Client-only current-page context, merged into the webframe state. */ getPageContext?: () => | { page: ContentKitWebframePage } | null | undefined | Promise<{ page: ContentKitWebframePage } | null | undefined>; /** * Navigate the host page to another page, in response to a webframe `@webframe.navigate` * action. The destination is addressed by `path` (resolved against the site base path); the * host restricts navigation to destinations within the current site. */ navigate?: (target: { path: string; anchor?: string; query?: Record }) => void; }; export interface ContentKitClientContextType { security: ContentKitSecurity; /** * Client-only contextual data for client-rendered elements such as webframes. * This data must not be included in integration render requests. */ clientContext?: ContentKitClientContextData; /** * Current value of the state. */ state: object; /** * Update the state. */ setState: React.Dispatch>; /** * Re-render the ContentKit component with a new set of props/state or an action. */ update: (newState: ContentKitRenderUpdate) => Promise; /** * Dispatch an action. */ dispatchAction: (action: ContentKitAction, bubble?: boolean) => Promise; } export const ContentKitClientContext = React.createContext( 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 '); } return context; }