Skip to content

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

refactor: separate notebooks store

thecodrrafc6d4cd05ca
diff --git a/apps/web/package.json b/apps/web/package.json
index 9f01bc12f..0f278d507 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -6,6 +6,7 @@
"@rebass/forms": "^4.0.6",
"emotion-theming": "^10.0.19",
"events": "^3.0.0",
+ "immer": "^5.3.6",
"localforage": "^1.7.3",
"notes-core": "npm:@streetwriters/notesnook-core@latest",
"quill-magic-url": "^1.0.3",
diff --git a/apps/web/src/common/nbstore.js b/apps/web/src/common/nbstore.js
new file mode 100644
index 000000000..17eb78985
--- /dev/null
+++ b/apps/web/src/common/nbstore.js
@@ -0,0 +1,31 @@
+import { db } from "./index";
+import createStore from "./store";
+
+function notebookStore(set) {
+ return {
+ init: function() {
+ set(state => (state.notebooks = db.notebooks.all));
+ },
+ notebooks: [],
+ add: async function(nb) {
+ let notebook = await db.notebooks.add(nb);
+ if (notebook) {
+ set(state => state.notebooks.push(nb));
+ }
+ },
+ delete: function() {},
+ update: function() {},
+ pin: async function(notebook, index) {
+ await db.notebooks.notebook(notebook).pin();
+ set(state => (state.notebooks[index].pinned = !notebook.pinned));
+ },
+ favorite: async function(notebook, index) {
+ await db.notebooks.notebook(notebook).favorite();
+ set(state => (state.notebooks[index].favorite = !notebook.favorite));
+ }
+ };
+}
+
+const [useStore, store] = createStore(notebookStore);
+
+export { useStore, store };
diff --git a/apps/web/src/common/store.js b/apps/web/src/common/store.js
index 1c8a4d30e..3f42c3b69 100644
--- a/apps/web/src/common/store.js
+++ b/apps/web/src/common/store.js
@@ -1,20 +1,14 @@
-import { db } from "./index";
+import produce from "immer";
import create from "zustand";
-const [useStore] = create(set => ({
- init: () => {
- console.log("initializing state...");
- set({
- notebooks: db.notebooks.all
- });
- },
- notebooks: [],
- isSideMenuOpen: false,
- addNotebook: async notebook => {
- if (await db.notebooks.add(notebook)) {
- set({ notebooks: db.notebooks.all });
- }
- }
-}));
+function immer(config) {
+ return function(set, get, api) {
+ return config(fn => set(produce(fn)), get, api);
+ };
+}
-export default useStore;
+function createStore(store) {
+ return create(immer(store));
+}
+
+export default createStore;
diff --git a/apps/web/src/components/notebook/index.js b/apps/web/src/components/notebook/index.js
index b007fa23d..9769fa808 100644
--- a/apps/web/src/components/notebook/index.js
+++ b/apps/web/src/components/notebook/index.js
@@ -4,30 +4,17 @@ import * as Icon from "react-feather";
import ListItem from "../list-item";
import { db, ev } from "../../common";
import { showSnack } from "../snackbar";
+import { store } from "../../common/notebook-store";
const dropdownRefs = [];
-const menuItems = notebook => [
+const menuItems = (notebook, index) => [
{
title: notebook.pinned ? "Unpin" : "Pin",
- onClick: async () =>
- db.notebooks
- .notebook(notebook.id)
- .pin()
- .then(() => {
- showSnack("Notebook pinned!", Icon.Check);
- ev.emit("refreshNotebooks");
- })
+ onClick: () => store.getState().pin(notebook, index)
},
{
title: notebook.favorite ? "Unfavorite" : "Favorite",
- onClick: async () =>
- db.notebooks
- .notebook(notebook.id)
- .favorite()
- .then(() => {
- showSnack("Notebook favorited!", Icon.Check);
- ev.emit("refreshNotebooks");
- })
+ onClick: () => store.getState().favorite(notebook, index)
},
{ title: "Edit" },
{ title: "Share" },
@@ -45,57 +32,68 @@ const menuItems = notebook => [
}
}
];
-const Notebook = ({ item, index, onClick, onTopicClick }) => {
- const notebook = item;
- return (
- <ListItem
- onClick={onClick}
- title={notebook.title}
- body={notebook.description}
- subBody={
- <Flex sx={{ marginBottom: 1 }}>
- {notebook.topics.slice(1, 4).map(topic => (
- <Flex
- onClick={e => {
- onTopicClick(notebook, topic);
- e.stopPropagation();
- }}
- key={topic.id + topic.title}
- bg="primary"
- px={2}
- py={1}
- sx={{
- marginRight: 1,
- borderRadius: "default",
- color: "static"
- }}
- >
- <Text variant="body" fontSize={11}>
- {topic.title}
- </Text>
- </Flex>
- ))}
- </Flex>
- }
- info={
- <Flex justifyContent="center" alignItems="center">
- {new Date(notebook.dateCreated).toDateString().substring(4)}
- <Text as="span" mx={1}>
- •
- </Text>
- <Text>{notebook.totalNotes} Notes</Text>
- {notebook.favorite && (
- <Icon.Star size={16} style={{ marginLeft: 5 }} />
- )}
- </Flex>
- }
- pinned={notebook.pinned}
- dropdownRefs={dropdownRefs}
- index={index}
- menuData={notebook}
- menuItems={menuItems(notebook)}
- />
- );
-};
-export default Notebook;
+export default class Notebook extends React.Component {
+ shouldComponentUpdate(nextProps) {
+ const prevItem = this.props.item;
+ const nextItem = nextProps.item;
+ return (
+ prevItem.pinned !== nextItem.pinned ||
+ prevItem.favorite !== nextItem.favorite
+ );
+ }
+ render() {
+ const { item, index, onClick, onTopicClick } = this.props;
+ const notebook = item;
+ console.log("rendering notebook", notebook.id);
+ return (
+ <ListItem
+ onClick={onClick}
+ title={notebook.title}
+ body={notebook.description}
+ subBody={
+ <Flex sx={{ marginBottom: 1 }}>
+ {notebook.topics.slice(1, 4).map(topic => (
+ <Flex
+ onClick={e => {
+ onTopicClick(notebook, topic);
+ e.stopPropagation();
+ }}
+ key={topic.id + topic.title}
+ bg="primary"
+ px={2}
+ py={1}
+ sx={{
+ marginRight: 1,
+ borderRadius: "default",
+ color: "static"
+ }}
+ >
+ <Text variant="body" fontSize={11}>
+ {topic.title}
+ </Text>
+ </Flex>
+ ))}
+ </Flex>
+ }
+ info={
+ <Flex justifyContent="center" alignItems="center">
+ {new Date(notebook.dateCreated).toDateString().substring(4)}
+ <Text as="span" mx={1}>
+ •
+ </Text>
+ <Text>{notebook.totalNotes} Notes</Text>
+ {notebook.favorite && (
+ <Icon.Star size={16} style={{ marginLeft: 5 }} />
+ )}
+ </Flex>
+ }
+ pinned={notebook.pinned}
+ dropdownRefs={dropdownRefs}
+ index={index}
+ menuData={notebook}
+ menuItems={menuItems(notebook, index)}
+ />
+ );
+ }
+}
diff --git a/apps/web/src/views/Notebooks.js b/apps/web/src/views/Notebooks.js
index 7410a1005..8ef630384 100644
--- a/apps/web/src/views/Notebooks.js
+++ b/apps/web/src/views/Notebooks.js
@@ -4,12 +4,14 @@ import { db } from "../common";
import Notebook from "../components/notebook";
import AddNotebookDialog from "../components/dialogs/addnotebookdialog";
import ListContainer from "../components/list-container";
-import useStore from "../common/store";
+import { useStore, store } from "../common/notebook-store";
const Notebooks = props => {
const [open, setOpen] = useState(false);
+ useEffect(() => store.getState().init(), []);
const notebooks = useStore(state => state.notebooks);
const addNotebook = useStore(state => state.addNotebook);
+
return (
<>
<ListContainer
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index 003c69271..6d9a6e4fa 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -5194,6 +5194,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/immer/-/immer-1.10.0.tgz#bad67605ba9c810275d91e1c2a47d4582e98286d"
integrity sha512-O3sR1/opvCDGLEVcvrGTMtLac8GJ5IwZC4puPrLuRj3l7ICKvkmA0vGuU9OW8mV9WIBRnaxp5GJh9IEAaNOoYg==
+immer@^5.3.6:
+ version "5.3.6"
+ resolved "https://registry.yarnpkg.com/immer/-/immer-5.3.6.tgz#51eab8cbbeb13075fe2244250f221598818cac04"
+ integrity sha512-pqWQ6ozVfNOUDjrLfm4Pt7q4Q12cGw2HUZgry4Q5+Myxu9nmHRkWBpI0J4+MK0AxbdFtdMTwEGVl7Vd+vEiK+A==
+
import-cwd@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-2.1.0.tgz#aa6cf36e722761285cb371ec6519f53e2435b0a9"
@@ -7020,7 +7025,7 @@ normalize-url@^3.0.0, normalize-url@^3.0.1:
"notes-core@https://github.com/thecodrr/notes-core.git":
version "1.1.0"
- resolved "https://github.com/thecodrr/notes-core.git#e591bdeff6852a104ff0db86a0235a95dfbd1fa3"
+ resolved "https://github.com/thecodrr/notes-core.git#feadc3ac93504309afa35e42c4e225fadacb3ee7"
dependencies:
fast-sort "^2.0.1"
fuzzysearch "^1.0.3"