Skip to content

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

fix: highlight selected route

thecodrr251f9af40afa
diff --git a/apps/web/src/app.js b/apps/web/src/app.js
index 515ab46d8..9f1747bf7 100644
--- a/apps/web/src/app.js
+++ b/apps/web/src/app.js
@@ -77,9 +77,9 @@ const NavMenuItem = props => {
};
function App() {
- const [selectedIndex, setSelectedIndex] = usePersistentState(
- "navSelectedIndex",
- 0
+ const [selectedKey, setSelectedKey] = usePersistentState(
+ "navSelectedKey",
+ "home"
);
const [show, setShow] = usePersistentState("navContainerState", true);
@@ -87,9 +87,8 @@ function App() {
const refreshColors = useStore(store => store.refreshColors);
const setSelectedContext = useNotesStore(store => store.setSelectedContext);
useEffect(() => {
- RootNavigator.navigate(Object.keys(RootNavigator.routes)[selectedIndex]);
+ RootNavigator.navigate(selectedKey);
refreshColors();
- console.log(colors);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const colors = useStore(store => store.colors);
@@ -129,24 +128,24 @@ function App() {
{Object.values(routes).map((item, index) => (
<NavMenuItem
onSelected={async () => {
- if (selectedIndex === index) {
+ if (selectedKey === item.key) {
setShow(!show);
return;
}
if (RootNavigator.navigate(item.key)) {
- setSelectedIndex(index);
+ setSelectedKey(item.key);
}
}}
key={item.key}
item={item}
- selected={selectedIndex === index}
+ selected={selectedKey === item.key}
/>
))}
{colors.map(color => {
return (
<NavMenuItem
onSelected={async () => {
- setSelectedIndex(-1);
+ setSelectedKey(undefined);
setSelectedContext({ type: "color", value: color.title });
RootNavigator.navigate("color", {
title: toTitleCase(color.title),
@@ -172,11 +171,12 @@ function App() {
return item.onClick();
}
if (RootNavigator.navigate(item.key)) {
- setSelectedIndex(index);
+ setSelectedKey(item.key);
}
}}
key={item.key}
item={item}
+ selected={selectedKey === item.key}
/>
))}
</Box>
diff --git a/apps/web/src/utils/hooks.js b/apps/web/src/utils/hooks.js
index 7aec1d3f1..a454cc291 100644
--- a/apps/web/src/utils/hooks.js
+++ b/apps/web/src/utils/hooks.js
@@ -1,7 +1,8 @@
import { useState } from "react";
export const usePersistentState = (key, def) => {
- const defState = JSON.parse(window.localStorage.getItem(key) || def);
+ let value = window.localStorage.getItem(key) || def;
+ const defState = tryParse(value);
const [k, setKey] = useState(defState);
const _setKey = s => {
setKey(s);
@@ -9,3 +10,11 @@ export const usePersistentState = (key, def) => {
};
return [k, _setKey];
};
+
+function tryParse(val) {
+ try {
+ return JSON.parse(val);
+ } catch (e) {
+ return val;
+ }
+}