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
feat: improve navigation (#165)
Abdullah Atta5c4529b26135
diff --git a/apps/web/src/app.js b/apps/web/src/app.jsindex 95a58a21c..540286bd6 100644--- a/apps/web/src/app.js+++ b/apps/web/src/app.js@@ -25,13 +25,14 @@ 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 } from "./navigation";+import { getCurrentPath, NavigationEvents } from "./navigation";import useVersion, { getCachedVersion } from "./utils/useVersion";import {showAppAvailableNotice,showAppUpdatedNotice,} from "./components/dialogs/confirm";import { useAnimation } from "framer-motion";+import RouteContainer from "./components/route-container";function App() {const [show, setShow] = useState(true);@@ -176,8 +177,10 @@ const cache = {};function CachedRouter() {const RouteResult = useRoutes(routes, { fallbackRoute: "/" });useEffect(() => {+ NavigationEvents.publish("onNavigate", RouteResult);+const key = RouteResult.key || "general";- const routeContainer = document.getElementById("routeContainer");+ const routeContainer = document.getElementById("mainRouteContainer");routeContainer.childNodes.forEach((node) => {node.style.display = "none";});@@ -196,10 +199,22 @@ function CachedRouter() {route.className = "route";routeContainer.appendChild(route);}- ReactDOM.render(<ThemeProvider>{RouteResult}</ThemeProvider>, route);+ ReactDOM.render(+ <ThemeProvider>{RouteResult.component}</ThemeProvider>,+ route+ );}}, [RouteResult]);- return <div style={{ display: "flex", flex: 1 }} id="routeContainer" />;++ return (+ <RouteContainer+ id="mainRouteContainer"+ type={RouteResult.type}+ title={RouteResult.title}+ subtitle={RouteResult.subtitle}+ buttons={RouteResult.buttons}+ />+ );}function EditorSwitch() {diff --git a/apps/web/src/common/selectionoptions.js b/apps/web/src/common/selectionoptions.jsindex 60fddd9aa..2ce38e8ea 100644--- a/apps/web/src/common/selectionoptions.js+++ b/apps/web/src/common/selectionoptions.js@@ -59,7 +59,7 @@ const DeleteOption = createOption(nbStore.refresh();} else if (item.type === "topic") {await db.notebooks.notebook(item.notebookId).topics.delete(...items);- nbStore.setSelectedNotebookTopics(item.notebookId);+ nbStore.setSelectedNotebook(item.notebookId);}showToast("success", `${items.length} ${item.type}s moved to trash!`);}diff --git a/apps/web/src/components/dialogs/move-note-dialog.js b/apps/web/src/components/dialogs/move-note-dialog.jsindex 4297a12cd..e4157a066 100644--- a/apps/web/src/components/dialogs/move-note-dialog.js+++ b/apps/web/src/components/dialogs/move-note-dialog.js@@ -6,6 +6,7 @@ import Dialog, { showDialog } from "./dialog";import { showNotesMovedToast } from "../../common/toasts";import { showToast } from "../../utils/toast";import Field from "../field";+import { store as notestore } from "../../stores/note-store";class MoveDialog extends React.Component {_inputRef;@@ -16,6 +17,11 @@ class MoveDialog extends React.Component {notebooks: db.notebooks.all,};+ refresh() {+ this.setState({ notebooks: db.notebooks.all });+ notestore.refresh();+ }+async addNotebook(input) {if (input.value.length > 0) {await db.notebooks.add({@@ -39,13 +45,13 @@ class MoveDialog extends React.Component {}async _addNoteToTopic(notebook, topic) {- const { noteIds, onMove, onClose } = this.props;+ const { noteIds } = this.props;if (this._topicHasNotes(topic, noteIds)) {await db.notebooks.notebook(topic.notebookId).topics.topic(topic.id).delete(...noteIds);- this.setState({ notebooks: db.notebooks.all });+ this.refresh();return;}try {@@ -56,12 +62,10 @@ class MoveDialog extends React.Component {const note = db.notes.note(noteIds[0]).data;await db.notes.move(nb, ...noteIds);showNotesMovedToast(note, noteIds, nb);- onMove();+ this.refresh();} catch (e) {showToast("error", e.message);console.error(e);- } finally {- onClose();}}@@ -76,7 +80,7 @@ class MoveDialog extends React.Component {icon={Icon.Move}onClose={props.onClose}negativeButton={{- text: "Cancel",+ text: "Done",onClick: props.onClose,}}>diff --git a/apps/web/src/components/dialogs/topicdialog.js b/apps/web/src/components/dialogs/topicdialog.jsindex 6719c1144..31ebaf7ee 100644--- a/apps/web/src/components/dialogs/topicdialog.js+++ b/apps/web/src/components/dialogs/topicdialog.js@@ -50,7 +50,7 @@ export function showTopicDialog() {if (!topic) return;const notebookId = store.get().selectedNotebookId;await db.notebooks.notebook(notebookId).topics.add(topic);- store.setSelectedNotebookTopics(notebookId);+ store.setSelectedNotebook(notebookId);perform(true);}}/>@@ -72,7 +72,7 @@ export function showEditTopicDialog(notebookId, topicId) {await db.notebooks.notebook(topic.notebookId).topics.add({ ...topic, title: t });- store.setSelectedNotebookTopics(topic.notebookId);+ store.setSelectedNotebook(topic.notebookId);showToast("success", "Topic edited successfully!");perform(true);}}diff --git a/apps/web/src/components/icons/index.js b/apps/web/src/components/icons/index.jsindex f109ca3a1..4cdc1ac87 100644--- a/apps/web/src/components/icons/index.js+++ b/apps/web/src/components/icons/index.js@@ -5,13 +5,11 @@ import { useTheme } from "emotion-theming";import Animated from "../animated";//import useMobile from "../../utils/use-mobile";-function Icon({ name, size = 24, color = "icon", stroke, rotate }) {+function Icon({ title, name, size = 24, color = "icon", stroke, rotate }) {const theme = useTheme();- // const isMobile = useMobile();- //size += isMobile ? 4 : 0;-return (<MDIIcon+ title={title}path={name}size={size + "px"}stroke={stroke}@@ -37,7 +35,7 @@ function createIcon(name, rotate = false) {justifyContent="center"alignItems="center">- <Icon name={name} rotate={rotate} {...props} />+ <Icon title={props.title} name={name} rotate={rotate} {...props} /></Animated.Flex>);};diff --git a/apps/web/src/components/navigation-menu/index.js b/apps/web/src/components/navigation-menu/index.jsindex 9cb8f7471..dd5128175 100644--- a/apps/web/src/components/navigation-menu/index.js+++ b/apps/web/src/components/navigation-menu/index.js@@ -16,11 +16,11 @@ import { useLocation } from "wouter";function shouldSelectNavItem(route, pin) {if (pin.type === "notebook") {- return route === `/notebooks/${pin.id}`;+ return route.startsWith(`/notebooks/${pin.id}`);} else if (pin.type === "topic") {- return route === `/notebooks/${pin.notebookId}/${pin.id}`;+ return route.startsWith(`/notebooks/${pin.notebookId}/${pin.id}`);} else if (pin.type === "tag") {- return route === `/tags/${pin.id}`;+ return route.startsWith(`/tags/${pin.id}`);}}@@ -36,8 +36,8 @@ const routes = [path: "/favorites",icon: Icon.StarOutline,},- { title: "Trash", path: "/trash", icon: Icon.Trash },{ title: "Tags", path: "/tags", icon: Icon.Tag },+ { title: "Trash", path: "/trash", icon: Icon.Trash },];const bottomRoutes = [diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.jsindex b7bb0f9b2..eaff3633f 100644--- a/apps/web/src/components/note/index.js+++ b/apps/web/src/components/note/index.js@@ -26,9 +26,7 @@ function menuItems(note, context) {{title: "Add to notebook",onClick: async () => {- if (await showMoveNoteDialog([note.id])) {- store.refresh();- }+ await showMoveNoteDialog([note.id]);},},{@@ -67,6 +65,7 @@ function menuItems(note, context) {visible: context?.type === "topic",title: "Remove from topic",onClick: async () => {+ console.log("Remove from topic:", Object.isExtensible(note));await db.notebooks.notebook(context.value.id).topics.topic(context.value.topic)diff --git a/apps/web/src/components/route-container/index.js b/apps/web/src/components/route-container/index.jsindex 30cc8e0fa..45c54785d 100644--- a/apps/web/src/components/route-container/index.js+++ b/apps/web/src/components/route-container/index.js@@ -8,26 +8,11 @@ import useMobile from "../../utils/use-mobile";import { navigate } from "../../navigation";function RouteContainer(props) {- const {- type,- route,- canGoBack,- title,- subtitle,- onlyBackButton,- noSearch,- } = props;+ const { id, type, title, subtitle, buttons } = props;return (<>- <Header- type={type}- canGoBack={canGoBack}- title={title}- subtitle={subtitle}- noSearch={noSearch}- onlyBackButton={onlyBackButton}- />- {route}+ <Header type={type} title={title} subtitle={subtitle} buttons={buttons} />+ <Flex id={id} flexDirection="column" flex={1} /></>);}@@ -35,21 +20,22 @@ function RouteContainer(props) {export default RouteContainer;function Header(props) {- const { title, subtitle, canGoBack, onlyBackButton, type, noSearch } = props;+ const { title, subtitle, buttons, type } = props;const createButtonData = CREATE_BUTTON_MAP[type];const toggleSideMenu = useStore((store) => store.toggleSideMenu);const isMobile = useMobile();const isSelectionMode = useSelectionStore((store) => store.isSelectionMode);- if (!onlyBackButton && !title && !subtitle) return null;+ if (!title && !subtitle) return null;return (<Flex mx={2} flexDirection="column" justifyContent="center"><Flex alignItems="center" justifyContent="space-between"><Flex justifyContent="center" alignItems="center" py={2}>- {canGoBack || onlyBackButton ? (+ {buttons?.back ? (<Icon.ArrowLeftsize={24}- onClick={() => window.history.back()}+ title={buttons.back.title}+ onClick={buttons.back.action}sx={{ flexShrink: 0, mr: 2 }}color="text"data-test-id="go-back"@@ -62,7 +48,7 @@ function Header(props) {ml: 0,mr: 4,mt: 1,- display: [onlyBackButton ? "none" : "block", "none", "none"],+ display: ["block", "none", "none"],}}size={30}/>@@ -74,12 +60,11 @@ function Header(props) {<SelectionOptions options={SELECTION_OPTIONS_MAP[type]} />{!isSelectionMode && (<Flex>- {!noSearch && (+ {buttons?.search && (<Icon.Searchsize={24}- onClick={() => {- navigate(`/search`, { type });- }}+ title={buttons.search.title}+ onClick={() => navigate(`/search/${type}`)}/>)}diff --git a/apps/web/src/components/scroll-container/index.js b/apps/web/src/components/scroll-container/index.jsindex 08106dfe4..0b04af949 100644--- a/apps/web/src/components/scroll-container/index.js+++ b/apps/web/src/components/scroll-container/index.js@@ -1,4 +1,4 @@-import React from "react";+import React, { useRef } from "react";import RSC from "react-scrollbars-custom";import { Text } from "rebass";@@ -9,26 +9,28 @@ const ScrollContainer = ({style,className,}) => {+ const ref = useRef();return (<RSCclassName={className}style={style}+ ref={ref}trackClickBehavior="step"onMouseEnter={() => {- const scrollTrackY = document.querySelector(".scrollTrackY");+ const scrollTrackY = ref.current?.trackYElement;if (scrollTrackY) scrollTrackY.style.opacity = 1;}}onTouchStart={() => {- const scrollTrackY = document.querySelector(".scrollTrackY");+ const scrollTrackY = ref.current?.trackYElement;if (scrollTrackY) scrollTrackY.style.opacity = 1;}}onTouchEnd={() => {- const scrollTrackY = document.querySelector(".scrollTrackY");+ const scrollTrackY = ref.current?.trackYElement;if (scrollTrackY) scrollTrackY.style.opacity = 0;}}onMouseLeave={(e) => {if (e.nativeEvent.buttons > 0) return;- const scrollTrackY = document.querySelector(".scrollTrackY");+ const scrollTrackY = ref.current?.trackYElement;if (scrollTrackY) scrollTrackY.style.opacity = 0;}}wrapperProps={{diff --git a/apps/web/src/components/topic/index.js b/apps/web/src/components/topic/index.jsindex 577e5ac8c..7c07c11d5 100644--- a/apps/web/src/components/topic/index.js+++ b/apps/web/src/components/topic/index.js@@ -24,7 +24,7 @@ const menuItems = (item) => [color: "red",onClick: async () => {await db.notebooks.notebook(item.notebookId).topics.delete(item.id);- store.setSelectedNotebookTopics(item.notebookId);+ store.setSelectedNotebook(item.notebookId);},},];diff --git a/apps/web/src/navigation/hash-routes.js b/apps/web/src/navigation/hash-routes.jsindex dc0966d99..25de5d8b7 100644--- a/apps/web/src/navigation/hash-routes.js+++ b/apps/web/src/navigation/hash-routes.js@@ -19,6 +19,7 @@ import {showEditTopicDialog,showTopicDialog,} from "../components/dialogs/topicdialog";+import { hashNavigate } from ".";const hashroutes = {"/": () => {@@ -49,8 +50,15 @@ const hashroutes = {"/notes/:noteId/unlock": ({ noteId }) => {return (<RouteContainer- onlyBackButton={isMobile()}- route={<Unlock noteId={noteId} />}+ buttons={{+ back: isMobile()+ ? {+ title: "Go back",+ action: () => hashNavigate("/notes/create"),+ }+ : undefined,+ }}+ component={<Unlock noteId={noteId} />}/>);},diff --git a/apps/web/src/navigation/index.js b/apps/web/src/navigation/index.jsindex eb11520bb..01ea78f43 100644--- a/apps/web/src/navigation/index.js+++ b/apps/web/src/navigation/index.js@@ -1,4 +1,5 @@import { useState } from "react";+import EventManager from "notes-core/utils/eventmanager";// import { useCallback } from "react";export function navigate(url, replaceOrQuery, replace) {@@ -92,3 +93,5 @@ export function getCurrentPath() {export function getCurrentHash() {return window.location.hash;}++export const NavigationEvents = new EventManager();diff --git a/apps/web/src/navigation/routes.js b/apps/web/src/navigation/routes.jsindex b447121fe..db5d5ca00 100644--- a/apps/web/src/navigation/routes.js+++ b/apps/web/src/navigation/routes.js@@ -1,7 +1,5 @@import React from "react";import { db } from "../common";-import FavoritesPlaceholder from "../components/placeholders/favorites-placeholder";-import RouteContainer from "../components/route-container";import { toTitleCase } from "../utils/string";import Home from "../views/home";import Notebooks from "../views/notebooks";@@ -12,117 +10,169 @@ import Tags from "../views/tags";import Topics from "../views/topics";import { navigate } from "../navigation";import Trash from "../views/trash";+import { store as notestore } from "../stores/note-store";+import { store as nbstore } from "../stores/notebook-store";const routes = {- "/": () => (- <RouteContainer key="home" type="notes" title="Notes" route={<Home />} />- ),- "/notebooks": () => (- <RouteContainer- key="notebooks"- type="notebooks"- title="Notebooks"- route={<Notebooks />}- />- ),- "/notebooks/:notebook": ({ notebook }) => {- const nbItem = db.notebooks.notebook(notebook);- if (!nbItem) return;- return (- <RouteContainer- type="topics"- title={nbItem.title}- canGoBack- route={<Topics notebookId={notebook} />}- />- );+ "/": () => ({+ key: "home",+ type: "notes",+ title: "Notes",+ component: <Home />,+ buttons: {+ search: {+ title: "Search notes",+ },+ },+ }),+ "/notebooks": () => ({+ key: "notebooks",+ type: "notebooks",+ title: "Notebooks",+ component: <Notebooks />,+ buttons: {+ search: {+ title: "Search notebooks",+ },+ },+ }),+ "/notebooks/:notebookId": ({ notebookId }) => {+ const notebook = db.notebooks.notebook(notebookId);+ if (!notebook) return false;+ nbstore.setSelectedNotebook(notebookId);+ return {+ key: "topics",+ type: "topics",+ title: notebook.title,+ component: <Topics />,+ buttons: {+ back: {+ title: "Go back to notebooks",+ action: () => navigate("/notebooks"),+ },+ search: {+ title: `Search ${notebook.title} topics`,+ },+ },+ };},- "/notebooks/:notebook/:topic": ({ notebook, topic }) => {- const nb = db.notebooks.notebook(notebook);- const topicItem = nb?.topics?.topic(topic)?._topic;- if (!topicItem) return navigate(`/notebooks/${nb.id}`);-- return (- <RouteContainer- type="notes"- title={nb.title}- canGoBack- subtitle={topicItem.title}- route={- <Notes- context={{- type: "topic",- value: { id: notebook, topic: topic },- }}- />- }- />- );+ "/notebooks/:notebookId/:topicId": ({ notebookId, topicId }) => {+ const notebook = db.notebooks.notebook(notebookId);+ const topic = notebook?.topics?.topic(topicId)?._topic;+ if (!topic) return false;+ notestore.setContext({+ type: "topic",+ value: { id: notebookId, topic: topicId },+ });+ console.log(topic, notebook);+ return {+ key: "notes",+ type: "notes",+ title: notebook.title,+ subtitle: topic.title,+ component: <Notes />,+ buttons: {+ back: {+ title: `Go back to ${notebook.title}`,+ action: () => navigate(`/notebooks/${notebookId}`),+ },+ search: {+ title: `Search ${notebook.title} ${topic.title} notes`,+ },+ },+ };+ },+ "/favorites": () => {+ notestore.setContext({ type: "favorites" });+ return {+ key: "notes",+ title: "Favorites",+ type: "notes",+ component: <Notes />,+ buttons: {+ search: {+ title: "Search favorite notes",+ },+ },+ };},- "/favorites": () => (- <RouteContainer- key="favorites"- title="Favorites"- type="favorites"- route={- <Notes- placeholder={FavoritesPlaceholder}- context={{ type: "favorites" }}- />- }- />- ),- "/trash": () => (- <RouteContainer key="trash" type="trash" title="Trash" route={<Trash />} />- ),- "/tags": () => (- <RouteContainer key="tags" title="Tags" type="tags" route={<Tags />} />- ),- "/tags/:tag": ({ tag }) => {- const tagItem = db.tags.tag(tag);- if (!tagItem) return navigate("/");- const { title } = tagItem;- return (- <RouteContainer- type="notes"- canGoBack- title={`#${title}`}- route={- <Notes type="notes" context={{ type: "tag", value: tagItem.id }} />- }- />- );+ "/trash": () => ({+ key: "trash",+ type: "trash",+ title: "Trash",+ component: <Trash />,+ buttons: {+ search: {+ title: "Search trash",+ },+ },+ }),+ "/tags": () => ({+ key: "tags",+ title: "Tags",+ type: "tags",+ component: <Tags />,+ buttons: {+ search: {+ title: "Search tags",+ },+ },+ }),+ "/tags/:tagId": ({ tagId }) => {+ const tag = db.tags.tag(tagId);+ if (!tag) return false;+ const { title, id } = tag;+ notestore.setContext({ type: "tag", value: id });+ return {+ key: "notes",+ type: "notes",+ title: `#${title}`,+ component: <Notes type="notes" />,+ buttons: {+ back: {+ title: "Go back to tags",+ action: () => navigate("/tags"),+ },+ search: {+ title: `Search #${title} notes`,+ },+ },+ };},- "/colors/:color": ({ color }) => {- const colorItem = db.colors.tag(color);- if (!colorItem) return navigate("/");- const { title } = colorItem;- return (- <RouteContainer- type="notes"- title={toTitleCase(title)}- route={<Notes context={{ type: "color", value: color }} />}- />- );+ "/colors/:colorId": ({ colorId }) => {+ const color = db.colors.tag(colorId);+ if (!color) return navigate("/");+ const { title, id } = color;+ notestore.setContext({ type: "color", value: id });+ return {+ key: "notes",+ type: "notes",+ title: toTitleCase(title),+ component: <Notes />,+ buttons: {+ search: {+ title: `Search ${title} colored notes`,+ },+ },+ };},- "/settings": () => (- <RouteContainer- key="settings"- noSearch- title="Settings"- route={<Settings />}- />- ),- "/search": () => (- <RouteContainer- key="search"- type="search"- canGoBack- noSearch- title="Search"- route={<Search />}- />- ),+ "/settings": () => ({+ key: "settings",+ title: "Settings",+ component: <Settings />,+ }),+ "/search/:type": ({ type }) => ({+ key: "search",+ type: "search",+ title: "Search",+ component: <Search type={type} />,+ buttons: {+ back: {+ title: `Go back to ${type}`,+ action: () => window.history.back(),+ },+ },+ }),};export default routes;diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.jsindex e3352058e..1d4a7e16b 100644--- a/apps/web/src/stores/app-store.js+++ b/apps/web/src/stores/app-store.js@@ -99,7 +99,7 @@ class AppStore extends BaseStore {break;}case "topic": {- notebookStore.setSelectedNotebookTopics(item.notebookId);+ notebookStore.setSelectedNotebook(item.notebookId);break;}case "tag": {diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.jsindex 82ea74796..a13cfc797 100644--- a/apps/web/src/stores/note-store.js+++ b/apps/web/src/stores/note-store.js@@ -32,6 +32,10 @@ class NoteStore extends BaseStore {Config.get("selectedGroup"),Config.get("sortDirection", "desc"));+ console.log(+ "Refresh notes:",+ notes.every((v) => Object.isExtensible(v))+ );this.set((state) => {state.notes = qclone(notes);});@@ -45,8 +49,14 @@ class NoteStore extends BaseStore {};setContext = (context) => {- let notes = notesFromContext(context);- this.set((state) => (state.context = { ...context, notes }));+ db.notes.init().then(() => {+ this.set((state) => {+ state.context = {+ ...context,+ notes: qclone(notesFromContext(context)),+ };+ });+ });};delete = async (id) => {diff --git a/apps/web/src/stores/notebook-store.js b/apps/web/src/stores/notebook-store.jsindex b5ad68e52..8978cd6d6 100644--- a/apps/web/src/stores/notebook-store.js+++ b/apps/web/src/stores/notebook-store.js@@ -25,9 +25,8 @@ class NotebookStore extends BaseStore {await db.notebooks.delete(id);this.set((state) => {state.notebooks.splice(index, 1);- trashStore.refresh();- appStore.refreshMenuPins();});+ appStore.refreshMenuPins();};pin = async (notebookId) => {@@ -39,7 +38,7 @@ class NotebookStore extends BaseStore {this.refresh();};- setSelectedNotebookTopics = (id) => {+ setSelectedNotebook = (id) => {this.set((state) => {state.selectedNotebookTopics = db.notebooks.notebook(id).topics.all;state.selectedNotebookId = id;diff --git a/apps/web/src/utils/use-navigate.js b/apps/web/src/utils/use-navigate.jsnew file mode 100644index 000000000..fdd65ade3--- /dev/null+++ b/apps/web/src/utils/use-navigate.js@@ -0,0 +1,22 @@+const { useEffect } = require("react");+const { NavigationEvents } = require("../navigation");++function useNavigate(routeKey, onNavigation) {+ useEffect(() => {+ onNavigation();+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, []);++ useEffect(() => {+ function onNavigate(route) {+ if (route?.key === routeKey) {+ onNavigation();+ }+ }+ NavigationEvents.subscribe("onNavigate", onNavigate);+ return () => {+ NavigationEvents.unsubscribe("onNavigate", onNavigate);+ };+ }, [routeKey, onNavigation]);+}+export default useNavigate;diff --git a/apps/web/src/utils/use-routes.js b/apps/web/src/utils/use-routes.jsindex 5a1d573bf..c27f1741c 100644--- a/apps/web/src/utils/use-routes.js+++ b/apps/web/src/utils/use-routes.js@@ -13,7 +13,9 @@ export default function useRoutes(routes, options) {for (var key in routes) {const [match, params] = matcher(key, location);if (match) {- return routes[key](params);+ const result = routes[key](params);+ if (!result) break;+ return result;}}if (!options) return;diff --git a/apps/web/src/views/home.js b/apps/web/src/views/home.jsindex fcf1e48d5..94ba627ce 100644--- a/apps/web/src/views/home.js+++ b/apps/web/src/views/home.js@@ -13,7 +13,7 @@ function Home() {useEffect(() => {(async function () {const intitialized = db.notes.initialized;- if (!intitialized) {+ if (!intitialized || !store.notes.length) {await db.notes.init();store.refresh();setIsLoading(false);diff --git a/apps/web/src/views/notebooks.js b/apps/web/src/views/notebooks.jsindex 14a1dc56b..f4efe4932 100644--- a/apps/web/src/views/notebooks.js+++ b/apps/web/src/views/notebooks.js@@ -3,9 +3,10 @@ import ListContainer from "../components/list-container";import { useStore, store } from "../stores/notebook-store";import NotebooksPlaceholder from "../components/placeholders/notebooks-placeholder";import { hashNavigate } from "../navigation";+import useNavigate from "../utils/use-navigate";function Notebooks() {- useEffect(() => store.refresh(), []);+ useNavigate("notebooks", () => store.refresh());const notebooks = useStore((state) => state.notebooks);return (diff --git a/apps/web/src/views/notes.js b/apps/web/src/views/notes.jsindex b42293f09..1f4bd2c60 100644--- a/apps/web/src/views/notes.js+++ b/apps/web/src/views/notes.js@@ -1,34 +1,21 @@-import React, { useEffect, useState } from "react";+import React from "react";import ListContainer from "../components/list-container";import { useStore as useNotesStore } from "../stores/note-store";import NotesPlaceholder from "../components/placeholders/notesplacholder";-import { db } from "../common";import { hashNavigate } from "../navigation";+import FavoritesPlaceholder from "../components/placeholders/favorites-placeholder";-function Notes(props) {- const [isLoading, setIsLoading] = useState(true);+function Notes() {const context = useNotesStore((store) => store.context);- const setContext = useNotesStore((store) => store.setContext);-- useEffect(() => {- (async function () {- await db.notes.init();- if (props.context) {- setContext(props.context);- }- setIsLoading(false);- })();- }, [props.context, setContext]);-if (!context) return null;-return (<ListContainertype="notes"- isLoading={isLoading}- context={context}+ context={{ ...context, notes: undefined }}items={context.notes}- placeholder={props.placeholder || NotesPlaceholder}+ placeholder={+ context.type === "favorites" ? FavoritesPlaceholder : NotesPlaceholder+ }button={{content: "Make a new note",onClick: () => hashNavigate("/notes/create", true),diff --git a/apps/web/src/views/search.js b/apps/web/src/views/search.jsindex 56108d5a9..5d74acfb5 100644--- a/apps/web/src/views/search.js+++ b/apps/web/src/views/search.js@@ -32,13 +32,12 @@ async function typeToItems(type, context) {}}-function Search() {+function Search({ type }) {const [searchState, setSearchState] = useState({isSearching: false,totalItems: 0,});const [results, setResults] = useState([]);- const [{ type }] = useQueryParams();const context = useNoteStore((store) => store.context);return (diff --git a/apps/web/src/views/tags.js b/apps/web/src/views/tags.jsindex 9b19cdf31..7fb40e310 100644--- a/apps/web/src/views/tags.js+++ b/apps/web/src/views/tags.js@@ -1,14 +1,12 @@-import React, { useEffect } from "react";+import React from "react";import ListContainer from "../components/list-container";import { useStore, store } from "../stores/tag-store";import TagsPlaceholder from "../components/placeholders/tags-placeholder";+import useNavigate from "../utils/use-navigate";function Tags() {+ useNavigate("tags", () => store.refresh());const tags = useStore((store) => store.tags);- useEffect(() => {- store.refresh();- }, []);-return (<ListContainer type="tags" items={tags} placeholder={TagsPlaceholder} />);diff --git a/apps/web/src/views/topics.js b/apps/web/src/views/topics.jsindex 5580a6c14..fc7949acb 100644--- a/apps/web/src/views/topics.js+++ b/apps/web/src/views/topics.js@@ -1,29 +1,21 @@-import React, { useEffect } from "react";+import React from "react";import { Flex } from "rebass";import ListContainer from "../components/list-container";import { useStore as useNbStore } from "../stores/notebook-store";import { hashNavigate } from "../navigation";-function Topics(props) {- const { notebookId } = props;-- const setSelectedNotebookTopics = useNbStore(- (store) => store.setSelectedNotebookTopics- );+function Topics() {const selectedNotebookTopics = useNbStore((store) => store.selectedNotebookTopics);-- useEffect(() => {- setSelectedNotebookTopics(notebookId);- }, [setSelectedNotebookTopics, notebookId]);+ const selectedNotebookId = useNbStore((store) => store.selectedNotebookId);return (<><ListContainertype="topics"items={selectedNotebookTopics}- context={{ notebookId }}+ context={{ notebookId: selectedNotebookId }}placeholder={Flex}button={{content: "Add a new topic",diff --git a/apps/web/src/views/trash.js b/apps/web/src/views/trash.jsindex 9ecd89977..893d3cedc 100644--- a/apps/web/src/views/trash.js+++ b/apps/web/src/views/trash.js@@ -6,11 +6,13 @@ import { useStore, store } from "../stores/trash-store";import TrashPlaceholder from "../components/placeholders/trash-placeholder";import { showToast } from "../utils/toast";import { Text } from "rebass";+import useNavigate from "../utils/use-navigate";function Trash() {- useEffect(() => store.refresh(), []);+ useNavigate("trash", () => store.refresh());const items = useStore((store) => store.trash);const clearTrash = useStore((store) => store.clear);+return (<ListContainertype="trash"