Skip to content

Commit c5d5d3d

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 ad16c08 commit c5d5d3d

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { BlockNoteView } from '@blocknote/mantine';
1212
import '@blocknote/mantine/style.css';
1313
import { useCreateBlockNote } from '@blocknote/react';
1414
import { HocuspocusProvider } from '@hocuspocus/provider';
15-
import { useEffect } from 'react';
15+
import { useEffect, useRef } from 'react';
1616
import { useTranslation } from 'react-i18next';
1717
import * as Y from 'yjs';
1818

@@ -79,6 +79,7 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
7979
const { setEditor } = useEditorStore();
8080
const { t } = useTranslation();
8181
const { isSynced: isConnectedToCollabServer } = useProviderStore();
82+
const refEditorContainer = useRef<HTMLDivElement>(null);
8283

8384
useSaveDoc(doc.id, provider.document, isConnectedToCollabServer);
8485
const { i18n } = useTranslation();
@@ -154,7 +155,9 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
154155
);
155156

156157
useHeadings(editor);
157-
useShortcuts(editor);
158+
159+
useShortcuts(editor, refEditorContainer.current);
160+
158161
useUploadStatus(editor);
159162

160163
useEffect(() => {
@@ -166,7 +169,7 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
166169
}, [setEditor, editor]);
167170

168171
return (
169-
<>
172+
<Box ref={refEditorContainer}>
170173
{errorAttachment && (
171174
<Box $margin={{ bottom: 'big', top: 'none', horizontal: 'large' }}>
172175
<TextErrors
@@ -186,7 +189,7 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
186189
<BlockNoteSuggestionMenu />
187190
<BlockNoteToolbar />
188191
</BlockNoteView>
189-
</>
192+
</Box>
190193
);
191194
};
192195

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 & 5 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+
el: HTMLDivElement | null,
8+
) => {
69
useEffect(() => {
710
const handleKeyDown = (event: KeyboardEvent) => {
811
if (event.key === '@' && editor?.isFocused()) {
@@ -29,11 +32,14 @@ export const useShortcuts = (editor: DocsBlockNoteEditor) => {
2932
}
3033
};
3134

32-
// Attach the event listener to the document instead of the window
33-
document.addEventListener('keydown', handleKeyDown);
35+
if (!el) {
36+
return;
37+
}
38+
39+
el.addEventListener('keydown', handleKeyDown);
3440

3541
return () => {
36-
document.removeEventListener('keydown', handleKeyDown);
42+
el.removeEventListener('keydown', handleKeyDown);
3743
};
38-
}, [editor]);
44+
}, [editor, el]);
3945
};

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)