️(frontend) improve heading store

Reset headings only when the headers are not
equal to the previous ones. It will prevent
unnecessary rerenders.
This commit is contained in:
Anthony LC
2025-03-17 16:57:25 +01:00
committed by Anthony LC
parent 7e7c9ac4c5
commit 910686293c

View File

@@ -1,3 +1,4 @@
import _ from 'lodash';
import { create } from 'zustand'; import { create } from 'zustand';
import { DocsBlockNoteEditor, HeadingBlock } from '../types'; import { DocsBlockNoteEditor, HeadingBlock } from '../types';
@@ -24,7 +25,7 @@ export interface UseHeadingStore {
resetHeadings: () => void; resetHeadings: () => void;
} }
export const useHeadingStore = create<UseHeadingStore>((set) => ({ export const useHeadingStore = create<UseHeadingStore>((set, get) => ({
headings: [], headings: [],
setHeadings: (editor) => { setHeadings: (editor) => {
const headingBlocks = editor?.document const headingBlocks = editor?.document
@@ -36,7 +37,9 @@ export const useHeadingStore = create<UseHeadingStore>((set) => ({
), ),
})) as unknown as HeadingBlock[]; })) as unknown as HeadingBlock[];
set(() => ({ headings: headingBlocks })); if (!_.isEqual(get().headings, headingBlocks)) {
set(() => ({ headings: headingBlocks }));
}
}, },
resetHeadings: () => set(() => ({ headings: [] })), resetHeadings: () => set(() => ({ headings: [] })),
})); }));