hedgedoc/src/components/markdown-renderer/hooks/use-post-toc-ast-on-change.ts
Philip Molares f3bf7cd105
Added reuse information (#782)
Signed-off-by: Philip Molares <philip.molares@udo.edu>
2020-11-22 21:50:07 +01:00

19 lines
676 B
TypeScript

/*
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import equal from 'fast-deep-equal'
import { RefObject, useEffect, useRef } from 'react'
import { TocAst } from '../../../external-types/markdown-it-toc-done-right/interface'
export const usePostTocAstOnChange = (tocAst: RefObject<TocAst|undefined>, onTocChange?: (ast: TocAst) => void): void => {
const lastTocAst = useRef<TocAst>()
useEffect(() => {
if (onTocChange && tocAst.current && !equal(tocAst, lastTocAst.current)) {
lastTocAst.current = tocAst.current
onTocChange(tocAst.current)
}
}, [onTocChange, tocAst])
}