From 3ecda367f8efbb3d14d0213c47985de202c0ab05 Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Tue, 26 Dec 2023 15:17:56 +0500 Subject: [PATCH] mobile: fix search to use filtered selector for notes --- apps/mobile/app/screens/favorites/index.tsx | 5 ++--- apps/mobile/app/screens/notebook/index.tsx | 11 ++++++---- apps/mobile/app/screens/notes/index.tsx | 11 +++++++--- apps/mobile/app/screens/search/index.tsx | 20 +++++++++---------- .../mobile/app/stores/use-navigation-store.ts | 5 ++++- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/apps/mobile/app/screens/favorites/index.tsx b/apps/mobile/app/screens/favorites/index.tsx index b1895de30..3128687ad 100644 --- a/apps/mobile/app/screens/favorites/index.tsx +++ b/apps/mobile/app/screens/favorites/index.tsx @@ -27,6 +27,7 @@ import Navigation, { NavigationProps } from "../../services/navigation"; import SettingsService from "../../services/settings"; import { useFavorites } from "../../stores/use-favorite-store"; import useNavigationStore from "../../stores/use-navigation-store"; +import { db } from "../../common/database"; export const Favorites = ({ navigation, @@ -61,9 +62,7 @@ export const Favorites = ({ type: "note", title: route.name, route: route.name, - ids: favorites?.ids.filter( - (id) => typeof id === "string" - ) as string[] + items: db.notes.favorites }); }} /> diff --git a/apps/mobile/app/screens/notebook/index.tsx b/apps/mobile/app/screens/notebook/index.tsx index ed8ef7dbf..0f59588ee 100644 --- a/apps/mobile/app/screens/notebook/index.tsx +++ b/apps/mobile/app/screens/notebook/index.tsx @@ -149,12 +149,17 @@ const NotebookScreen = ({ route, navigation }: NavigationProps<"Notebook">) => { canGoBack={params?.current?.canGoBack} hasSearch={true} onSearch={() => { + const selector = db.relations.from( + params.current.item, + "note" + ).selector; + Navigation.push("Search", { placeholder: `Type a keyword to search in ${params.current.item?.title}`, type: "note", title: params.current.item?.title, route: route.name, - ids: notes?.ids.filter((id) => typeof id === "string") as string[] + items: selector }); }} titleHiddenOnRender @@ -230,9 +235,7 @@ const NotebookScreen = ({ route, navigation }: NavigationProps<"Notebook">) => { AddNotebookSheet.present(params.current.item); }} notebook={params.current.item} - totalNotes={ - notes?.ids.filter((id) => typeof id === "string")?.length || 0 - } + totalNotes={notes?.ids.length || 0} /> } placeholder={{ diff --git a/apps/mobile/app/screens/notes/index.tsx b/apps/mobile/app/screens/notes/index.tsx index 53b109f15..d0ae93e3f 100644 --- a/apps/mobile/app/screens/notes/index.tsx +++ b/apps/mobile/app/screens/notes/index.tsx @@ -31,14 +31,14 @@ import { eUnSubscribeEvent } from "../../services/event-manager"; import Navigation, { NavigationProps } from "../../services/navigation"; +import { resolveItems } from "../../stores/resolve-items"; import useNavigationStore, { HeaderRightButton, NotesScreenParams, RouteName } from "../../stores/use-navigation-store"; -import { useNoteStore } from "../../stores/use-notes-store"; import { setOnFirstSave } from "./common"; -import { resolveItems } from "../../stores/resolve-items"; +import { db } from "../../common/database"; export const WARNING_DATA = { title: "Some notes in this topic are not synced" }; @@ -172,12 +172,17 @@ const NotesPage = ({ route.name === "Monographs" ? "Monographs" : params?.current.item?.id } onSearch={() => { + const selector = + route.name === "Monographs" + ? db.monographs.all + : db.relations.from(params.current.item, "note").selector; + Navigation.push("Search", { placeholder: `Type a keyword to search in ${title}`, type: "note", title: title, route: route.name, - ids: notes?.ids?.filter((id) => typeof id === "string") as string[] + items: selector }); }} accentColor={accentColor} diff --git a/apps/mobile/app/screens/search/index.tsx b/apps/mobile/app/screens/search/index.tsx index d8c19db49..3e6d7ab91 100644 --- a/apps/mobile/app/screens/search/index.tsx +++ b/apps/mobile/app/screens/search/index.tsx @@ -17,20 +17,21 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -import { Item, VirtualizedGrouping } from "@notesnook/core"; +import { Item, Note, VirtualizedGrouping } from "@notesnook/core"; import React, { useEffect, useRef, useState } from "react"; import { db } from "../../common/database"; import List from "../../components/list"; -import { NavigationProps } from "../../services/navigation"; -import { SearchBar } from "./search-bar"; +import SelectionHeader from "../../components/selection-header"; import { useNavigationFocus } from "../../hooks/use-navigation-focus"; -import useNavigationStore from "../../stores/use-navigation-store"; import { eSubscribeEvent, eUnSubscribeEvent } from "../../services/event-manager"; +import { NavigationProps } from "../../services/navigation"; +import useNavigationStore from "../../stores/use-navigation-store"; import { eOnRefreshSearch } from "../../utils/events"; -import SelectionHeader from "../../components/selection-header"; +import { SearchBar } from "./search-bar"; +import { FilteredSelector } from "@notesnook/core/dist/database/sql-collection"; export const Search = ({ route, navigation }: NavigationProps<"Search">) => { const [results, setResults] = useState>(); const [loading, setLoading] = useState(false); @@ -59,13 +60,10 @@ export const Search = ({ route, navigation }: NavigationProps<"Search">) => { route.params.type === "trash" ? "trash" : ((route.params?.type + "s") as keyof typeof db.lookup); - console.log( - `Searching in ${type} for ${query}`, - route.params?.ids?.length - ); + console.log(`Searching in ${type} for ${query}`); const results = await db.lookup[type]( query, - route.params?.type === "note" ? route.params?.ids : undefined + route.params.items as FilteredSelector ).sorted(); console.log(`Found ${results.ids?.length} results for ${query}`); setResults(results); @@ -79,7 +77,7 @@ export const Search = ({ route, navigation }: NavigationProps<"Search">) => { console.log(e); } }, - [route.params?.ids, route.params.type] + [route.params?.items, route.params.type] ); useEffect(() => { diff --git a/apps/mobile/app/stores/use-navigation-store.ts b/apps/mobile/app/stores/use-navigation-store.ts index fa2b57635..ff3ebe479 100644 --- a/apps/mobile/app/stores/use-navigation-store.ts +++ b/apps/mobile/app/stores/use-navigation-store.ts @@ -19,6 +19,7 @@ along with this program. If not, see . import { Color, + Item, ItemType, Note, Notebook, @@ -28,6 +29,8 @@ import { } from "@notesnook/core/dist/types"; import create, { State } from "zustand"; import { ColorValues } from "../utils/colors"; +import { FilteredSelector } from "@notesnook/core/dist/database/sql-collection"; +import { VirtualizedGrouping } from "@notesnook/core"; export type GenericRouteParam = undefined; @@ -69,7 +72,7 @@ export type RouteParams = { type: ItemType; title: string; route: RouteName; - ids?: string[]; + items?: FilteredSelector; }; Settings: GenericRouteParam; TaggedNotes: NotesScreenParams;