Skip to content

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

feat: migrate whole search engine to zustand state

thecodrrd403a38e2bec
diff --git a/apps/web/src/components/list-container/index.js b/apps/web/src/components/list-container/index.js
index 31ec37740..566b4b8cc 100644
--- a/apps/web/src/components/list-container/index.js
+++ b/apps/web/src/components/list-container/index.js
@@ -1,15 +1,23 @@
-import React from "react";
+import React, { useEffect } from "react";
import { Flex } from "rebass";
import Button from "../button";
import Search from "../search";
import * as Icon from "react-feather";
import { Virtuoso as List } from "react-virtuoso";
-import RootNavigator from "../../navigation/navigators/rootnavigator";
-
+import { useStore as useSearchStore } from "../../stores/searchstore";
const ListContainer = props => {
+ const setSearchContext = useSearchStore(store => store.setSearchContext);
+ useEffect(() => {
+ if (props.noSearch) return;
+ setSearchContext({
+ items: props.items,
+ item: props.item,
+ type: props.type
+ });
+ }, [setSearchContext, props.item, props.items, props.type, props.noSearch]);
return (
<Flex flexDirection="column" flex="1 1 auto">
- {!props.itemsLength && props.placeholder ? (
+ {!props.items.length && props.placeholder ? (
<Flex
flexDirection="column"
alignSelf="center"
@@ -20,21 +28,7 @@ const ListContainer = props => {
</Flex>
) : (
<>
- {!props.noSearch && (
- <Search
- autoFocus={!!props.term}
- defaultValue={props.term}
- placeholder={props.searchPlaceholder}
- onChange={e => {
- if (e.target.value.length > 2) {
- RootNavigator.navigate("search", {
- term: e.target.value,
- ...props.searchParams
- });
- }
- }}
- />
- )}
+ {!props.noSearch && <Search type={props.type} />}
<Flex
flexDirection="column"
flex="1 1 auto"
@@ -50,8 +44,8 @@ const ListContainer = props => {
height: "auto",
overflowX: "hidden"
}}
- totalCount={props.itemsLength}
- item={props.item}
+ totalCount={props.items.length}
+ item={index => props.item(index, props.items[index])}
/>
)}
</Flex>
diff --git a/apps/web/src/components/search/index.js b/apps/web/src/components/search/index.js
index 7cb9f2da9..acff0dcd7 100644
--- a/apps/web/src/components/search/index.js
+++ b/apps/web/src/components/search/index.js
@@ -2,39 +2,53 @@ import React from "react";
import { Flex, Box } from "rebass";
import { Input } from "@rebass/forms";
import * as Icon from "react-feather";
+import { useStore } from "../../stores/searchstore";
import "./search.css";
-const Search = props => (
- <Flex
- px={2}
- flexDirection="row"
- justifyContent="center"
- alignItems="center"
- sx={{
- position: "relative",
- marginBottom: 0
- }}
- >
- <Input
- id="searchInput"
- variant="default"
- name="search"
- autoFocus={props.autoFocus}
- defaultValue={props.defaultValue}
- placeholder={props.placeholder}
- onChange={props.onChange}
- />
- <Box
- id="searchIcon"
+import RootNavigator from "../../navigation/navigators/rootnavigator";
+var query = "";
+const Search = props => {
+ const search = useStore(store => store.search);
+ return (
+ <Flex
+ px={2}
+ flexDirection="row"
+ justifyContent="center"
+ alignItems="center"
sx={{
- position: "absolute",
- right: 0,
- marginRight: 20,
- color: "hover",
- height: 24
+ position: "relative",
+ marginBottom: 0
}}
>
- <Icon.Search />
- </Box>
- </Flex>
-);
+ <Input
+ id="searchInput"
+ variant="default"
+ name="search"
+ autoFocus={!!query}
+ defaultValue={query}
+ placeholder={`Search your ${props.type}`}
+ onChange={e => {
+ query = e.target.value;
+ if (query.length >= 1) {
+ search(query);
+ RootNavigator.navigate("search");
+ } else {
+ RootNavigator.goBack();
+ }
+ }}
+ />
+ <Box
+ id="searchIcon"
+ sx={{
+ position: "absolute",
+ right: 0,
+ marginRight: 20,
+ color: "hover",
+ height: 24
+ }}
+ >
+ <Icon.Search />
+ </Box>
+ </Flex>
+ );
+};
export default Search;
diff --git a/apps/web/src/stores/search-store.js b/apps/web/src/stores/search-store.js
new file mode 100644
index 000000000..be0ae3843
--- /dev/null
+++ b/apps/web/src/stores/search-store.js
@@ -0,0 +1,28 @@
+import createStore from "../common/store";
+import { db } from "../common";
+
+function searchStore(set, get) {
+ return {
+ type: "",
+ items: [],
+ item: undefined,
+ results: [],
+ setSearchContext: function(context) {
+ set(state => {
+ state.type = context.type;
+ state.items = context.items;
+ state.item = context.item;
+ });
+ },
+ search: function(query) {
+ const { items, type } = get();
+ set(state => {
+ state.results = db.lookup[type](items, query);
+ });
+ }
+ };
+}
+
+const [useStore, store] = createStore(searchStore);
+
+export { useStore, store };
diff --git a/apps/web/src/views/Home.js b/apps/web/src/views/Home.js
index 3b0f4045f..f286394fe 100644
--- a/apps/web/src/views/Home.js
+++ b/apps/web/src/views/Home.js
@@ -7,24 +7,19 @@ import { useStore as useEditorStore } from "../stores/editor-store";
import ListContainer from "../components/list-container";
import NotesPlaceholder from "../components/placeholders/notesplacholder";
-function Home(props) {
+function Home() {
useEffect(() => store.getState().refresh(), []);
const notes = useStore(store => store.notes);
const newSession = useEditorStore(store => store.newSession);
return (
<ListContainer
- term={props.term}
- itemsLength={notes.items.length}
+ items={notes.items}
+ type="notes"
+ item={(index, item) => (
+ <Note index={index} pinnable={false} item={item} />
+ )}
placeholder={NotesPlaceholder}
- searchPlaceholder={"Search your notes"}
button={{ content: "Make a new note", onClick: () => newSession() }}
- searchParams={{
- items: notes.items,
- type: "notes",
- item: (index, item) => (
- <Note index={index} pinnable={false} item={item} />
- )
- }}
>
<GroupList
style={{
@@ -56,12 +51,7 @@ function Home(props) {
}
item={(index, groupIndex) =>
notes.groupCounts[groupIndex] && (
- <Note
- index={index}
- pinnable={true}
- groupIndex={groupIndex}
- item={notes.items[index]}
- />
+ <Note index={index} pinnable={true} item={notes.items[index]} />
)
}
/>
diff --git a/apps/web/src/views/Notebooks.js b/apps/web/src/views/Notebooks.js
index 038c6ee31..675d2b29d 100644
--- a/apps/web/src/views/Notebooks.js
+++ b/apps/web/src/views/Notebooks.js
@@ -7,27 +7,6 @@ import ListContainer from "../components/list-container";
import { useStore, store } from "../stores/notebook-store";
import NotebooksPlaceholder from "../components/placeholders/notebooks-placeholder";
-const NotebookItem = props => (index, item) => (
- <Notebook
- index={index}
- item={item}
- onClick={() => {
- props.navigator.navigate("topics", {
- title: item.title,
- topics: item.topics,
- notebook: item
- });
- }}
- onTopicClick={(notebook, topic) =>
- props.navigator.navigate("notes", {
- title: notebook.title,
- subtitle: topic.title,
- notes: db.notebooks.notebook(notebook.id).topics.topic(topic.title).all
- })
- }
- />
-);
-
const Notebooks = props => {
const [open, setOpen] = useState(false);
useEffect(() => store.getState().refresh(), []);
@@ -36,16 +15,31 @@ const Notebooks = props => {
return (
<>
<ListContainer
- searchPlaceholder="Search your notebooks"
- term={props.term}
- itemsLength={notebooks.length}
+ type="notebooks"
+ items={notebooks}
+ item={(index, item) => (
+ <Notebook
+ index={index}
+ item={item}
+ onClick={() => {
+ props.navigator.navigate("topics", {
+ title: item.title,
+ topics: item.topics,
+ notebook: item
+ });
+ }}
+ onTopicClick={(notebook, topic) =>
+ props.navigator.navigate("notes", {
+ title: notebook.title,
+ subtitle: topic.title,
+ notes: db.notebooks
+ .notebook(notebook.id)
+ .topics.topic(topic.title).all
+ })
+ }
+ />
+ )}
placeholder={NotebooksPlaceholder}
- searchParams={{
- items: notebooks,
- type: "notebooks",
- item: NotebookItem(props)
- }}
- item={index => NotebookItem(props)(index, notebooks[index])}
button={{
content: "Create a notebook",
onClick: async () => {
diff --git a/apps/web/src/views/Notes.js b/apps/web/src/views/Notes.js
index 4e011d0ef..621a171c0 100644
--- a/apps/web/src/views/Notes.js
+++ b/apps/web/src/views/Notes.js
@@ -18,19 +18,11 @@ const Notes = props => {
}, [clearSelectedContext]);
return (
<ListContainer
- item={index => (
- <Note index={index} pinnable={false} item={selectedNotes[index]} />
+ type="notes"
+ items={selectedNotes}
+ item={(index, item) => (
+ <Note index={index} pinnable={false} item={item} />
)}
- term={props.term}
- searchPlaceholder="Search notes"
- searchParams={{
- type: "notes",
- items: selectedNotes,
- item: (index, item) => (
- <Note index={index} pinnable={false} item={item} />
- )
- }}
- itemsLength={selectedNotes.length}
button={{
content: "Make a new note",
onClick: () =>
diff --git a/apps/web/src/views/Search.js b/apps/web/src/views/Search.js
index 34e194177..37a3d38d0 100644
--- a/apps/web/src/views/Search.js
+++ b/apps/web/src/views/Search.js
@@ -1,36 +1,15 @@
-import React, { useEffect, useState } from "react";
-import { Flex } from "rebass";
+import React from "react";
import ListContainer from "../components/list-container";
import SearchInput from "../components/search";
-import RootNavigator from "../navigation/navigators/rootnavigator";
-import { db } from "../common";
+import { useStore } from "../stores/searchstore";
function Search(props) {
- const [items, setItems] = useState([]);
- useEffect(() => {
- const items = db.lookup[props.type](props.items, props.term);
- setItems(items);
- }, [props]);
+ const results = useStore(store => store.results);
+ const item = useStore(store => store.item);
return (
<>
- <SearchInput
- autoFocus
- placeholder={"Search here"}
- defaultValue={props.term}
- onChange={e => {
- if (e.target.value.length > 2) {
- const items = db.lookup[props.type](props.items, e.target.value);
- setItems(items);
- } else {
- RootNavigator.goBack({ term: e.target.value });
- }
- }}
- />
- <ListContainer
- noSearch={true}
- itemsLength={items.length}
- item={index => props.item(index, items[index])}
- />
+ <SearchInput type={""} />
+ <ListContainer noSearch items={results} item={item} />
</>
);
}
diff --git a/apps/web/src/views/Tags.js b/apps/web/src/views/Tags.js
index 9b96e8555..9cacdfd62 100644
--- a/apps/web/src/views/Tags.js
+++ b/apps/web/src/views/Tags.js
@@ -3,7 +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";
+import { useStore } from "../stores/note-store";
import TagsPlaceholder from "../components/placeholders/tags-placeholder";
const TagNode = ({ title }) => (
@@ -14,35 +14,34 @@ const TagNode = ({ title }) => (
{title}
</Text>
);
-const TagItem = props => (index, item) => {
- const { title, count } = item;
- return (
- <ListItem
- item={item}
- selectable={false}
- index={index}
- title={<TagNode title={title} />}
- info={`${count} notes`}
- onClick={() => {
- store.getState().setSelectedContext({ type: "tag", value: title });
- props.navigator.navigate("notes", {
- title: "#" + title,
- context: { tags: [title] }
- });
- }}
- />
- );
-};
+
const Tags = props => {
+ const setSelectedContext = useStore(store => store.setSelectedContext);
const tags = db.tags.all;
return (
<ListContainer
- term={props.term}
+ type="tags"
+ items={tags}
+ item={(index, item) => {
+ const { title, count } = item;
+ return (
+ <ListItem
+ item={item}
+ selectable={false}
+ index={index}
+ title={<TagNode title={title} />}
+ info={`${count} notes`}
+ onClick={() => {
+ setSelectedContext({ type: "tag", value: title });
+ props.navigator.navigate("notes", {
+ title: "#" + title,
+ context: { tags: [title] }
+ });
+ }}
+ />
+ );
+ }}
placeholder={TagsPlaceholder}
- itemsLength={tags.length}
- searchPlaceholder="Search tags"
- searchParams={{ type: "tags", items: tags, item: TagItem(props) }}
- item={index => TagItem(props)(index, tags[index])}
/>
);
};
diff --git a/apps/web/src/views/Topics.js b/apps/web/src/views/Topics.js
index 50c18c073..dbda9365d 100644
--- a/apps/web/src/views/Topics.js
+++ b/apps/web/src/views/Topics.js
@@ -7,25 +7,6 @@ import { useStore as useNoteStore } from "../stores/note-store";
import { useStore as useNbStore } from "../stores/notebook-store";
import { showTopicDialog } from "../components/dialogs/topicdialog";
-const TopicItem = props => (setSelectedContext, index, item) => (
- <Topic
- index={index}
- item={item}
- onClick={() => {
- let topic = item;
- setSelectedContext({
- type: "topic",
- value: topic.title,
- notebook: props.notebook
- });
- props.navigator.navigate("notes", {
- title: props.notebook.title,
- subtitle: topic.title
- });
- }}
- />
-);
-
const Topics = props => {
const setSelectedContext = useNoteStore(store => store.setSelectedContext);
const setSelectedNotebookTopics = useNbStore(
@@ -46,16 +27,27 @@ const Topics = props => {
return (
<ListContainer
- itemsLength={topics.length}
- term={props.term}
+ type="topics"
+ items={topics}
+ item={(index, item) => (
+ <Topic
+ index={index}
+ item={item}
+ onClick={() => {
+ let topic = item;
+ setSelectedContext({
+ type: "topic",
+ value: topic.title,
+ notebook: props.notebook
+ });
+ props.navigator.navigate("notes", {
+ title: props.notebook.title,
+ subtitle: topic.title
+ });
+ }}
+ />
+ )}
placeholder={Flex}
- searchPlaceholder="Search notebook topics"
- searchParams={{
- type: "topics",
- items: topics,
- item: TopicItem(props).bind(this, setSelectedContext)
- }}
- item={index => TopicItem(props)(setSelectedContext, index, topics[index])}
button={{
content: "Add more topics",
onClick: async () => {
diff --git a/apps/web/src/views/Trash.js b/apps/web/src/views/Trash.js
index c41ce5379..00b00ed37 100644
--- a/apps/web/src/views/Trash.js
+++ b/apps/web/src/views/Trash.js
@@ -1,7 +1,7 @@
import React, { useEffect } from "react";
import * as Icon from "react-feather";
import { Flex, Text } from "rebass";
-import ListItem from "../list-item";
+import ListItem from "../components/list-item";
import TimeAgo from "timeago-react";
import ListContainer from "../components/list-container";
import { confirm } from "../components/dialogs/confirm";
@@ -31,43 +31,35 @@ const menuItems = (item, index) => [
}
];
-const TrashItem = props => (index, item) => (
- <ListItem
- selectable
- item={item}
- title={item.title}
- body={item.headline}
- index={index}
- info={
- <Flex justifyContent="center" alignItems="center">
- <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)}
- dropdownRefs={dropdownRefs}
- />
-);
-
function Trash() {
useEffect(() => store.getState().refresh(), []);
const items = useStore(store => store.trash);
const clearTrash = useStore(store => store.clear);
return (
<ListContainer
- itemsLength={items.length}
- searchPlaceholder={"Search trash"}
- searchParams={{
- type: "trash",
- items: items,
- item: TrashItem()
- }}
+ type="trash"
items={items}
- item={index => TrashItem()(index, items[index])}
+ item={(index, item) => (
+ <ListItem
+ selectable
+ item={item}
+ title={item.title}
+ body={item.headline}
+ index={index}
+ info={
+ <Flex justifyContent="center" alignItems="center">
+ <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)}
+ dropdownRefs={dropdownRefs}
+ />
+ )}
button={{
content: "Clear Trash",
icon: Icon.Trash2,