A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
perf: speed up first launch even more
thecodrr7a2c0933e187
diff --git a/apps/web/src/app-effects.js b/apps/web/src/app-effects.jsnew file mode 100644index 000000000..adc58c1f9--- /dev/null+++ b/apps/web/src/app-effects.js@@ -0,0 +1,103 @@+import React, { useEffect } from "react";+import { useStore } from "./stores/app-store";+import { useStore as useUserStore } from "./stores/user-store";+import { useStore as useNotesStore } from "./stores/note-store";+import useVersion, { getCachedVersion } from "./utils/useVersion";+import { resetReminders } from "./common/reminders";+import { isUserPremium } from "./common";+import { db } from "./common/db";+import { CHECK_IDS, EV, EVENTS } from "notes-core/common";++function AppEffects({ isMobile, isTablet, setShow }) {+ var refreshColors = useStore((store) => store.refreshColors);+ var refreshMenuPins = useStore((store) => store.refreshMenuPins);+ var isFocusMode = useStore((store) => store.isFocusMode);+ var isEditorOpen = useStore((store) => store.isEditorOpen);+ var toggleSideMenu = useStore((store) => store.toggleSideMenu);+ var addReminder = useStore((store) => store.addReminder);+ var initUser = useUserStore((store) => store.init);+ var initNotes = useNotesStore((store) => store.init);+ var setIsVaultCreated = useStore((store) => store.setIsVaultCreated);+ var version = useVersion();++ useEffect(() => {+ (async function () {+ const cached = getCachedVersion();+ if (!cached) return;+ await import("./common/dialogcontroller").then((dialogs) => {+ if (cached.appUpdated) return dialogs.showAppUpdatedNotice(cached);+ else if (cached.appUpdateable)+ return dialogs.showAppAvailableNotice(cached);+ });+ })();+ }, [version]);++ useEffect(+ function initializeApp() {+ refreshColors();+ refreshMenuPins();+ initUser();+ initNotes();+ (async function () {+ await resetReminders();+ setIsVaultCreated(await db.vault.exists());+ })();+ },+ [+ refreshColors,+ refreshMenuPins,+ initUser,+ initNotes,+ addReminder,+ setIsVaultCreated,+ ]+ );++ useEffect(() => {+ EV.subscribe(EVENTS.userCheckStatus, async (type) => {+ if (process.env.REACT_APP_CI) return { type, result: true };+ if (isUserPremium()) {+ return { type, result: true };+ } else {+ if (type !== CHECK_IDS.databaseSync)+ await import("./common/dialogcontroller").then((dialogs) =>+ dialogs.showBuyDialog(type)+ );+ return { type, result: false };+ }+ });+ }, []);++ useEffect(() => {+ if (isFocusMode) {+ setShow(false);+ } else {+ if (!isTablet) setShow(true);+ }+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [isFocusMode]);++ useEffect(() => {+ if (!isMobile && !isTablet) return;+ setShow(!isEditorOpen);+ //setIsEditorOpen(!show);+ // if (isTablet) toggleSideMenu(!isEditorOpen);+ // if (!isEditorOpen && !isTablet && !isMobile) toggleSideMenu(true);+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [isEditorOpen, isMobile, isTablet]);++ useEffect(() => {+ return () => {+ EV.unsubscribeAll();+ };+ }, []);++ useEffect(() => {+ toggleSideMenu(!isMobile);+ if (!isMobile && !isTablet) setShow(true);+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [isMobile, isTablet, toggleSideMenu]);++ return <React.Fragment />;+}+export default AppEffects;diff --git a/apps/web/src/app.js b/apps/web/src/app.jsindex 582d3231e..7773f5999 100644--- a/apps/web/src/app.js+++ b/apps/web/src/app.js@@ -1,131 +1,55 @@-import React, { useEffect, useState } from "react";-import ReactDOM from "react-dom";+import React, { useState, useEffect } from "react";import "./app.css";import { Flex } from "rebass";+import { MotionConfig, AnimationFeature, GesturesFeature } from "framer-motion";import ThemeProvider from "./components/theme-provider";-import { useStore } from "./stores/app-store";-import { useStore as useUserStore } from "./stores/user-store";-import { useStore as useNotesStore } from "./stores/note-store";-import { useStore as useEditorStore } from "./stores/editor-store";-import { useStore as useThemeStore } from "./stores/theme-store";+import Banner from "./components/banner";+import StatusBar from "./components/statusbar";import Animated from "./components/animated";import NavigationMenu from "./components/navigationmenu";-import routes from "./navigation/routes";-import Editor from "./components/editor";-import useMobile from "./utils/use-mobile";import GlobalMenuWrapper from "./components/globalmenuwrapper";-import { resetReminders } from "./common/reminders";-import { isUserPremium } from "./common";-import { db } from "./common/db";-import { CHECK_IDS, EV, EVENTS } from "notes-core/common";-import useTablet from "./utils/use-tablet";-import { showBuyDialog } from "./common/dialog-controller";-import Banner from "./components/banner";-import StatusBar from "./components/statusbar";-import useRoutes from "./utils/use-routes";-import useHashRoutes from "./utils/use-hash-routes";-import hashroutes from "./navigation/hash-routes";-import rootroutes from "./navigation/rootroutes";import { getCurrentPath, NavigationEvents } from "./navigation";-import useVersion, { getCachedVersion } from "./utils/useVersion";-import {- showAppAvailableNotice,- showAppUpdatedNotice,-} from "./common/dialog-controller";-import { useAnimation } from "framer-motion";-import RouteContainer from "./components/route-container";-import { MotionConfig, AnimationFeature, GesturesFeature } from "framer-motion";+import rootroutes from "./navigation/rootroutes";+import { useStore as useEditorStore } from "./stores/editor-store";+import { Suspense } from "react";+import useMobile from "./utils/use-mobile";+import useTablet from "./utils/use-tablet";+import HashRouter from "./components/hashrouter";++const ThemeTransition = React.lazy(() =>+ import("./components/themeprovider/themetransition")+);+const AppEffects = React.lazy(() => import("./app-effects"));+const CachedRouter = React.lazy(() => import("./components/cached-router"));function App() {const [show, setShow] = useState(true);- const refreshColors = useStore((store) => store.refreshColors);- const refreshMenuPins = useStore((store) => store.refreshMenuPins);- const isFocusMode = useStore((store) => store.isFocusMode);- const addReminder = useStore((store) => store.addReminder);- const initUser = useUserStore((store) => store.init);- const initNotes = useNotesStore((store) => store.init);- const isEditorOpen = useStore((store) => store.isEditorOpen);- const clearSession = useEditorStore((store) => store.clearSession);- const toggleSideMenu = useStore((store) => store.toggleSideMenu);- const setIsVaultCreated = useStore((store) => store.setIsVaultCreated);- const version = useVersion();const isMobile = useMobile();const isTablet = useTablet();+ const [isAppLoaded, setIsAppLoaded] = useState(false);+ const clearSession = useEditorStore((store) => store.clearSession);useEffect(() => {- (async function () {- const cached = getCachedVersion();- if (!cached) return;- if (cached.appUpdated) await showAppUpdatedNotice(cached);- else if (cached.appUpdateable) await showAppAvailableNotice(cached);- })();- }, [version]);-- useEffect(- function initializeApp() {- refreshColors();- refreshMenuPins();- initUser();- initNotes();- (async function () {- await resetReminders();- setIsVaultCreated(await db.vault.exists());- })();- },- [- refreshColors,- refreshMenuPins,- initUser,- initNotes,- addReminder,- setIsVaultCreated,- ]- );-- useEffect(() => {- EV.subscribe(EVENTS.userCheckStatus, async (type) => {- if (process.env.REACT_APP_CI) return { type, result: true };- if (isUserPremium()) {- return { type, result: true };- } else {- if (type !== CHECK_IDS.databaseSync) await showBuyDialog(type);- return { type, result: false };- }- });- }, []);-- useEffect(() => {- if (isFocusMode) {- setShow(false);- } else {- if (!isTablet) setShow(true);+ function onNavigate() {+ NavigationEvents.unsubscribe("onNavigate", onNavigate);+ setIsAppLoaded(true);}- // eslint-disable-next-line react-hooks/exhaustive-deps- }, [isFocusMode]);-- useEffect(() => {- if (!isMobile && !isTablet) return;- setShow(!isEditorOpen);- //setIsEditorOpen(!show);- // if (isTablet) toggleSideMenu(!isEditorOpen);- // if (!isEditorOpen && !isTablet && !isMobile) toggleSideMenu(true);- // // eslint-disable-next-line react-hooks/exhaustive-deps- }, [isEditorOpen, isMobile, isTablet]);-- useEffect(() => {- return () => {- EV.unsubscribeAll();- };+ NavigationEvents.subscribe("onNavigate", onNavigate);}, []);- useEffect(() => {- toggleSideMenu(!isMobile);- if (!isMobile && !isTablet) setShow(true);- }, [isMobile, isTablet, toggleSideMenu]);-return (<MotionConfig features={[AnimationFeature, GesturesFeature]}><ThemeProvider>+ {isAppLoaded && (+ <Suspense fallback={<div style={{ display: "none" }} />}>+ <AppEffects+ setShow={setShow}+ isMobile={isMobile}+ isTablet={isTablet}+ />+ </Suspense>+ )}+<FlexflexDirection="column"id="app"@@ -136,8 +60,9 @@ function App() {<Flex flex={1} sx={{ overflow: "hidden" }}><NavigationMenutoggleNavigationContainer={(state) => {- if (isMobile || isTablet) clearSession();- else setShow(state || !show);+ if (isMobile || isTablet) {+ clearSession();+ } else setShow(state || !show);}}/><Flex variant="rowFill">@@ -158,74 +83,30 @@ function App() {}}>{isMobile && <Banner />}- <CachedRouter />+ <Suspense fallback={<div />}>+ <CachedRouter />+ </Suspense></Animated.Flex><Flexwidth={[show ? 0 : "100%", show ? 0 : "100%", "100%"]}flexDirection="column">- <EditorSwitch />+ <HashRouter />+ {/* <EditorSwitch /> */}</Flex></Flex><GlobalMenuWrapper /></Flex><StatusBar />- <ThemeTransition />+ <Suspense fallback={<div />}>+ <ThemeTransition />+ </Suspense></Flex></ThemeProvider></MotionConfig>);}-const cache = {};-function CachedRouter() {- const RouteResult = useRoutes(routes, { fallbackRoute: "/" });- useEffect(() => {- NavigationEvents.publish("onNavigate", RouteResult);-- const key = RouteResult.key || "general";- const routeContainer = document.getElementById("mainRouteContainer");- routeContainer.childNodes.forEach((node) => {- node.style.display = "none";- });-- var route = document.getElementById(key);- if (route) {- route.style.display = "flex";- if (key !== "general") return;- }-- if (!cache[key] || !route) {- if (!route) {- cache[key] = true;- route = document.createElement("div");- route.id = key;- route.className = "route";- routeContainer.appendChild(route);- }- ReactDOM.render(- <ThemeProvider>{RouteResult.component}</ThemeProvider>,- route- );- }- }, [RouteResult]);-- return (- <RouteContainer- id="mainRouteContainer"- type={RouteResult?.type}- title={RouteResult?.title}- subtitle={RouteResult?.subtitle}- buttons={RouteResult?.buttons}- />- );-}--function EditorSwitch() {- const routeResult = useHashRoutes(hashroutes);- return routeResult || <Editor />;-}-function Root() {const path = getCurrentPath();switch (path) {@@ -238,49 +119,4 @@ function Root() {}}-function ThemeTransition() {- const theme = useThemeStore((store) => store.theme);- const animate = useAnimation();- useEffect(() => {- if (!ThemeTransition.first) {- ThemeTransition.first = true;- return;- }- const element = document.getElementById("themeTransition");- element.style.visibility = "visible";- animate- .start({- opacity: 1,- transition: { duration: 0 },- })- .then(() => {- animate- .start({- opacity: 0,- transition: { duration: 1.5 },- })- .then(() => {- element.style.visibility = "collapse";- });- });- }, [theme, animate]);- return (- <Animated.Box- id="themeTransition"- sx={{- position: "absolute",- width: "100%",- height: "100%",- top: 0,- left: 0,- zIndex: 999,- opacity: 1,- visibility: "collapse",- }}- animate={animate}- bg="bgSecondary"- />- );-}-export default Root;diff --git a/apps/web/src/components/cached-router/index.js b/apps/web/src/components/cached-router/index.jsnew file mode 100644index 000000000..bcc6e3c1d--- /dev/null+++ b/apps/web/src/components/cached-router/index.js@@ -0,0 +1,52 @@+import React, { useEffect } from "react";+import ReactDOM from "react-dom";+import { NavigationEvents } from "../../navigation";+import useRoutes from "../../utils/use-routes";+import RouteContainer from "../route-container";+import ThemeProvider from "../theme-provider";+import routes from "../../navigation/routes";++const cache = {};+function CachedRouter() {+ const RouteResult = useRoutes(routes, { fallbackRoute: "/" });+ useEffect(() => {+ NavigationEvents.publish("onNavigate", RouteResult);++ const key = RouteResult.key || "general";+ const routeContainer = document.getElementById("mainRouteContainer");+ routeContainer.childNodes.forEach((node) => {+ node.style.display = "none";+ });++ var route = document.getElementById(key);+ if (route) {+ route.style.display = "flex";+ if (key !== "general") return;+ }++ if (!cache[key] || !route) {+ if (!route) {+ cache[key] = true;+ route = document.createElement("div");+ route.id = key;+ route.className = "route";+ routeContainer.appendChild(route);+ }+ ReactDOM.render(+ <ThemeProvider>{RouteResult.component}</ThemeProvider>,+ route+ );+ }+ }, [RouteResult]);++ return (+ <RouteContainer+ id="mainRouteContainer"+ type={RouteResult?.type}+ title={RouteResult?.title}+ subtitle={RouteResult?.subtitle}+ buttons={RouteResult?.buttons}+ />+ );+}+export default CachedRouter;diff --git a/apps/web/src/components/editor/-placeholder.js b/apps/web/src/components/editor/-placeholder.jsnew file mode 100644index 000000000..b8cfdcd96--- /dev/null+++ b/apps/web/src/components/editor/-placeholder.js@@ -0,0 +1,33 @@+import React from "react";+import { Button, Flex, Image, Text } from "rebass";+import * as Icon from "../icons";++function EditorPlaceholder() {+ return (+ <Flex+ flexDirection="column"+ flex={1}+ justifyContent="center"+ alignItems="center"+ >+ <Image src="./logo.svg" width={150} />+ <Text variant="body" textAlign="center" mt={2}>+ Please create or open a note to start editing.+ </Text>+ <Button+ mt={2}+ display="flex"+ sx={{+ alignItems: "center",+ justifyContent: "center",+ }}+ variant="tool"+ onClick={() => {}}+ >+ <Icon.Plus size={18} color="primary" />+ <Text ml={1}>Make a new note</Text>+ </Button>+ </Flex>+ );+}+export default EditorPlaceholder;diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.jsindex dc4b5c971..b89868734 100644--- a/apps/web/src/components/editor/index.js+++ b/apps/web/src/components/editor/index.js@@ -1,4 +1,10 @@-import React, { useEffect, useMemo, useRef, Suspense } from "react";+import React, {+ useEffect,+ useMemo,+ useRef,+ Suspense,+ useCallback,+} from "react";import { Flex } from "rebass";import Properties from "../properties";import {@@ -17,13 +23,15 @@ import EditorLoading from "./loading";const ReactMCE = React.lazy(() => import("./tinymce"));-function Editor(props) {+function Editor({ noteId }) {const editorRef = useRef();const sessionState = useStore((store) => store.session.state);const sessionId = useStore((store) => store.session.id);const contentType = useStore((store) => store.session.content?.type);const setSession = useStore((store) => store.setSession);const saveSession = useStore((store) => store.saveSession);+ const newSession = useStore((store) => store.newSession);+ const openSession = useStore((store) => store.openSession);const toggleProperties = useStore((store) => store.toggleProperties);const updateWordCount = useStore((store) => store.updateWordCount);const init = useStore((store) => store.init);@@ -39,6 +47,23 @@ function Editor(props) {init();}, [init]);+ const startSession = useCallback(+ async function startSession(noteId) {+ if (noteId === 0) newSession();+ else if (noteId) {+ await openSession(noteId);+ }+ },+ [newSession, openSession]+ );++ useEffect(() => {+ if (!editorRef.current?.editor) return;+ (async () => {+ await startSession(noteId);+ })();+ }, [startSession, noteId]);+useEffect(() => {if (sessionState === SESSION_STATES.new) {editorstore.set((state) => (state.session.state = SESSION_STATES.stale));@@ -145,6 +170,9 @@ function Editor(props) {}}changeInterval={500}onWordCountChanged={updateWordCount}+ onInit={async () => {+ await startSession(noteId);+ }}/>) : null}</Suspense>diff --git a/apps/web/src/components/editor/loading.js b/apps/web/src/components/editor/loading.jsindex 9b2694572..ea7cd1608 100644--- a/apps/web/src/components/editor/loading.js+++ b/apps/web/src/components/editor/loading.js@@ -2,7 +2,7 @@ import React from "react";import { Flex, Text } from "rebass";import * as Icon from "../icons";-function EditorLoading() {+function EditorLoading({ text }) {return (<FlexflexDirection="column"@@ -12,7 +12,7 @@ function EditorLoading() {><Icon.Loading color="primary" sx={{ mt: 2 }} /><Text variant="body" textAlign="center" mt={2}>- Loading editor. Please wait...+ {text || "Loading editor. Please wait..."}</Text></Flex>);diff --git a/apps/web/src/components/editor/tinymce.js b/apps/web/src/components/editor/tinymce.jsindex 9246ee6b4..db76dde32 100644--- a/apps/web/src/components/editor/tinymce.js+++ b/apps/web/src/components/editor/tinymce.js@@ -112,6 +112,7 @@ function TinyMCE(props) {initialValue,onFocus,editorRef,+ onInit,} = props;const [oldSkin, newSkin] = useSkin();const tinymceRef = editorRef;@@ -135,6 +136,7 @@ function TinyMCE(props) {<Editorref={tinymceRef}onFocus={onFocus}+ onInit={onInit}initialValue={initialValue}init={{menubar: false,diff --git a/apps/web/src/components/editor/toolbar.js b/apps/web/src/components/editor/toolbar.jsindex ead117902..c216aeceb 100644--- a/apps/web/src/components/editor/toolbar.js+++ b/apps/web/src/components/editor/toolbar.js@@ -157,6 +157,7 @@ function Toolbar(props) {data-test-id={tool.title.toLowerCase().replace(/ /g, "-")}disabled={!tool.enabled}variant="tool"+ ml={2}title={tool.title}key={tool.title}sx={{diff --git a/apps/web/src/components/hash-router/index.js b/apps/web/src/components/hash-router/index.jsnew file mode 100644index 000000000..7fb3ca586--- /dev/null+++ b/apps/web/src/components/hash-router/index.js@@ -0,0 +1,10 @@+import React from "react";+import useHashRoutes from "../../utils/use-hash-routes";+import hashroutes from "../../navigation/hash-routes";+import EditorPlaceholder from "../editor/-placeholder";++function HashRouter() {+ const routeResult = useHashRoutes(hashroutes);+ return routeResult || <EditorPlaceholder />;+}+export default HashRouter;diff --git a/apps/web/src/components/theme-provider/themetransition.js b/apps/web/src/components/theme-provider/themetransition.jsnew file mode 100644index 000000000..46b9bd585--- /dev/null+++ b/apps/web/src/components/theme-provider/themetransition.js@@ -0,0 +1,50 @@+import React, { useEffect } from "react";+import { useStore as useThemeStore } from "../../stores/theme-store";+import Animated from "../animated";+import { useAnimation } from "framer-motion";++function ThemeTransition() {+ const theme = useThemeStore((store) => store.theme);+ const animate = useAnimation();+ useEffect(() => {+ if (!ThemeTransition.first) {+ ThemeTransition.first = true;+ return;+ }+ const element = document.getElementById("themeTransition");+ element.style.visibility = "visible";+ animate+ .start({+ opacity: 1,+ transition: { duration: 0 },+ })+ .then(() => {+ animate+ .start({+ opacity: 0,+ transition: { duration: 1.5 },+ })+ .then(() => {+ element.style.visibility = "collapse";+ });+ });+ }, [theme, animate]);+ return (+ <Animated.Box+ id="themeTransition"+ sx={{+ position: "absolute",+ width: "100%",+ height: "100%",+ top: 0,+ left: 0,+ zIndex: 999,+ opacity: 1,+ visibility: "collapse",+ }}+ animate={animate}+ bg="bgSecondary"+ />+ );+}+export default ThemeTransition;diff --git a/apps/web/src/navigation/hash-routes.js b/apps/web/src/navigation/hash-routes.jsindex f2478a9c0..8e6ba1da1 100644--- a/apps/web/src/navigation/hash-routes.js+++ b/apps/web/src/navigation/hash-routes.js@@ -13,13 +13,15 @@ import SplitEditor from "../components/spliteditor";import Unlock from "../components/unlock";import { store as appStore } from "../stores/app-store";import { store as editorStore } from "../stores/editor-store";-import { store as noteStore } from "../stores/note-store";import { isMobile, isTablet } from "../utils/dimensions";import {showEditTopicDialog,showTopicDialog,} from "../common/dialog-controller";import { hashNavigate } from ".";+import { Suspense } from "react";+import EditorLoading from "../components/editor/loading";+const Editor = React.lazy(() => import("../components/editor"));const hashroutes = {"/": () => {@@ -42,10 +44,20 @@ const hashroutes = {showEditTopicDialog(notebookId, topicId);},"/notes/create": () => {- editorStore.get().newSession(noteStore.get().context);+ //editorStore.get().newSession(noteStore.get().context);+ return (+ <Suspense fallback={<EditorLoading />}>+ <Editor noteId={0} />+ </Suspense>+ );},"/notes/:noteId/edit": ({ noteId }) => {- editorStore.openSession(noteId);+ // editorStore.openSession(noteId);+ return (+ <Suspense fallback={<EditorLoading text="Opening note..." />}>+ <Editor noteId={noteId} />+ </Suspense>+ );},"/notes/:noteId/unlock": ({ noteId }) => {return (diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.jsindex 38106e674..acf2c001d 100644--- a/apps/web/src/stores/editor-store.js+++ b/apps/web/src/stores/editor-store.js@@ -130,7 +130,10 @@ class EditorStore extends BaseStore {});};- newSession = (context = {}) => {+ newSession = (context) => {+ if (!context) {+ context = noteStore.get().context;+ }this.set((state) => {state.session = {...DEFAULT_SESSION,diff --git a/apps/web/src/theme/variants/button.js b/apps/web/src/theme/variants/button.jsindex 85ae9005b..5d973c5c8 100644--- a/apps/web/src/theme/variants/button.js+++ b/apps/web/src/theme/variants/button.js@@ -118,9 +118,10 @@ class Tool {color: "text",p: 1,px: 2,+ backgroundColor: "bgSecondary",borderRadius: "default",":hover": {- backgroundColor: "bgSecondary",+ backgroundColor: "#dbdbdb",},};}