Skip to content

Commit 2b67aa7

Browse files
committed
⚡️(frontend) improve unsubscribe logic when unmounting components
We can now unsubscribe on the editor events, improving performance and preventing memory leaks.
1 parent d5c3f24 commit 2b67aa7

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/frontend/apps/impress/src/features/docs/doc-editor/components/BlockNoteEditor.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
169169
);
170170

171171
useHeadings(editor);
172-
useShortcuts(editor);
172+
173+
const className = '--docs--editor-container';
174+
useShortcuts(editor, className);
175+
173176
useUploadStatus(editor);
174177

175178
useEffect(() => {
@@ -185,7 +188,7 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
185188
$padding={{ top: 'md' }}
186189
$background="white"
187190
$css={cssEditor(readOnly, isDeletedDoc)}
188-
className="--docs--editor-container"
191+
className={className}
189192
>
190193
{errorAttachment && (
191194
<Box $margin={{ bottom: 'big', top: 'none', horizontal: 'large' }}>

src/frontend/apps/impress/src/features/docs/doc-editor/hook/useHeadings.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ export const useHeadings = (editor: DocsBlockNoteEditor) => {
99
useEffect(() => {
1010
setHeadings(editor);
1111

12-
editor?.onChange(() => {
12+
const unsubscribe = editor?.onChange(() => {
1313
setHeadings(editor);
1414
});
1515

1616
return () => {
1717
resetHeadings();
18+
unsubscribe();
1819
};
1920
}, [editor, resetHeadings, setHeadings]);
2021
};

src/frontend/apps/impress/src/features/docs/doc-editor/hook/useShortcuts.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { useEffect } from 'react';
22

33
import { DocsBlockNoteEditor } from '../types';
44

5-
export const useShortcuts = (editor: DocsBlockNoteEditor) => {
5+
export const useShortcuts = (
6+
editor: DocsBlockNoteEditor,
7+
elClassname: string,
8+
) => {
69
useEffect(() => {
710
const handleKeyDown = (event: KeyboardEvent) => {
811
if (event.key === '@' && editor?.isFocused()) {
@@ -30,10 +33,14 @@ export const useShortcuts = (editor: DocsBlockNoteEditor) => {
3033
};
3134

3235
// Attach the event listener to the document instead of the window
33-
document.addEventListener('keydown', handleKeyDown);
36+
const el = document.querySelector<HTMLElement>(`.${elClassname}`);
37+
if (!el) {
38+
return;
39+
}
40+
el.addEventListener('keydown', handleKeyDown);
3441

3542
return () => {
36-
document.removeEventListener('keydown', handleKeyDown);
43+
el.removeEventListener('keydown', handleKeyDown);
3744
};
38-
}, [editor]);
45+
}, [editor, elClassname]);
3946
};

src/frontend/apps/impress/src/features/docs/doc-editor/hook/useUploadFile.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const useUploadStatus = (editor: DocsBlockNoteEditor) => {
4040
const { t } = useTranslation();
4141

4242
useEffect(() => {
43-
editor.onChange((_, context) => {
43+
const unsubscribe = editor.onChange((_, context) => {
4444
const blocksChanges = context.getChanges();
4545

4646
if (!blocksChanges.length) {
@@ -90,7 +90,12 @@ export const useUploadStatus = (editor: DocsBlockNoteEditor) => {
9090

9191
return () => {
9292
clearTimeout(timeoutId);
93+
unsubscribe();
9394
};
9495
});
96+
97+
return () => {
98+
unsubscribe();
99+
};
95100
}, [editor, t]);
96101
};

0 commit comments

Comments
 (0)