Skip to main content
Glama
CommunicatorContext.tsx1.46 kB
'use client'; import configuration from '@intlayer/config/built'; import { type ComponentChildren, createContext, type FunctionComponent, } from 'preact'; import { useContext, useMemo, useRef } from 'preact/hooks'; const randomUUID = () => Math.random().toString(36).slice(2); export type UseCrossPlatformStateProps = { postMessage: typeof window.postMessage; allowedOrigins?: string[]; senderId: string; }; const { editor } = configuration; const CommunicatorContext = createContext<UseCrossPlatformStateProps>({ postMessage: () => null, allowedOrigins: [ editor?.applicationURL, editor?.editorURL, editor?.cmsURL, ] as string[], senderId: '', }); export type CommunicatorProviderProps = { children?: ComponentChildren; postMessage: typeof window.postMessage; allowedOrigins?: string[]; }; export const CommunicatorProvider: FunctionComponent< CommunicatorProviderProps > = ({ children, allowedOrigins, postMessage }) => { // Create a stable, unique ID for the lifetime of this app/iframe instance. const senderIdRef = useRef(randomUUID()); const value = useMemo( () => ({ postMessage, allowedOrigins, senderId: senderIdRef.current }), [postMessage, allowedOrigins] // senderIdRef.current is stable ); return ( <CommunicatorContext.Provider value={value}> {children} </CommunicatorContext.Provider> ); }; export const useCommunicator = () => useContext(CommunicatorContext);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aymericzip/intlayer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server