Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: improve startup loading shortcuts

kashaf-ansari-dev97749729adbe
diff --git a/apps/mobile/app/app.tsx b/apps/mobile/app/app.tsx
index bee37dbe5..40d17fe49 100644
--- a/apps/mobile/app/app.tsx
+++ b/apps/mobile/app/app.tsx
@@ -23,7 +23,7 @@ import {
THEME_COMPATIBILITY_VERSION,
useThemeEngineStore
} from "@notesnook/theme";
-import React, { PropsWithChildren, useEffect } from "react";
+import React, { PropsWithChildren, useEffect, useState } from "react";
import { Appearance, I18nManager, Linking, StatusBar } from "react-native";
import "react-native-gesture-handler";
import { GestureHandlerRootView } from "react-native-gesture-handler";
@@ -59,23 +59,6 @@ if (appLockEnabled || appLockMode !== "none") {
useUserStore.getState().lockApp(true);
}
-Shortcuts.getInitialShortcut().then((shortcut) => {
- if (shortcut?.type === "notesnook.action.newnote") {
- launchNewNoteTab();
- }
-
- useSettingStore.setState({
- pendingShortcut: shortcut ?? null,
- pendingShortcutLoaded: true
- });
- RNBootSplash.hide({ fade: true });
- initShortcutListener();
-});
-
-Linking.getInitialURL().then((url) => {
- useSettingStore.setState({ initialUrl: url });
-});
-
const App = (props: { configureMode: "note-preview" }) => {
useAppEvents();
//@ts-ignore
@@ -203,4 +186,38 @@ export const withTheme = (
};
};
-export default withTheme(withErrorBoundry(App, "App"));
+export const withStartupBoundry = (
+ Element: (props: PropsWithChildren) => JSX.Element
+) => {
+ return function AppWithStartupBoundary(props: PropsWithChildren) {
+ const [ready, setReady] = useState(false);
+
+ useEffect(() => {
+ async function init() {
+ const [url, shortcut] = await Promise.all([
+ Linking.getInitialURL(),
+ Shortcuts.getInitialShortcut()
+ ]);
+ if (shortcut?.type === "notesnook.action.newnote") {
+ launchNewNoteTab();
+ }
+ useSettingStore.setState({
+ initialUrl: url,
+ pendingShortcut: shortcut ?? null
+ });
+
+ initShortcutListener();
+ await RNBootSplash.hide({ fade: true });
+ setReady(true);
+ }
+
+ init();
+ }, []);
+
+ if (!ready) return null;
+
+ return <Element {...props} />;
+ };
+};
+
+export default withStartupBoundry(withTheme(withErrorBoundry(App, "App")));
diff --git a/apps/mobile/app/navigation/fluid-panels-view.tsx b/apps/mobile/app/navigation/fluid-panels-view.tsx
index 1292c6452..e2650aef9 100644
--- a/apps/mobile/app/navigation/fluid-panels-view.tsx
+++ b/apps/mobile/app/navigation/fluid-panels-view.tsx
@@ -101,6 +101,15 @@ export const FluidPanelsView = React.memo(
setOrientation(o);
}
});
+ React.useEffect(() => {
+ const shortcut = useSettingStore.getState().pendingShortcut;
+
+ if (shortcut?.type === "notesnook.action.newnote") {
+ useSettingStore.setState({
+ pendingShortcut: null
+ });
+ }
+ }, []);
useEffect(() => {
if (!appLoading) {
diff --git a/apps/mobile/app/navigation/navigation-stack.tsx b/apps/mobile/app/navigation/navigation-stack.tsx
index ab1ebf957..486b4150c 100644
--- a/apps/mobile/app/navigation/navigation-stack.tsx
+++ b/apps/mobile/app/navigation/navigation-stack.tsx
@@ -307,14 +307,15 @@ export const RootNavigation = () => {
const introCompleted = useSettingStore(
(state) => state.settings.introCompleted
);
- const pendingShortcut = useSettingStore((state) => state.pendingShortcut);
- const pendingShortcutLoaded = useSettingStore(
- (state) => state.pendingShortcutLoaded
- );
+
+ const initialShortcut = React.useRef(
+ useSettingStore.getState().pendingShortcut
+ ).current;
+
const reminderFeature = useIsFeatureAvailable("activeReminders");
const clearSelection = useSelectionStore((state) => state.clearSelection);
const resetTimer = React.useRef<NodeJS.Timeout>(undefined);
- const isNavigationLoaded = React.useRef(true);
+
const onStateChange = React.useCallback(
(state: any) => {
if (useSelectionStore.getState().selectionMode) {
@@ -330,59 +331,55 @@ export const RootNavigation = () => {
);
React.useEffect(() => {
- if (pendingShortcut?.type === "notesnook.action.newreminder") {
- useSettingStore.setState({ pendingShortcut: null });
- }
- }, []);
+ const unsubscribe = useSettingStore.subscribe((state, prevState) => {
+ const pendingShortcut = state.pendingShortcut;
- React.useEffect(() => {
- if (isNavigationLoaded.current) {
- isNavigationLoaded.current = false;
- return;
- }
- if (!pendingShortcut) return;
-
- if (pendingShortcut.type === "notesnook.action.newreminder") {
- if (reminderFeature === undefined) return;
-
- if (!reminderFeature.isAllowed) {
- presentDialog({
- title: strings.upgrade(),
- paragraph: reminderFeature.error,
- positiveText: strings.upgrade(),
- negativeText: strings.cancel(),
- positivePress: async () => {
- PaywallSheet.present(reminderFeature);
- }
- });
- useSettingStore.setState({ pendingShortcut: null });
+ if (pendingShortcut === prevState.pendingShortcut || !pendingShortcut) {
return;
}
- rootNavigatorRef.current?.navigate("AddReminder" as any);
- useSettingStore.setState({ pendingShortcut: null });
- } else if (pendingShortcut.type === "notesnook.action.newnote") {
- if (fluidTabsRef.current) {
- rootNavigatorRef.current?.navigate("FluidPanelsView" as any);
- eSendEvent(eOnLoadNote, { newNote: true });
- editorState().movedAway = false;
- fluidTabsRef.current.goToPage("editor", true);
- } else {
- launchNewNoteTab();
-
- rootNavigatorRef.current?.navigate("FluidPanelsView" as any, {
- initialPage: "editor"
- });
- }
- useSettingStore.setState({ pendingShortcut: null });
- }
- }, [pendingShortcut]);
+ if (pendingShortcut.type === "notesnook.action.newreminder") {
+ if (reminderFeature === undefined) return;
+
+ if (!reminderFeature.isAllowed) {
+ presentDialog({
+ title: strings.upgrade(),
+ paragraph: reminderFeature.error,
+ positiveText: strings.upgrade(),
+ negativeText: strings.cancel(),
+ positivePress: async () => {
+ PaywallSheet.present(reminderFeature);
+ }
+ });
+ useSettingStore.setState({
+ pendingShortcut: null
+ });
+ return;
+ }
+
+ rootNavigatorRef.current?.navigate("AddReminder" as any);
+ } else if (pendingShortcut.type === "notesnook.action.newnote") {
+ if (fluidTabsRef.current) {
+ rootNavigatorRef.current?.navigate("FluidPanelsView" as any);
+ eSendEvent(eOnLoadNote, { newNote: true });
+ editorState().movedAway = false;
+ fluidTabsRef.current.goToPage("editor", true);
+ } else {
+ launchNewNoteTab();
+
+ rootNavigatorRef.current?.navigate("FluidPanelsView" as any, {
+ initialPage: "editor"
+ });
+ }
+ }
+ });
- if (!pendingShortcutLoaded) return null;
+ return unsubscribe;
+ }, [reminderFeature]);
const initialRouteName = !introCompleted
? "Welcome"
- : pendingShortcut?.type === "notesnook.action.newreminder"
+ : initialShortcut?.type === "notesnook.action.newreminder"
? "AddReminder"
: "FluidPanelsView";
@@ -419,7 +416,7 @@ export const RootNavigation = () => {
}}
initialParams={{
initialPage:
- pendingShortcut?.type === "notesnook.action.newnote"
+ initialShortcut?.type === "notesnook.action.newnote"
? "editor"
: undefined
}}
diff --git a/apps/mobile/app/screens/add-reminder/index.tsx b/apps/mobile/app/screens/add-reminder/index.tsx
index 30c29995b..e6cc813fa 100644
--- a/apps/mobile/app/screens/add-reminder/index.tsx
+++ b/apps/mobile/app/screens/add-reminder/index.tsx
@@ -173,6 +173,14 @@ export default function AddReminder(props: NavigationProps<"AddReminder">) {
);
const [dateError, setDateError] = useState<string>();
const [selectDayError, setSelectDayError] = useState<string>();
+ React.useEffect(() => {
+ const shortcut = useSettingStore.getState().pendingShortcut;
+ if (shortcut?.type === "notesnook.action.newreminder") {
+ useSettingStore.setState({
+ pendingShortcut: null
+ });
+ }
+ }, []);
useEffect(() => {
if (activeReminderFeature === undefined) return;
if (!activeReminderFeature.isAllowed) {
diff --git a/apps/mobile/app/stores/use-setting-store.ts b/apps/mobile/app/stores/use-setting-store.ts
index 1726e7bb9..ea5aa5c28 100644
--- a/apps/mobile/app/stores/use-setting-store.ts
+++ b/apps/mobile/app/stores/use-setting-store.ts
@@ -151,7 +151,6 @@ export interface SettingStore {
inboxEnabled: boolean;
setInboxEnabled: (inboxEnabled: boolean) => void;
pendingShortcut: ShortcutItem | null;
- pendingShortcutLoaded: boolean;
}
const { width, height } = Dimensions.get("window");
@@ -273,6 +272,5 @@ export const useSettingStore = create<SettingStore>((set, get) => ({
},
inboxEnabled: false,
setInboxEnabled: (inboxEnabled) => set({ inboxEnabled }),
- pendingShortcut: null,
- pendingShortcutLoaded: false
+ pendingShortcut: null
}));