hedgedoc/frontend/src/components/render-page/window-post-message-communicator/hooks/use-send-to-renderer.ts
Tilman Vatteroth 762a0a850e
fix: Move content into to frontend directory
Doing this BEFORE the merge prevents a lot of merge conflicts.

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2022-11-20 19:48:40 +01:00

29 lines
1 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useEffect } from 'react'
import type { CommunicationMessages, EditorToRendererMessageType } from '../rendering-message'
import { useEditorToRendererCommunicator } from '../../../editor-page/render-context/editor-to-renderer-communicator-context-provider'
import type { MessagePayload } from '../window-post-message-communicator'
/**
* 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: undefined | Extract<CommunicationMessages, MessagePayload<EditorToRendererMessageType>>,
rendererReady: boolean
): void => {
const iframeCommunicator = useEditorToRendererCommunicator()
useEffect(() => {
if (message && rendererReady) {
iframeCommunicator.sendMessageToOtherSide(message)
}
}, [iframeCommunicator, message, rendererReady])
}