Skip to content

Commit

Permalink
mobile: fix editor loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed committed May 10, 2024
1 parent 511acc5 commit 0e8ea3d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
11 changes: 10 additions & 1 deletion apps/mobile/app/hooks/use-app-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import { updateStatusBarColor } from "../utils/colors";
import { BETA } from "../utils/constants";
import {
eCloseSheet,
eEditorReset,
eLoginSessionExpired,
eOnLoadNote,
eOpenAnnouncementDialog,
Expand Down Expand Up @@ -235,7 +236,7 @@ async function checkForShareExtensionLaunchedInBackground() {
if (notesAddedFromIntent || shareExtensionOpened) {
const id = useTabStore.getState().getCurrentNoteId();
const note = id && (await db.notes.note(id));
eSendEvent("webview_reset");
eSendEvent(eEditorReset);
if (note) setTimeout(() => eSendEvent("loadingNote", note), 1);
MMKV.removeItem("shareExtensionOpened");
}
Expand Down Expand Up @@ -532,6 +533,14 @@ export const useAppEvents = () => {
}
//@ts-ignore
globalThis["IS_SHARE_EXTENSION"] = false;

if (
SettingsService.getBackgroundEnterTime() + 60 * 1000 * 30 <
Date.now()
) {
// Reset the editor if the app has been in background for more than 10 minutes.
eSendEvent(eEditorReset);
}
} else {
await saveEditorState();
if (
Expand Down
14 changes: 6 additions & 8 deletions apps/mobile/app/screens/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
eSubscribeEvent
} from "../../services/event-manager";
import {
eEditorReset,
eOnLoadNote,
eUnlockNote,
eUnlockWithBiometrics,
Expand All @@ -54,7 +55,8 @@ import { syncTabs, useTabStore } from "./tiptap/use-tab-store";
import {
editorController,
editorState,
openInternalLink
openInternalLink,
randId
} from "./tiptap/utils";
import { tabBarRef } from "../../utils/global-refs";

Expand Down Expand Up @@ -103,24 +105,20 @@ const Editor = React.memo(
noToolbar,
noHeader
});
const renderKey = useRef(`editor-0` + editorId);
const renderKey = useRef(randId("editor-id") + editorId);
useImperativeHandle(ref, () => ({
get: () => editor
}));
useLockedNoteHandler();

const onError = useCallback(() => {
renderKey.current =
renderKey.current === `editor-0`
? `editor-1` + editorId
: `editor-0` + editorId;

renderKey.current = randId("editor-id") + editorId;
editor.state.current.ready = false;
editor.setLoading(true);
}, [editor, editorId]);

useEffect(() => {
const sub = [eSubscribeEvent("webview_reset", onError)];
const sub = [eSubscribeEvent(eEditorReset, onError)];
return () => {
sub.forEach((s) => s?.unsubscribe());
};
Expand Down
6 changes: 4 additions & 2 deletions apps/mobile/app/screens/editor/tiptap/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import SettingsService from "../../../services/settings";
import { useSettingStore } from "../../../stores/use-setting-store";
import { useTagStore } from "../../../stores/use-tag-store";
import {
eEditorReset,
eEditorTabFocused,
eOnLoadNote,
eShowMergeDialog,
Expand Down Expand Up @@ -464,6 +465,7 @@ export const useEditor = (
overlay(false);
return;
}
console.log("LOADING NOTE", event.item.id);
const item = event.item;

const currentTab = useTabStore
Expand Down Expand Up @@ -549,6 +551,7 @@ export const useEditor = (
loadingState.current === currentContents.current[item.id]?.data
) {
// If note is already loading, return.
console.log("Note is already loading...");
return;
}

Expand Down Expand Up @@ -886,7 +889,7 @@ export const useEditor = (
useTabStore.getState().currentTab
))
) {
eSendEvent("webview_reset", "onReady");
eSendEvent(eEditorReset, "onReady");
return false;
} else {
syncTabs();
Expand Down Expand Up @@ -917,7 +920,6 @@ export const useEditor = (
} else if (state.current?.initialLoadCalled) {
const note = currentNotes.current[noteId];
if (note) {
console.log("Loading note in onLoad...");
loadNote({
item: note
});
Expand Down
9 changes: 5 additions & 4 deletions apps/mobile/app/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ function canLockAppInBackground() {
}
let backgroundEnterTime = 0;
function appEnteredBackground() {
if (canLockAppInBackground()) {
backgroundEnterTime = Date.now();
}
backgroundEnterTime = Date.now();
}

const getBackgroundEnterTime = () => backgroundEnterTime;

function shouldLockAppOnEnterForeground() {
if (
useUserStore.getState().disableAppLockRequests ||
Expand Down Expand Up @@ -227,7 +227,8 @@ export const SettingsService = {
shouldLockAppOnEnterForeground,
canLockAppInBackground,
appEnteredBackground,
setPrivacyScreen
setPrivacyScreen,
getBackgroundEnterTime
};

init();
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/app/utils/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,4 @@ export const eUnlockWithPassword = "619";
export const eUpdateNoteInEditor = "620";
export const eOnEnterEditor = "621";
export const eOnExitEditor = "622";
export const eEditorReset = "623";
20 changes: 10 additions & 10 deletions packages/editor-mobile/src/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ const Tiptap = ({
pendingSaveRequests
.getPendingContentIds()
.then(async (result) => {
logger("info", result.length, "PENDING ITEMS");

if (result && result.length) {
dbLogger("log", "Pending save requests found... restoring");
await pendingSaveRequests.postPendingRequests();
Expand All @@ -315,6 +313,16 @@ const Tiptap = ({
const noteState = tabRef.current.noteId
? state.noteState[tabRef.current.noteId]
: undefined;

post(
EventTypes.tabFocused,
!!globalThis.editorControllers[tabRef.current.id]?.content.current &&
!editorControllers[tabRef.current.id]?.loading,
tabRef.current.id,
state.getCurrentNoteId()
);
editorControllers[tabRef.current.id]?.updateTab();

if (noteState) {
if (
containerRef.current &&
Expand All @@ -339,14 +347,6 @@ const Tiptap = ({
) {
editorControllers[tabRef.current.id]?.setLoading(true);
}
post(
EventTypes.tabFocused,
!!globalThis.editorControllers[tabRef.current.id]?.content.current &&
!editorControllers[tabRef.current.id]?.loading,
tabRef.current.id,
state.getCurrentNoteId()
);
editorControllers[tabRef.current.id]?.updateTab();
} else {
isFocusedRef.current = false;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/editor-mobile/src/hooks/useTabStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ export const useTabStore = create(
currentTab: 0,
scrollPosition: {},
setNoteState: (noteId: string, state: Partial<NoteState>) => {
if (editorControllers[get().currentTab]?.loading) return;

const noteState = {
...get().noteState
};
noteState[noteId] = {
...get().noteState[noteId],
...state
};

set({
noteState
});
Expand Down

0 comments on commit 0e8ea3d

Please sign in to comment.