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
web: use same search ux for notes everywhere
Abdullah Atta0e0add2f7129
diff --git a/apps/web/src/views/all-notes.tsx b/apps/web/src/views/all-notes.tsxindex 3027a87db..de15b8b93 100644--- a/apps/web/src/views/all-notes.tsx+++ b/apps/web/src/views/all-notes.tsx@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/-import React, { useEffect, useRef } from "react";+import React, { useEffect } from "react";import { useStore } from "../stores/note-store";import ListContainer from "../components/list-container";import useNavigate from "../hooks/use-navigate";@@ -26,24 +26,9 @@ import { useSearch } from "../hooks/use-search";import { db } from "../common/db";import { useEditorStore } from "../stores/editor-store";import { ListLoader } from "../components/loaders/list-loader";-import { Box, Text } from "@theme-ui/components";-import { strings } from "@notesnook/intl";-import {- TreeNode,- VirtualizedTree,- VirtualizedTreeHandle-} from "../components/virtualized-tree";-import SearchResult from "../components/search-result";-import { HighlightedResult, Match } from "@notesnook/core";-import GroupHeader from "../components/group-header";+import Search from "./search";function Home() {- const treeRef = useRef<- VirtualizedTreeHandle<{- item: HighlightedResult;- matchIndex?: number;- }>- >(null);const notes = useStore((store) => store.notes);const isCompact = useStore((store) => store.viewMode === "compact");const refresh = useStore((store) => store.refresh);@@ -63,10 +48,6 @@ function Home() {useStore.getState().refresh();}, []);- useEffect(() => {- treeRef.current?.resetAndRefresh();- }, [filteredItems]);-// useEffect(() => {// (async function () {@@ -84,87 +65,7 @@ function Home() {// })();// }, []);- if (filteredItems) {- return (- <>- {filteredItems.length === 0 ? (- <Text- variant="body"- sx={{ color: "paragraph-secondary", mx: 1 }}- data-test-id="list-placeholder"- >- {strings.noResultsFound()}- </Text>- ) : (- <>- <GroupHeader- groupingKey={"search"}- isSearching={true}- refresh={refresh}- title={`${filteredItems.length} results`}- isFocused={false}- index={0}- onSelectGroup={() => {}}- groups={async () => []}- onJump={() => {}}- />- <VirtualizedTree- treeRef={treeRef}- testId="search-results-list"- rootId={"root"}- getChildNodes={async (parent) => {- const nodes: TreeNode<{- item: HighlightedResult;- matchIndex?: number;- }>[] = [];- if (parent.id === "root") {- for (let i = 0; i < filteredItems.length; ++i) {- const result = await filteredItems.item(i);- if (!result.item) continue;- nodes.push({- data: { item: result.item },- depth: parent.depth + 1,- hasChildren: !!result.item.content?.length,- id: result.item.id,- parentId: parent.id,- expanded: true- });- }- } else {- for (- let i = 0;- i < (parent.data.item.content.length || 0);- ++i- ) {- nodes.push({- data: { item: parent.data.item, matchIndex: i },- depth: parent.depth + 1,- parentId: parent.id,- id: parent.id + i,- hasChildren: false- });- }- }- return nodes;- }}- renderItem={({ collapse, expand, expanded, item: node }) => (- <SearchResult- key={node.id}- depth={node.depth}- isExpandable={node.hasChildren}- item={node.data.item}- matchIndex={node.data.matchIndex}- isExpanded={expanded}- collapse={collapse}- expand={expand}- />- )}- />- </>- )}- </>- );- }+ if (filteredItems) return <Search items={filteredItems} refresh={refresh} />;if (!notes) return <ListLoader />;return (diff --git a/apps/web/src/views/notes.tsx b/apps/web/src/views/notes.tsxindex d778f7a2e..a4a1d4b1f 100644--- a/apps/web/src/views/notes.tsx+++ b/apps/web/src/views/notes.tsx@@ -28,6 +28,7 @@ import { db } from "../common/db";import { handleDrop } from "../common/drop-handler";import { useEditorStore } from "../stores/editor-store";import { ListLoader } from "../components/loaders/list-loader";+import Search from "./search";type NotesProps = { header?: JSX.Element };function Notes(props: NotesProps) {@@ -47,21 +48,23 @@ function Notes(props: NotesProps) {async (query, sortOptions) => {if (!context || !contextNotes) return;const notes = notesFromContext(context);- return await db.lookup.notes(query, notes).sorted(sortOptions);+ return await db.lookup.notes(query, sortOptions, notes);},[context, contextNotes]);+ if (filteredItems)+ return <Search items={filteredItems} refresh={refreshContext} />;+if (!context || !contextNotes) return <ListLoader />;return (<ListContainertype={type}group={type}refresh={refreshContext}- isSearching={!!filteredItems}compact={isCompact}context={context}- items={filteredItems || contextNotes}+ items={contextNotes}onDrop={(e) => handleDrop(e.dataTransfer, context)}placeholder={<Placeholderdiff --git a/apps/web/src/views/search.tsx b/apps/web/src/views/search.tsxnew file mode 100644index 000000000..648aa1cd1--- /dev/null+++ b/apps/web/src/views/search.tsx@@ -0,0 +1,129 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++import { HighlightedResult, VirtualizedGrouping } from "@notesnook/core";+import { strings } from "@notesnook/intl";+import { Text } from "@theme-ui/components";+import GroupHeader from "../components/group-header";+import {+ TreeNode,+ VirtualizedTree,+ VirtualizedTreeHandle+} from "../components/virtualized-tree";+import { useEffect, useRef } from "react";+import SearchResult from "../components/search-result";++type NotesProps = {+ items: VirtualizedGrouping<HighlightedResult>;+ refresh: () => void;+};+function Search(props: NotesProps) {+ const { items, refresh } = props;+ const treeRef = useRef<+ VirtualizedTreeHandle<{+ item: HighlightedResult;+ matchIndex?: number;+ }>+ >(null);++ useEffect(() => {+ treeRef.current?.resetAndRefresh();+ }, [items]);++ return (+ <>+ {items.length === 0 ? (+ <Text+ variant="body"+ sx={{ color: "paragraph-secondary", mx: 1 }}+ data-test-id="list-placeholder"+ >+ {strings.noResultsFound()}+ </Text>+ ) : (+ <>+ <GroupHeader+ groupingKey={"search"}+ isSearching={true}+ refresh={refresh}+ title={`${items.length} results`}+ isFocused={false}+ index={0}+ onSelectGroup={() => {}}+ groups={async () => []}+ onJump={() => {}}+ />+ <VirtualizedTree+ treeRef={treeRef}+ testId="search-results-list"+ rootId={"root"}+ getChildNodes={async (parent) => {+ const nodes: TreeNode<{+ item: HighlightedResult;+ matchIndex?: number;+ }>[] = [];+ if (parent.id === "root") {+ for (let i = 0; i < items.length; ++i) {+ const result = await items.item(i);+ if (!result.item) continue;+ nodes.push({+ data: { item: result.item },+ depth: parent.depth + 1,+ hasChildren: !!result.item.content?.length,+ id: result.item.id,+ parentId: parent.id,+ expanded: true+ });+ }+ } else {+ for (+ let i = 0;+ i < (parent.data.item.content.length || 0);+ ++i+ ) {+ nodes.push({+ data: { item: parent.data.item, matchIndex: i },+ depth: parent.depth + 1,+ parentId: parent.id,+ id: parent.id + i,+ hasChildren: false+ });+ }+ }+ return nodes;+ }}+ renderItem={({ collapse, expand, expanded, item: node }) => (+ <SearchResult+ key={node.id}+ depth={node.depth}+ isExpandable={node.hasChildren}+ item={node.data.item}+ matchIndex={node.data.matchIndex}+ isExpanded={expanded}+ collapse={collapse}+ expand={expand}+ />+ )}+ />+ </>+ )}+ </>+ );+}+export default Search;