import { ActionRejectedPayload } from '../../_contract-src/event-payloads/action-rejected/action-rejected-payload'; import { EventType } from '../../_contract-src/event-payloads/event-type'; import type { StructEventMessage } from '../../_contract-src/shared/struct-event-message'; import { isAllowedOrigin } from '../../_contract-src/setup/configure'; /** * Registers a listener for the event from Struct PIM where an action has been rejected. * Called whenever the host rejects an action that is not supported for the current extension point type. * * The callback receives a {@link ActionRejectedPayload} containing the rejected action type and a reason string * explaining why it was rejected. For example, calling resizeContainer from a Widget extension point * will trigger this event since Widget sizing is controlled by the dashboard grid. * * @example * ```ts * struct.events.onActionRejectedEvent((payload) => { * console.warn(`Action "${payload.rejectedAction}" rejected: ${payload.reason}`); * }); * ``` */ export function onActionRejectedEvent(callback: (payload: ActionRejectedPayload) => void): () => void { const handler = (event: MessageEvent>) => { if (event.data?.eventType !== EventType.ACTION_REJECTED) return; if (!isAllowedOrigin(event.origin)) return; callback(event.data.payload); }; window.addEventListener('message', handler); return () => window.removeEventListener('message', handler); }