mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 14:44:43 -04:00
40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import { useSendToRenderer } from '../../../render-page/window-post-message-communicator/hooks/use-send-to-renderer'
|
|
import { useMemo, useRef } from 'react'
|
|
import { CommunicationMessageType } from '../../../render-page/window-post-message-communicator/rendering-message'
|
|
import { useApplicationState } from '../../../../hooks/common/use-application-state'
|
|
import equal from 'fast-deep-equal'
|
|
import type { RendererFrontmatterInfo } from '../../../../redux/note-details/types/note-details'
|
|
|
|
/**
|
|
* Extracts the {@link RendererFrontmatterInfo frontmatter data}
|
|
* from the global application state and sends it to the renderer.
|
|
*/
|
|
export const useSendFrontmatterInfoFromReduxToRenderer = (): void => {
|
|
const frontmatterInfo = useApplicationState((state) => state.noteDetails.frontmatterRendererInfo)
|
|
const lastFrontmatter = useRef<RendererFrontmatterInfo | undefined>(undefined)
|
|
|
|
const cachedFrontmatterInfo = useMemo(() => {
|
|
if (lastFrontmatter.current !== undefined && equal(lastFrontmatter.current, frontmatterInfo)) {
|
|
return lastFrontmatter.current
|
|
} else {
|
|
lastFrontmatter.current = frontmatterInfo
|
|
return frontmatterInfo
|
|
}
|
|
}, [frontmatterInfo])
|
|
|
|
return useSendToRenderer(
|
|
useMemo(
|
|
() => ({
|
|
type: CommunicationMessageType.SET_FRONTMATTER_INFO,
|
|
frontmatterInfo: cachedFrontmatterInfo
|
|
}),
|
|
[cachedFrontmatterInfo]
|
|
)
|
|
)
|
|
}
|