hedgedoc/frontend/src/components/render-page/window-post-message-communicator/hooks/use-send-to-renderer.ts
Tilman Vatteroth 61362f9175 fix(frontend): migrate type changes
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2023-10-20 13:35:54 +00:00

28 lines
1 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useEditorToRendererCommunicator } from '../../../editor-page/render-context/editor-to-renderer-communicator-context-provider'
import type { CommunicationMessages, EditorToRendererMessageType } from '../rendering-message'
import type { MessagePayload } from '../window-post-message-communicator'
import { useEffect } from 'react'
/**
* Sends the given message to the renderer.
*
* @param message The message to send
* @param rendererReady Defines if the target renderer is ready
*/
export const useSendToRenderer = (
message: null | Extract<CommunicationMessages, MessagePayload<EditorToRendererMessageType>>,
rendererReady: boolean
): void => {
const iframeCommunicator = useEditorToRendererCommunicator()
useEffect(() => {
if (message && rendererReady) {
iframeCommunicator.sendMessageToOtherSide(message)
}
}, [iframeCommunicator, message, rendererReady])
}