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: sync selected tag with editor
thecodrr22db08c18d88
diff --git a/apps/web/src/common/index.js b/apps/web/src/common/index.jsindex fc5d83411..a22df31ee 100644--- a/apps/web/src/common/index.js+++ b/apps/web/src/common/index.js@@ -14,3 +14,5 @@ export const COLORS = [{ label: "purple", code: "purple" },{ label: "gray", code: "gray" }];++export const DEFAULT_CONTEXT = { colors: [], tags: [], notebook: {} };diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.jsindex 9c0284393..b1fd12655 100644--- a/apps/web/src/stores/editor-store.js+++ b/apps/web/src/stores/editor-store.js@@ -49,17 +49,23 @@ function editorStore(set, get) {},saveSession: function(oldSession) {const { session } = get();- const { title, id, content, pinned, favorite } = session;+ const { title, id, content, pinned, favorite, tags, colors } = session;let note = {content,title,id,favorite,- pinned+ pinned,+ tags,+ colors};db.notes.add(note).then(id => {+ if (tags.length > 0) updateContext("tags", tags);+ if (colors.length > 0) updateContext("colors", colors);+set(state => {state.session.id = id;+ state.session.state = SESSION_STATES.stale;notestore.getState().refresh();// we update favorites only if favorite has changed@@ -72,7 +78,6 @@ function editorStore(set, get) {setSession: function(session) {const oldSession = get().session;set(state => {- state.session.state = SESSION_STATES.stale;session(state);clearTimeout(state.session.timeout);state.session.timeout = setTimeout(() => {@@ -94,7 +99,7 @@ function editorStore(set, get) {setTagOrColor(get().session, "colors", color, "color", set);},setTag: function(tag) {- setTagOrColor(get().session, "tags", tag, "tag", set);+ setTagOrColor(get().session, "tags", tag, "tag", get().setSession);}};}@@ -119,6 +124,21 @@ function setTagOrColor(session, array, value, func, set) {}}+function updateContext(key, array) {+ let type = key === "colors" ? "color" : "tag";+ // update notes if the selected context (the current view in the navigator) is a tag or color+ const notesState = notestore.getState();+ const context = notesState.selectedContext;+ if (context.type === type) {+ array.forEach(value => {+ if (context.value === value) {+ console.log("updating according to context");+ notestore.getState().setSelectedContext(context);+ }+ });+ }+}+const [useStore, store] = createStore(editorStore);export { useStore, store, SESSION_STATES };diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.jsindex e310c05f2..2909d9a8e 100644--- a/apps/web/src/stores/note-store.js+++ b/apps/web/src/stores/note-store.js@@ -15,6 +15,8 @@ function noteStore(set, get) {groups: []},favorites: [],+ selectedNotes: [],+ selectedContext: {},refresh: function() {set(state => {//TODO save group type@@ -26,6 +28,29 @@ function noteStore(set, get) {state[listType] = db.notes[listType];});},+ setSelectedContext: function(context) {+ let notes = [];+ switch (context.type) {+ case "tag":+ notes = db.notes.tagged(context.value);+ break;+ case "color":+ notes = db.notes.colored(context.value);+ break;+ default:+ return;+ }+ set(state => {+ state.selectedContext = context;+ state.selectedNotes = notes;+ });+ },+ clearSelectedContext: function() {+ set(state => {+ state.selectedContext = {};+ state.selectedNotes = [];+ });+ },delete: async function(id, info) {await db.notes.delete(id);set(state => {diff --git a/apps/web/src/views/Notes.js b/apps/web/src/views/Notes.jsindex bdf1c3523..0ab596fe6 100644--- a/apps/web/src/views/Notes.js+++ b/apps/web/src/views/Notes.js@@ -1,21 +1,30 @@-import React from "react";+import React, { useEffect } from "react";import Note from "../components/note";import ListContainer from "../components/list-container";import { useStore } from "../stores/editor-store";+import { useStore as useNotesStore } from "../stores/note-store";+import { DEFAULT_CONTEXT } from "../common";const Notes = props => {const newSession = useStore(store => store.newSession);+ const selectedNotes = useNotesStore(store => store.selectedNotes);+ const clearSelectedContext = useNotesStore(+ store => store.clearSelectedContext+ );+ useEffect(() => {+ return () => {+ clearSelectedContext();+ };+ }, [clearSelectedContext]);return (<ListContainer- item={index => <Note index={index} item={props.notes[index]} />}- itemsLength={props.notes.length}+ item={index => <Note index={index} item={selectedNotes[index]} />}+ itemsLength={selectedNotes.length}button={{content: "Make a new note",onClick: () =>newSession({- colors: [],- tags: [],- notebook: {},+ ...DEFAULT_CONTEXT,...props.context})}}diff --git a/apps/web/src/views/Tags.js b/apps/web/src/views/Tags.jsindex 74ae27670..0d99490af 100644--- a/apps/web/src/views/Tags.js+++ b/apps/web/src/views/Tags.js@@ -3,6 +3,7 @@ import { Flex, Text } from "rebass";import ListContainer from "../components/list-container";import ListItem from "../components/list-item";import { db } from "../common";+import { store } from "../stores/note-store";const TagNode = ({ title }) => (<Text as="span" fontFamily={"body"} fontSize="title" fontWeight={"body"}>@@ -25,9 +26,10 @@ const Tags = props => {index={index}title={<TagNode title={title} />}onClick={() => {- const notesOfTag = db.notes.tagged(title);+ store+ .getState()+ .setSelectedContext({ type: "tag", value: title });props.navigator.navigate("notes", {- notes: notesOfTag,title: "#" + title,context: { tags: [title] }});