From c50052ac04247eee5db02488291e54a88337118b Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Mon, 14 Aug 2023 10:33:48 -0700 Subject: [PATCH] Chore: Desktop: Fix NoteEditor unnecessary rerendering (#8662) --- packages/app-desktop/gui/NoteEditor/NoteEditor.tsx | 14 +++++++++----- packages/app-desktop/gui/NoteEditor/utils/types.ts | 3 +-- packages/lib/services/share/reducer.ts | 4 ---- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx b/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx index b3a41efb37..3feeffafa9 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx +++ b/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { useState, useEffect, useCallback, useRef } from 'react'; +import { useState, useEffect, useCallback, useRef, useMemo } from 'react'; import TinyMCE from './NoteBody/TinyMCE/TinyMCE'; import CodeMirror from './NoteBody/CodeMirror/CodeMirror'; import { connect } from 'react-redux'; @@ -40,7 +40,7 @@ import Note from '@joplin/lib/models/Note'; import Folder from '@joplin/lib/models/Folder'; const bridge = require('@electron/remote').require('./bridge').default; import NoteRevisionViewer from '../NoteRevisionViewer'; -import { readFromSettings } from '@joplin/lib/services/share/reducer'; +import { parseShareCache } from '@joplin/lib/services/share/reducer'; import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect'; import { ModelType } from '@joplin/lib/BaseModel'; import BaseItem from '@joplin/lib/models/BaseItem'; @@ -286,11 +286,15 @@ function NoteEditor(props: NoteEditorProps) { // } // }, [props.dispatch]); + const shareCache = useMemo(() => { + return parseShareCache(props.shareCacheSetting); + }, [props.shareCacheSetting]); + useAsyncEffect(async event => { if (!formNote.id) return; try { - const result = await itemIsReadOnly(BaseItem, ModelType.Note, ItemChange.SOURCE_UNSPECIFIED, formNote.id, props.syncUserId, props.shareCache); + const result = await itemIsReadOnly(BaseItem, ModelType.Note, ItemChange.SOURCE_UNSPECIFIED, formNote.id, props.syncUserId, shareCache); if (event.cancelled) return; setIsReadOnly(result); } catch (error) { @@ -301,7 +305,7 @@ function NoteEditor(props: NoteEditorProps) { throw error; } } - }, [formNote.id, props.syncUserId, props.shareCache]); + }, [formNote.id, props.syncUserId, shareCache]); const onBodyWillChange = useCallback((event: any) => { handleProvisionalFlag(); @@ -656,7 +660,7 @@ const mapStateToProps = (state: AppState) => { isSafeMode: state.settings.isSafeMode, useCustomPdfViewer: false, syncUserId: state.settings['sync.userId'], - shareCache: readFromSettings(state), + shareCacheSetting: state.settings['sync.shareCache'], }; }; diff --git a/packages/app-desktop/gui/NoteEditor/utils/types.ts b/packages/app-desktop/gui/NoteEditor/utils/types.ts index 8d0c938d7a..be4f833133 100644 --- a/packages/app-desktop/gui/NoteEditor/utils/types.ts +++ b/packages/app-desktop/gui/NoteEditor/utils/types.ts @@ -2,7 +2,6 @@ import AsyncActionQueue from '@joplin/lib/AsyncActionQueue'; import { ToolbarButtonInfo } from '@joplin/lib/services/commands/ToolbarButtonUtils'; import { PluginStates } from '@joplin/lib/services/plugins/reducer'; -import { State as ShareState } from '@joplin/lib/services/share/reducer'; import { MarkupLanguage } from '@joplin/renderer'; import { RenderResult, RenderResultPluginAsset } from '@joplin/renderer/MarkupToHtml'; import { MarkupToHtmlOptions } from './useMarkupToHtml'; @@ -46,7 +45,7 @@ export interface NoteEditorProps { contentMaxWidth: number; isSafeMode: boolean; useCustomPdfViewer: boolean; - shareCache: ShareState; + shareCacheSetting: string; syncUserId: string; } diff --git a/packages/lib/services/share/reducer.ts b/packages/lib/services/share/reducer.ts index a6b1ec56c2..b131895d85 100644 --- a/packages/lib/services/share/reducer.ts +++ b/packages/lib/services/share/reducer.ts @@ -82,10 +82,6 @@ export const parseShareCache = (serialized: string): State => { }; }; -export const readFromSettings = (state: RootState): State => { - return parseShareCache(state.settings['sync.shareCache']); -}; - export function isSharedFolderOwner(state: RootState, folderId: string): boolean { const userId = state.settings['sync.userId']; const share = state[stateRootKey].shares.find(s => s.folder_id === folderId);