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