From 26050c20b8a956c862358561918840b27d0ba99c Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Fri, 25 Aug 2023 13:41:44 +0300 Subject: [PATCH] proper descending order sort in idb, filter.search --- src/js/Main.tsx | 2 +- src/js/nostr/EventDB.ts | 5 ++--- src/js/nostr/Filter.ts | 6 ------ src/js/nostr/IndexedDB.ts | 2 +- src/js/nostr/PubSub.ts | 5 ++--- src/js/nostr/Relays.ts | 1 - src/js/nostr/hooks/useSubscribe.ts | 3 +-- src/js/views/Search.tsx | 2 +- src/js/views/View.tsx | 2 +- src/js/views/feeds/Search.tsx | 12 ++++++------ 10 files changed, 15 insertions(+), 25 deletions(-) delete mode 100644 src/js/nostr/Filter.ts diff --git a/src/js/Main.tsx b/src/js/Main.tsx index c025abbd9..d7b6d9b10 100644 --- a/src/js/Main.tsx +++ b/src/js/Main.tsx @@ -98,7 +98,7 @@ const Main = () => { - + diff --git a/src/js/nostr/EventDB.ts b/src/js/nostr/EventDB.ts index 098ae516a..28086e041 100644 --- a/src/js/nostr/EventDB.ts +++ b/src/js/nostr/EventDB.ts @@ -1,7 +1,6 @@ import loki from 'lokijs'; -import { Event } from 'nostr-tools'; +import { Event, Filter } from 'nostr-tools'; -import Filter from '@/nostr/Filter'; import { ID, STR } from '@/utils/UniqueIds'; export class EventDB { @@ -98,7 +97,7 @@ export class EventDB { .chain() .find(query) .where((e: Event) => { - if (filter.keywords && !filter.keywords.some((keyword) => e.content?.includes(keyword))) { + if (filter.search && !e.content?.includes(filter.search)) { return false; } return true; diff --git a/src/js/nostr/Filter.ts b/src/js/nostr/Filter.ts deleted file mode 100644 index 05579f08d..000000000 --- a/src/js/nostr/Filter.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Filter as NostrToolsFilter } from 'nostr-tools'; - -type Filter = NostrToolsFilter & { - keywords?: string[]; -}; -export default Filter; diff --git a/src/js/nostr/IndexedDB.ts b/src/js/nostr/IndexedDB.ts index 3fd222310..f751396d5 100644 --- a/src/js/nostr/IndexedDB.ts +++ b/src/js/nostr/IndexedDB.ts @@ -184,7 +184,7 @@ const IndexedDB = { query = query.limit(filter.limit); } // TODO test that the sort is actually working - await query.sortBy('created_at').reverse().each(handleEvent); + await query.sortBy('-created_at').each(handleEvent); }, }; diff --git a/src/js/nostr/PubSub.ts b/src/js/nostr/PubSub.ts index 26205183c..1195c5ff3 100644 --- a/src/js/nostr/PubSub.ts +++ b/src/js/nostr/PubSub.ts @@ -1,8 +1,7 @@ import throttle from 'lodash/throttle'; -import { Event, matchFilter } from 'nostr-tools'; +import { Event, Filter, matchFilter } from 'nostr-tools'; import EventDB from '@/nostr/EventDB'; -import Filter from '@/nostr/Filter'; import getRelayPool from '@/nostr/relayPool'; import Events from '../nostr/Events'; @@ -92,7 +91,7 @@ const PubSub = { subscribeRelayPool(filter: Filter, sinceLastOpened: boolean, mergeSubscriptions: boolean) { let relays; - if (filter.keywords) { + if (filter.search) { relays = Array.from(Relays.searchRelays.keys()); } else if (mergeSubscriptions || filter.authors?.length !== 1) { relays = Relays.enabledRelays(); diff --git a/src/js/nostr/Relays.ts b/src/js/nostr/Relays.ts index e6750942f..531abccf7 100644 --- a/src/js/nostr/Relays.ts +++ b/src/js/nostr/Relays.ts @@ -66,7 +66,6 @@ const Relays = { filtersBySubscriptionName: new Map(), subscribedEventTags: new Set(), subscribedProfiles: new Set(), - subscribedKeywords: new Set(), // seach keywords subscriptionsByName: new Map>(), newAuthors: new Set(), DEFAULT_RELAYS, diff --git a/src/js/nostr/hooks/useSubscribe.ts b/src/js/nostr/hooks/useSubscribe.ts index dbe72a274..cc4fa97c5 100644 --- a/src/js/nostr/hooks/useSubscribe.ts +++ b/src/js/nostr/hooks/useSubscribe.ts @@ -1,9 +1,8 @@ import { useCallback, useEffect, useRef, useState } from 'react'; import throttle from 'lodash/throttle'; -import { Event } from 'nostr-tools'; +import { Event, Filter } from 'nostr-tools'; import EventDB from '@/nostr/EventDB.ts'; -import Filter from '@/nostr/Filter.ts'; import PubSub from '@/nostr/PubSub.ts'; interface SubscribeOptions { diff --git a/src/js/views/Search.tsx b/src/js/views/Search.tsx index 284d7c574..5fb0b2483 100644 --- a/src/js/views/Search.tsx +++ b/src/js/views/Search.tsx @@ -96,4 +96,4 @@ const Search = (props: any) => { ); }; -export default Search; +export default memo(Search); diff --git a/src/js/views/View.tsx b/src/js/views/View.tsx index 14fc4b32d..b517fa36b 100644 --- a/src/js/views/View.tsx +++ b/src/js/views/View.tsx @@ -83,7 +83,7 @@ const View = ({ children, hideHeader = false, hideSideBar = false }) => {
- +
diff --git a/src/js/views/feeds/Search.tsx b/src/js/views/feeds/Search.tsx index efe0699ed..d89046dd8 100644 --- a/src/js/views/feeds/Search.tsx +++ b/src/js/views/feeds/Search.tsx @@ -7,16 +7,16 @@ import View from '../View'; type Props = { path: string; - keyword?: string; + query?: string; }; -const Search: React.FC = ({ keyword }) => { +const Search: React.FC = ({ query }) => { const filterOptions = useMemo(() => { - const filter = { kinds: [1], keywords: [keyword || ''] }; + const filter = { kinds: [1], search: query }; const filterFn = (event) => { // some relays don't support filtering by keyword - return event.content.includes(keyword); + return event.content.includes(query); }; return [ @@ -26,13 +26,13 @@ const Search: React.FC = ({ keyword }) => { filterFn, }, ]; - }, [keyword]); + }, [query]); return (
- +