Skip to content

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

fix: preserve navigation state (fixes streetwriters/notesnook#322)

thecodrr7e9d79d853d4
diff --git a/apps/web/src/components/navigation-menu/index.js b/apps/web/src/components/navigation-menu/index.js
index c017cb5e8..2db68c6e1 100644
--- a/apps/web/src/components/navigation-menu/index.js
+++ b/apps/web/src/components/navigation-menu/index.js
@@ -1,4 +1,4 @@
-import { useCallback } from "react";
+import { useCallback, useEffect } from "react";
import { Box, Flex } from "rebass";
import {
Note,
@@ -29,6 +29,7 @@ import { useStore as useThemeStore } from "../../stores/theme-store";
import useLocation from "../../hooks/use-location";
import { FlexScrollContainer } from "../scroll-container";
+const navigationHistory = new Map();
function shouldSelectNavItem(route, pin) {
if (pin.type === "notebook") {
return route === `/notebooks/${pin.id}`;
@@ -73,7 +74,7 @@ const NAVIGATION_MENU_TABLET_WIDTH = "4em";
function NavigationMenu(props) {
const { toggleNavigationContainer } = props;
- const [location] = useLocation();
+ const [location, previousLocation, state] = useLocation();
const isFocusMode = useAppStore((store) => store.isFocusMode);
const colors = useAppStore((store) => store.colors);
const pins = useAppStore((store) => store.menuPins);
@@ -88,11 +89,18 @@ function NavigationMenu(props) {
const _navigate = useCallback(
(path) => {
toggleNavigationContainer(true);
- navigate(path);
+ const nestedRoute = findNestedRoute(path);
+ navigate(!nestedRoute || nestedRoute === location ? path : nestedRoute);
},
- [toggleNavigationContainer]
+ [location, toggleNavigationContainer]
);
+ useEffect(() => {
+ if (state === "forward" || state === "neutral")
+ navigationHistory.set(location, true);
+ else navigationHistory.delete(previousLocation);
+ }, [location, previousLocation, state]);
+
return (
<AnimatedFlex
id="navigationmenu"
@@ -263,3 +271,20 @@ function NavigationMenu(props) {
);
}
export default NavigationMenu;
+
+function findNestedRoute(location) {
+ let level = location.split("/").length;
+ let nestedRoute = undefined;
+ const history = Array.from(navigationHistory.keys());
+ for (let i = history.length - 1; i >= 0; --i) {
+ const route = history[i];
+ if (!navigationHistory.get(route)) continue;
+
+ const routeLevel = route.split("/").length;
+ if (route.startsWith(location) && routeLevel > level) {
+ level = routeLevel;
+ nestedRoute = route;
+ }
+ }
+ return nestedRoute;
+}
diff --git a/apps/web/src/hooks/use-location.js b/apps/web/src/hooks/use-location.js
index 073f9fce5..416932977 100644
--- a/apps/web/src/hooks/use-location.js
+++ b/apps/web/src/hooks/use-location.js
@@ -3,16 +3,38 @@ const { getCurrentPath, NavigationEvents } = require("../navigation");
export default function useLocation() {
const [location, setLocation] = useState(getCurrentPath());
+ const [previousLocation, setPreviousLocation] = useState();
+ const [navigationState, setNavigationState] = useState();
+
useEffect(() => {
const navigateEvent = NavigationEvents.subscribe(
"onNavigate",
- (_, location) => {
- setLocation(location);
+ (_, currentLocation) => {
+ setLocation((prev) => {
+ setNavigationState(getNavigationState(currentLocation, prev));
+ setPreviousLocation(prev);
+ return currentLocation;
+ });
}
);
return () => {
navigateEvent.unsubscribe();
};
}, []);
- return [location];
+ return [location, previousLocation, navigationState];
+}
+
+function getNavigationState(currentLocation, previousLocation) {
+ if (!previousLocation || !currentLocation) return "neutral";
+
+ let currentLevels = currentLocation.split("/");
+ let previousLevels = previousLocation.split("/");
+ const isSameRoot = currentLevels[1] === previousLevels[1];
+ return isSameRoot
+ ? currentLevels.length > previousLevels.length
+ ? "forward"
+ : currentLevels.length < previousLevels.length
+ ? "backward"
+ : "same"
+ : "neutral";
}