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: make search independent of other data
thecodrrfd89615b36d6
diff --git a/apps/web/src/common/index.js b/apps/web/src/common/index.jsindex b416f99de..de05a5524 100644--- a/apps/web/src/common/index.js+++ b/apps/web/src/common/index.js@@ -2,6 +2,7 @@ import StorageInterface from "../interfaces/storage";import Database from "notes-core/api/";import SelectionOptions from "./selectionoptions";import EventSource from "eventsource";+import { navigate } from "hookrouter";export const db = new Database(StorageInterface, EventSource);@@ -24,3 +25,27 @@ export const SELECTION_OPTIONS_MAP = {};export const DEFAULT_CONTEXT = { colors: [], tags: [], notebook: {} };++export function notesFromContext(context) {+ let notes = [];+ switch (context.type) {+ case "tag":+ notes = db.notes.tagged(context.value);+ break;+ case "color":+ notes = db.notes.colored(context.value);+ if (!notes.length) return navigate("/");+ break;+ case "topic":+ const notebook = db.notebooks.notebook(context.value.id);+ const topic = notebook.topics.topic(context.value.topic);+ notes = topic.all;+ break;+ case "favorites":+ notes = db.notes.favorites;+ break;+ default:+ return [];+ }+ return notes;+}diff --git a/apps/web/src/common/list-profiles.js b/apps/web/src/common/list-profiles.jsnew file mode 100644index 000000000..0161a4e96--- /dev/null+++ b/apps/web/src/common/list-profiles.js@@ -0,0 +1,78 @@+import { navigate } from "hookrouter";+import React from "react";+import Note from "../components/note";+import Notebook from "../components/notebook";+import Tag from "../components/tag";+import Topic from "../components/topic";+import TrashItem from "../components/trash-item";+import {+ getItemHeight,+ getNotebookHeight,+ getNoteHeight,+ MAX_HEIGHTS,+} from "./height-calculator";++function createProfile(item, itemHeight, estimatedItemHeight) {+ return { item, itemHeight, estimatedItemHeight };+}++const NotesProfile = createProfile(+ (index, item, context) => (+ <Note index={index} pinnable={!!context} item={item} context={context} />+ ),+ getNoteHeight,+ MAX_HEIGHTS.note+);++const NotebooksProfile = createProfile(+ (index, item) => (+ <Notebook+ index={index}+ item={item}+ onClick={() => {+ navigate(`/notebooks/${item.id}`);+ }}+ onTopicClick={(notebook, topic) =>+ navigate(`/notebooks/${notebook.id}/${topic}`)+ }+ />+ ),+ getNotebookHeight,+ MAX_HEIGHTS.notebook+);++const TagsProfile = createProfile(+ (index, item) => <Tag item={item} index={index} />,+ getItemHeight,+ MAX_HEIGHTS.generic+);++const TopicsProfile = createProfile(+ (index, item, context) => (+ <Topic+ index={index}+ item={item}+ onClick={() => navigate(`/notebooks/${context.notebookId}/${item.id}`)}+ />+ ),+ getItemHeight,+ MAX_HEIGHTS.generic+);++const TrashProfile = createProfile(+ (index, item) => <TrashItem index={index} item={item} />,+ (item) => {+ if (item.type === "note") return getNoteHeight(item);+ else if (item.type === "notebook") return getNotebookHeight(item);+ },+ Math.max(MAX_HEIGHTS.note, MAX_HEIGHTS.notebook)+);++export default {+ home: NotesProfile,+ notebooks: NotebooksProfile,+ notes: NotesProfile,+ tags: TagsProfile,+ topics: TopicsProfile,+ trash: TrashProfile,+};diff --git a/apps/web/src/components/list-container/index.js b/apps/web/src/components/list-container/index.jsindex 87ce22f03..b965d2446 100644--- a/apps/web/src/components/list-container/index.js+++ b/apps/web/src/components/list-container/index.js@@ -1,17 +1,18 @@-import React, { useEffect, useRef } from "react";+import React, { useEffect, useMemo, useRef } from "react";import { Flex } from "rebass";import Button from "../button";import Search from "../search";import * as Icon from "../icons";import { VariableSizeList as List } from "react-window";import AutoSizer from "react-virtualized-auto-sizer";-import { useStore as useSearchStore } from "../../stores/searchstore";import { useStore as useSelectionStore } from "../../stores/selection-store";import LoginBar from "../loginbar";import GroupHeader from "../group-header";+import ListProfiles from "../../common/list-profiles";function ListContainer(props) {- const setSearchContext = useSearchStore((store) => store.setSearchContext);+ const { type, context } = props;+ const profile = useMemo(() => ListProfiles[type], [type]);const shouldSelectAll = useSelectionStore((store) => store.shouldSelectAll);const setSelectedItems = useSelectionStore((store) => store.setSelectedItems);const listRef = useRef();@@ -20,15 +21,6 @@ function ListContainer(props) {if (shouldSelectAll) setSelectedItems(props.items);}, [shouldSelectAll, setSelectedItems, props.items]);- useEffect(() => {- if (props.noSearch) return;- setSearchContext({- items: props.items,- item: props.item,- type: props.type,- });- }, [setSearchContext, props.item, props.items, props.type, props.noSearch]);-useEffect(() => {if (props.static) return;// whenever there is a change in items array we have to reset the size cache@@ -46,7 +38,7 @@ function ListContainer(props) {</Flex>) : (<>- {!props.noSearch && <Search type={props.type} />}+ <Search type={props.type} query={props.query} context={context} /><LoginBar /><Flex variant="columnFill" mt={2}>{props.children@@ -63,14 +55,14 @@ function ListContainer(props) {return item.id || item.title;}}overscanCount={2}- estimatedItemSize={props.estimatedItemHeight}+ estimatedItemSize={profile.estimatedItemHeight}itemSize={(index) => {const item = props.items[index];if (item.type === "header") {if (item.title === "Pinned") return 22;else return 22;} else {- return props.itemHeight(item);+ return profile.itemHeight(item);}}}itemCount={props.items.length}@@ -82,7 +74,7 @@ function ListContainer(props) {{item.type === "header" ? (<GroupHeader title={item.title} />) : (- props.item(index, item)+ profile.item(index, item, context))}</div>);diff --git a/apps/web/src/components/search/index.js b/apps/web/src/components/search/index.jsindex 49c8099d1..51451a918 100644--- a/apps/web/src/components/search/index.js+++ b/apps/web/src/components/search/index.js@@ -4,12 +4,9 @@ import { Input } from "@rebass/forms";import * as Icon from "../icons";import "./search.css";import { navigate } from "hookrouter";-import { useStore } from "../../stores/searchstore";-var query;function Search(props) {- const search = useStore((store) => store.search);- const { type } = props;+ const { query, type, context } = props;return (<Flex@@ -24,14 +21,17 @@ function Search(props) {name="search"autoFocus={!!query}defaultValue={query}- placeholder={`Search your ${type}`}- onChange={(e) => {- query = e.target.value;- if (query.length <= 0) return window.history.back();- if (query.length === 1) {- navigate("/search");+ placeholder={`Type your query here`}+ onKeyDown={(e) => {+ if (e.key === "Enter") {+ let query = e.target.value;+ navigate(+ `/search`,+ false,+ { q: query, type, context: btoa(JSON.stringify(context)) },+ true+ );}- search(query);}}/><Boxdiff --git a/apps/web/src/components/tag/index.js b/apps/web/src/components/tag/index.jsnew file mode 100644index 000000000..c77926edc--- /dev/null+++ b/apps/web/src/components/tag/index.js@@ -0,0 +1,32 @@+import React from "react";+import ListItem from "../list-item";+import { navigate } from "hookrouter";+import { Text } from "rebass";++function Tag({ item, index }) {+ const { title, noteIds } = item;+ return (+ <ListItem+ item={item}+ selectable={false}+ index={index}+ title={<TagNode title={title} />}+ info={`${noteIds.length} notes`}+ onClick={() => {+ navigate(`/tags/${title}`);+ }}+ />+ );+}+export default Tag;++function TagNode({ title }) {+ return (+ <Text as="span" variant="title">+ <Text as="span" color="primary">+ {"#"}+ </Text>+ {title}+ </Text>+ );+}diff --git a/apps/web/src/components/trash-item/index.js b/apps/web/src/components/trash-item/index.jsnew file mode 100644index 000000000..422501169--- /dev/null+++ b/apps/web/src/components/trash-item/index.js@@ -0,0 +1,65 @@+import React from "react";+import ListItem from "../list-item";+import { confirm } from "../dialogs/confirm";+import * as Icon from "../icons";+import { store } from "../../stores/notebook-store";+import { Flex, Text } from "rebass";+import TimeAgo from "timeago-react";+import { toTitleCase } from "../../utils/string";+import { showToast } from "../../utils/toast";+import { showPermanentDeleteToast } from "../../common/toasts";++function menuItems(item, index) {+ return [+ {+ title: "Restore",+ onClick: () => {+ store.restore(item.id, index);+ showToast(+ "success",+ `${item.type === "note" ? "Note" : "Notebook"} restored successfully!`+ );+ },+ },+ {+ title: "Delete",+ color: "red",+ onClick: () => {+ confirm(+ Icon.Trash,+ "Delete",+ `Are you sure you want to permanently delete this item?`+ ).then(async (res) => {+ if (res) {+ await store.delete(item.id, index);+ showPermanentDeleteToast(item, index);+ }+ });+ },+ },+ ];+}++function TrashItem({ item, index }) {+ return (+ <ListItem+ selectable+ item={item}+ title={item.title}+ body={item.headline || item.description}+ index={index}+ info={+ <Flex variant="rowCenter">+ <TimeAgo datetime={item.dateDeleted || item.dateCreated} />+ <Text as="span" mx={1}>+ •+ </Text>+ <Text color="primary">{toTitleCase(item.type)}</Text>+ </Flex>+ }+ menuData={item}+ menuItems={menuItems(item, index)}+ />+ );+}+export default TrashItem;diff --git a/apps/web/src/navigation/routes.js b/apps/web/src/navigation/routes.jsindex 7bcb26966..0853317c1 100644--- a/apps/web/src/navigation/routes.js+++ b/apps/web/src/navigation/routes.js@@ -37,7 +37,9 @@ const routes = {/>),"/settings": () => <RouteContainer title="Settings" route={<Settings />} />,- "/search": () => <RouteContainer title="Search" route={<Search />} />,+ "/search": () => (+ <RouteContainer canGoBack title="Search" route={<Search />} />+ ),};export default routes;diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.jsindex 258ca5874..46f7850fc 100644--- a/apps/web/src/stores/note-store.js+++ b/apps/web/src/stores/note-store.js@@ -1,4 +1,4 @@-import { db } from "../common/index";+import { db, notesFromContext } from "../common/index";import createStore from "../common/store";import { store as editorStore } from "./editor-store";import Vault from "../common/vault";@@ -47,26 +47,7 @@ class NoteStore extends BaseStore {};setContext = (context) => {- let notes = [];- switch (context.type) {- case "tag":- notes = db.notes.tagged(context.value);- break;- case "color":- notes = db.notes.colored(context.value);- if (!notes.length) return navigate("/");- break;- case "topic":- const notebook = db.notebooks.notebook(context.value.id);- const topic = notebook.topics.topic(context.value.topic);- notes = topic.all;- break;- case "favorites":- notes = db.notes.favorites;- break;- default:- return;- }+ let notes = notesFromContext(context);this.set((state) => (state.context = { ...context, notes }));};diff --git a/apps/web/src/stores/search-store.js b/apps/web/src/stores/search-store.jsindex 7d471ebfb..b656e148b 100644--- a/apps/web/src/stores/search-store.js+++ b/apps/web/src/stores/search-store.js@@ -3,21 +3,10 @@ import { db } from "../common";import BaseStore from "./index";class SearchStore extends BaseStore {- type = "";- items = [];- item = undefined;results = [];- setSearchContext = (context) => {- this.set((state) => {- state.type = context.type;- state.items = context.items;- state.item = context.item;- });- };-- search = async (query) => {- const { items, type } = this.get();+ search = async (items, query) => {+ const { type } = this.get();const results = await db.lookup[type](items, query);this.set((state) => (state.results = results));};diff --git a/apps/web/src/views/home.js b/apps/web/src/views/home.jsindex f774263df..7b20b04a0 100644--- a/apps/web/src/views/home.js+++ b/apps/web/src/views/home.js@@ -1,10 +1,8 @@import React, { useEffect } from "react";-import Note from "../components/note";import { useStore, store } from "../stores/note-store";import { useStore as useEditorStore } from "../stores/editor-store";import ListContainer from "../components/list-container";import NotesPlaceholder from "../components/placeholders/notesplacholder";-import { getNoteHeight, MAX_HEIGHTS } from "../common/height-calculator";function Home() {useEffect(() => store.refresh(), []);@@ -13,11 +11,8 @@ function Home() {return (<ListContainer- type="notes"+ type="home"items={notes}- estimatedItemHeight={MAX_HEIGHTS.note}- itemHeight={getNoteHeight}- item={(index, item) => <Note index={index} pinnable item={item} />}placeholder={NotesPlaceholder}button={{ content: "Make a new note", onClick: () => newSession() }}/>diff --git a/apps/web/src/views/notebooks.js b/apps/web/src/views/notebooks.jsindex 443e4a9f2..c676a0a5f 100644--- a/apps/web/src/views/notebooks.js+++ b/apps/web/src/views/notebooks.js@@ -1,5 +1,4 @@import React, { useState, useEffect } from "react";-import Notebook from "../components/notebook";import AddNotebookDialog from "../components/dialogs/addnotebookdialog";import ListContainer from "../components/list-container";import { useStore, store } from "../stores/notebook-store";@@ -9,7 +8,6 @@ import Notes from "./notes.js";import { useRoutes, navigate } from "hookrouter";import RouteContainer from "../components/route-container";import { db } from "../common";-import { getNotebookHeight, MAX_HEIGHTS } from "../common/height-calculator";const routes = {"/": () => (@@ -63,20 +61,6 @@ function Notebooks() {<ListContainertype="notebooks"items={notebooks}- estimatedItemHeight={MAX_HEIGHTS.notebook}- itemHeight={getNotebookHeight}- item={(index, item) => (- <Notebook- index={index}- item={item}- onClick={() => {- navigate(`/notebooks/${item.id}`);- }}- onTopicClick={(notebook, topic) =>- navigate(`/notebooks/${notebook.id}/${topic}`)- }- />- )}placeholder={NotebooksPlaceholder}button={{content: "Create a notebook",diff --git a/apps/web/src/views/notes.js b/apps/web/src/views/notes.jsindex 57d72cba3..77ea7c2f7 100644--- a/apps/web/src/views/notes.js+++ b/apps/web/src/views/notes.js@@ -1,10 +1,8 @@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 NotesPlaceholder from "../components/placeholders/notesplacholder";-import { getNoteHeight, MAX_HEIGHTS } from "../common/height-calculator";function Notes(props) {const newSession = useStore((store) => store.newSession);@@ -22,13 +20,9 @@ function Notes(props) {return (<ListContainertype="notes"- estimatedItemHeight={MAX_HEIGHTS.note}- itemHeight={getNoteHeight}+ context={props.context}items={context.notes}placeholder={props.placeholder || NotesPlaceholder}- item={(index, item) => (- <Note index={index} pinnable={false} item={item} context={context} />- )}button={{content: "Make a new note",onClick: () => newSession(props.context),diff --git a/apps/web/src/views/search.js b/apps/web/src/views/search.jsindex 856c77263..3e58943b5 100644--- a/apps/web/src/views/search.js+++ b/apps/web/src/views/search.js@@ -1,28 +1,61 @@-import React from "react";+import React, { useEffect, useMemo, useState } from "react";import ListContainer from "../components/list-container";-import SearchInput from "../components/search";-import { useStore } from "../stores/searchstore";import SearchPlaceholder from "../components/placeholders/search-placeholder";+import { useQueryParams } from "hookrouter";+import { db, notesFromContext } from "../common";-window.addEventListener("load", () => {- const path = window.location.pathname;- if (path === "/search") window.location = "/";-});+function typeToItems(type, context) {+ switch (type) {+ case "home":+ return ["notes", db.notes.all];+ case "notes":+ const notes = notesFromContext(context);+ return ["notes", notes];+ case "notebooks":+ return ["notebooks", db.notebooks.all];+ case "topics":+ const { notebookId } = context;+ if (!notebookId) return ["topics", []];+ const topics = db.notebooks.notebook(notebookId).topics.all;+ return ["topics", topics];+ case "trash":+ return ["trash", db.trash.all];+ default:+ return [];+ }+}-// TODO this will break for now.function Search() {- const results = useStore((store) => store.results);- const item = useStore((store) => store.item);- const type = useStore((store) => store.type);+ const [results, setResults] = useState([]);+ const [params] = useQueryParams();+ const { q: query, type, context: ctx } = params;+ const context = useMemo(() => {+ try {+ return ctx && JSON.parse(atob(ctx));+ } catch (e) {+ console.log(e);+ }+ }, [ctx]);++ useEffect(() => {+ const [lookupType, items] = typeToItems(type, context);+ if (lookupType === "notes") {+ db.lookup[lookupType](items, query).then((results) => {+ setResults(results);+ });+ } else {+ setResults(db.lookup[lookupType](items, query));+ }+ }, [context, query, type]);return (<>- <SearchInput type={type} /><ListContainer- noSearch+ context={context}+ type={type}+ query={query}items={results}placeholder={SearchPlaceholder}- item={item}/></>);diff --git a/apps/web/src/views/tags.js b/apps/web/src/views/tags.jsindex 18afb1f02..ddd17965e 100644--- a/apps/web/src/views/tags.js+++ b/apps/web/src/views/tags.js@@ -1,24 +1,10 @@import React, { useEffect } from "react";-import { Text } from "rebass";import ListContainer from "../components/list-container";-import ListItem from "../components/list-item";import { useStore, store } from "../stores/tag-store";import TagsPlaceholder from "../components/placeholders/tags-placeholder";import Notes from "./notes";-import { useRoutes, navigate } from "hookrouter";+import { useRoutes } from "hookrouter";import RouteContainer from "../components/route-container";-import { getItemHeight, MAX_HEIGHTS } from "../common/height-calculator";--function TagNode({ title }) {- return (- <Text as="span" variant="title">- <Text as="span" color="primary">- {"#"}- </Text>- {title}- </Text>- );-}const routes = {"/": () => <RouteContainer title="Tags" route={<Tags />} />,@@ -44,28 +30,7 @@ function Tags() {}, []);return (- <ListContainer- type="tags"- items={tags}- itemHeight={getItemHeight}- estimatedItemHeight={MAX_HEIGHTS.generic}- item={(index, item) => {- const { title, noteIds } = item;- return (- <ListItem- item={item}- selectable={false}- index={index}- title={<TagNode title={title} />}- info={`${noteIds.length} notes`}- onClick={() => {- navigate(`/tags/${title}`);- }}- />- );- }}- placeholder={TagsPlaceholder}- />+ <ListContainer type="tags" items={tags} placeholder={TagsPlaceholder} />);}diff --git a/apps/web/src/views/topics.js b/apps/web/src/views/topics.jsindex e8ba79fda..7171731ed 100644--- a/apps/web/src/views/topics.js+++ b/apps/web/src/views/topics.js@@ -1,11 +1,8 @@import React, { useState, useEffect } from "react";-import Topic from "../components/topic";import { Flex } from "rebass";import ListContainer from "../components/list-container";import { useStore as useNbStore } from "../stores/notebook-store";import { showTopicDialog } from "../components/dialogs/topicdialog";-import { navigate } from "hookrouter";-import { getItemHeight, MAX_HEIGHTS } from "../common/height-calculator";function Topics(props) {const { notebookId } = props;@@ -31,15 +28,7 @@ function Topics(props) {<ListContainertype="topics"items={topics}- itemHeight={getItemHeight}- estimatedItemHeight={MAX_HEIGHTS.generic}- item={(index, item) => (- <Topic- index={index}- item={item}- onClick={() => navigate(`/notebooks/${notebookId}/${item.id}`)}- />- )}+ context={{ notebookId }}placeholder={Flex}button={{content: "Add more topics",diff --git a/apps/web/src/views/trash.js b/apps/web/src/views/trash.jsindex 1ab9caecf..a76997c58 100644--- a/apps/web/src/views/trash.js+++ b/apps/web/src/views/trash.js@@ -1,51 +1,10 @@import React, { useEffect } from "react";import * as Icon from "../components/icons";-import { Flex, Text } from "rebass";-import ListItem from "../components/list-item";-import TimeAgo from "timeago-react";import ListContainer from "../components/list-container";import { confirm } from "../components/dialogs/confirm";import { useStore, store } from "../stores/trash-store";-import { toTitleCase } from "../utils/string";import TrashPlaceholder from "../components/placeholders/trash-placeholder";import { showToast } from "../utils/toast";-import { showPermanentDeleteToast } from "../common/toasts";-import {- getNotebookHeight,- getNoteHeight,- MAX_HEIGHTS,-} from "../common/height-calculator";--function menuItems(item, index) {- return [- {- title: "Restore",- onClick: () => {- store.restore(item.id, index);- showToast(- "success",- `${item.type === "note" ? "Note" : "Notebook"} restored successfully!`- );- },- },- {- title: "Delete",- color: "red",- onClick: () => {- confirm(- Icon.Trash,- "Delete",- `Are you sure you want to permanently delete this item?`- ).then(async (res) => {- if (res) {- await store.delete(item.id, index);- showPermanentDeleteToast(item, index);- }- });- },- },- ];-}function Trash() {useEffect(() => store.refresh(), []);@@ -55,32 +14,7 @@ function Trash() {<ListContainertype="trash"placeholder={TrashPlaceholder}- estimatedItemHeight={Math.max(MAX_HEIGHTS.note, MAX_HEIGHTS.notebook)}- itemHeight={(item) => {- if (item.type === "note") return getNoteHeight(item);- else if (item.type === "notebook") return getNotebookHeight(item);- }}items={items}- item={(index, item) => (- <ListItem- selectable- item={item}- title={item.title}- body={item.headline || item.description}- index={index}- info={- <Flex variant="rowCenter">- <TimeAgo datetime={item.dateDeleted || item.dateCreated} />- <Text as="span" mx={1}>- •- </Text>- <Text color="primary">{toTitleCase(item.type)}</Text>- </Flex>- }- menuData={item}- menuItems={menuItems(item, index)}- />- )}button={{content: "Clear Trash",icon: Icon.Trash,