Skip to content

Commit

Permalink
proper descending order sort in idb, filter.search
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Aug 25, 2023
1 parent 2755f09 commit 26050c2
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/js/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const Main = () => {
<Search path="/search" focus={true} />
<KeyConverter path="/key" />
<Global path="/global" />
<SearchFeed path="/search/:keyword" />
<SearchFeed path="/search/:query" />
<Login path="/login" fullScreen={true} />
<Notifications path="/notifications" />
<Chat path="/chat/:id?" />
Expand Down
5 changes: 2 additions & 3 deletions src/js/nostr/EventDB.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions src/js/nostr/Filter.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/js/nostr/IndexedDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down
5 changes: 2 additions & 3 deletions src/js/nostr/PubSub.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/js/nostr/Relays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ const Relays = {
filtersBySubscriptionName: new Map<string, string>(),
subscribedEventTags: new Set<string>(),
subscribedProfiles: new Set<string>(),
subscribedKeywords: new Set<string>(), // seach keywords
subscriptionsByName: new Map<string, Set<Sub>>(),
newAuthors: new Set<string>(),
DEFAULT_RELAYS,
Expand Down
3 changes: 1 addition & 2 deletions src/js/nostr/hooks/useSubscribe.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/js/views/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ const Search = (props: any) => {
);
};

export default Search;
export default memo(Search);
2 changes: 1 addition & 1 deletion src/js/views/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const View = ({ children, hideHeader = false, hideSideBar = false }) => {
</div>
<Show when={!hideSideBar}>
<div className="flex-col hidden lg:flex lg:w-1/3">
<Search focus={false} />
<Search key="search" focus={false} />
</div>
</Show>
</div>
Expand Down
12 changes: 6 additions & 6 deletions src/js/views/feeds/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import View from '../View';

type Props = {
path: string;
keyword?: string;
query?: string;
};

const Search: React.FC<Props> = ({ keyword }) => {
const Search: React.FC<Props> = ({ 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 [
Expand All @@ -26,13 +26,13 @@ const Search: React.FC<Props> = ({ keyword }) => {
filterFn,
},
];
}, [keyword]);
}, [query]);

return (
<View>
<div className="flex flex-row">
<div className="flex flex-col w-full">
<FeedComponent key={keyword} filterOptions={filterOptions} />
<FeedComponent key={query} filterOptions={filterOptions} />
</div>
</div>
</View>
Expand Down

0 comments on commit 26050c2

Please sign in to comment.