Skip to content

Commit

Permalink
♻️(frontend) create useHeadings hook
Browse files Browse the repository at this point in the history
- We create the useHeadings hook to manage the
headings of the document and staty DRY.
- We use the headings store in IconOpenPanelEditor
and TableContent, to avoid prop drilling.
- We add a debounce on the onEditorContentChange
to improve a bit the performance.
  • Loading branch information
AntoLC committed Dec 6, 2024
1 parent 69186e9 commit a9def8c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { useAuthStore } from '@/core/auth';
import { Doc } from '@/features/docs/doc-management';

import { useUploadFile } from '../hook';
import { useHeadings } from '../hook/useHeadings';
import useSaveDoc from '../hook/useSaveDoc';
import { useEditorStore, useHeadingStore } from '../stores';
import { useEditorStore } from '../stores';
import { randomColor } from '../utils';

import { BlockNoteToolbar } from './BlockNoteToolbar';
Expand Down Expand Up @@ -75,7 +76,6 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {

const readOnly = !doc.abilities.partial_update;
useSaveDoc(doc.id, provider.document, !readOnly);
const { setHeadings, resetHeadings } = useHeadingStore();
const { i18n } = useTranslation();
const lang = i18n.language;

Expand Down Expand Up @@ -126,6 +126,7 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
},
[collabName, lang, provider, uploadFile],
);
useHeadings(editor);

useEffect(() => {
setEditor(editor);
Expand All @@ -135,18 +136,6 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
};
}, [setEditor, editor]);

useEffect(() => {
setHeadings(editor);

editor?.onEditorContentChange(() => {
setHeadings(editor);
});

return () => {
resetHeadings();
};
}, [editor, resetHeadings, setHeadings]);

return (
<Box $css={cssEditor(readOnly)}>
{errorAttachment && (
Expand Down Expand Up @@ -179,8 +168,7 @@ export const BlockNoteEditorVersion = ({
initialContent,
}: BlockNoteEditorVersionProps) => {
const readOnly = true;
const { setHeadings, resetHeadings } = useHeadingStore();

const { setEditor } = useEditorStore();
const editor = useCreateBlockNote(
{
collaboration: {
Expand All @@ -194,18 +182,15 @@ export const BlockNoteEditorVersion = ({
},
[initialContent],
);
useHeadings(editor);

useEffect(() => {
setHeadings(editor);

editor?.onEditorContentChange(() => {
setHeadings(editor);
});
setEditor(editor);

return () => {
resetHeadings();
setEditor(undefined);
};
}, [editor, resetHeadings, setHeadings]);
}, [setEditor, editor]);

return (
<Box $css={cssEditor(readOnly)}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
import { Versions, useDocVersion } from '@/features/docs/doc-versioning/';
import { useResponsiveStore } from '@/stores';

import { useHeadingStore } from '../stores';

import { BlockNoteEditor, BlockNoteEditorVersion } from './BlockNoteEditor';
import { IconOpenPanelEditor, PanelEditor } from './PanelEditor';

Expand All @@ -29,7 +27,6 @@ export const DocEditor = ({ doc }: DocEditorProps) => {
query: { versionId },
} = useRouter();
const { t } = useTranslation();
const { headings } = useHeadingStore();
const { isMobile } = useResponsiveStore();

const isVersion = versionId && typeof versionId === 'string';
Expand Down Expand Up @@ -79,9 +76,9 @@ export const DocEditor = ({ doc }: DocEditorProps) => {
) : (
<BlockNoteEditor doc={doc} provider={provider} />
)}
{!isMobile && <IconOpenPanelEditor headings={headings} />}
{!isMobile && <IconOpenPanelEditor />}
</Card>
<PanelEditor doc={doc} headings={headings} />
<PanelEditor doc={doc} />
</Box>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ import { TableContent } from '@/features/docs/doc-table-content';
import { VersionList } from '@/features/docs/doc-versioning';
import { useResponsiveStore } from '@/stores';

import { usePanelEditorStore } from '../stores';
import { HeadingBlock } from '../types';
import { useHeadingStore, usePanelEditorStore } from '../stores';

interface PanelProps {
doc: Doc;
headings: HeadingBlock[];
}

export const PanelEditor = ({
doc,
headings,
}: PropsWithChildren<PanelProps>) => {
export const PanelEditor = ({ doc }: PropsWithChildren<PanelProps>) => {
const { t } = useTranslation();
const { colorsTokens } = useCunninghamTheme();
const { isMobile } = useResponsiveStore();
Expand Down Expand Up @@ -63,7 +58,7 @@ export const PanelEditor = ({
`}
$maxHeight="99vh"
>
{isMobile && <IconOpenPanelEditor headings={headings} />}
{isMobile && <IconOpenPanelEditor />}
<Box
$direction="row"
$justify="space-between"
Expand Down Expand Up @@ -127,7 +122,7 @@ export const PanelEditor = ({
</BoxButton>
)}
</Box>
{isPanelTableContentOpen && <TableContent headings={headings} />}
{isPanelTableContentOpen && <TableContent />}
{!isPanelTableContentOpen && doc.abilities.versions_list && (
<VersionList doc={doc} />
)}
Expand All @@ -136,11 +131,8 @@ export const PanelEditor = ({
);
};

interface IconOpenPanelEditorProps {
headings: HeadingBlock[];
}

export const IconOpenPanelEditor = ({ headings }: IconOpenPanelEditorProps) => {
export const IconOpenPanelEditor = () => {
const { headings } = useHeadingStore();
const { t } = useTranslation();
const { setIsPanelOpen, isPanelOpen, setIsPanelTableContentOpen } =
usePanelEditorStore();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BlockNoteEditor } from '@blocknote/core';
import { useEffect } from 'react';

import { useHeadingStore } from '../stores';

export const useHeadings = (editor: BlockNoteEditor) => {
const { setHeadings, resetHeadings } = useHeadingStore();

useEffect(() => {
setHeadings(editor);

let timeout: NodeJS.Timeout;
editor?.onEditorContentChange(() => {
clearTimeout(timeout);
timeout = setTimeout(() => setHeadings(editor), 200);
});

return () => {
clearTimeout(timeout);
resetHeadings();
};
}, [editor, resetHeadings, setHeadings]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { Box, BoxButton, Text } from '@/components';
import { HeadingBlock, useEditorStore } from '@/features/docs/doc-editor';
import { useEditorStore, useHeadingStore } from '@/features/docs/doc-editor';
import { useResponsiveStore } from '@/stores';

import { Heading } from './Heading';

interface TableContentProps {
headings: HeadingBlock[];
}

export const TableContent = ({ headings }: TableContentProps) => {
export const TableContent = () => {
const { headings } = useHeadingStore();
const { editor } = useEditorStore();
const { isMobile } = useResponsiveStore();
const { t } = useTranslation();
Expand Down

0 comments on commit a9def8c

Please sign in to comment.