feat: local releay search
This commit is contained in:
46
packages/app/src/Components/Feed/LocalSearch.tsx
Normal file
46
packages/app/src/Components/Feed/LocalSearch.tsx
Normal file
@ -0,0 +1,46 @@
|
||||
import { EventKind, RequestBuilder, TaggedNostrEvent } from "@snort/system";
|
||||
import { useRequestBuilder } from "@snort/system-react";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
|
||||
import { Relay } from "@/Cache";
|
||||
import { SearchRelays } from "@/Utils/Const";
|
||||
|
||||
import PageSpinner from "../PageSpinner";
|
||||
import { TimelineFragment } from "./TimelineFragment";
|
||||
|
||||
export function LocalSearch({ term, kind }: { term: string; kind: EventKind }) {
|
||||
const [frag, setFrag] = useState<TimelineFragment>();
|
||||
|
||||
const r = useMemo(() => {
|
||||
const rb = new RequestBuilder("search");
|
||||
rb.withFilter().search(term).kinds([kind]).relay(SearchRelays).limit(100);
|
||||
return rb;
|
||||
}, [term]);
|
||||
useRequestBuilder(r);
|
||||
|
||||
useEffect(() => {
|
||||
setFrag(undefined);
|
||||
if (term) {
|
||||
Relay.req({
|
||||
id: "local-search",
|
||||
filters: [
|
||||
{
|
||||
kinds: [kind],
|
||||
limit: 100,
|
||||
search: term,
|
||||
},
|
||||
],
|
||||
}).then(res => {
|
||||
setFrag({
|
||||
refTime: 0,
|
||||
events: res.result as Array<TaggedNostrEvent>,
|
||||
});
|
||||
});
|
||||
}
|
||||
}, [term, kind]);
|
||||
|
||||
if (frag) {
|
||||
return <TimelineFragment frag={frag} />;
|
||||
}
|
||||
return <PageSpinner />;
|
||||
}
|
@ -1,33 +1,9 @@
|
||||
import { unixNow } from "@snort/shared";
|
||||
import { socialGraphInstance } from "@snort/system";
|
||||
import { useMemo } from "react";
|
||||
|
||||
import fuzzySearch from "@/Db/FuzzySearch";
|
||||
import useTimelineFeed, { TimelineFeedOptions, TimelineSubject } from "@/Feed/TimelineFeed";
|
||||
|
||||
export default function useProfileSearch(search: string) {
|
||||
const options: TimelineFeedOptions = useMemo(
|
||||
() => ({
|
||||
method: "LIMIT_UNTIL",
|
||||
window: undefined,
|
||||
now: unixNow(),
|
||||
}),
|
||||
[],
|
||||
);
|
||||
|
||||
const subject: TimelineSubject = useMemo(
|
||||
() => ({
|
||||
type: "profile_keyword",
|
||||
discriminator: search,
|
||||
items: search ? [search] : [],
|
||||
relay: undefined,
|
||||
streams: false,
|
||||
}),
|
||||
[search],
|
||||
);
|
||||
|
||||
const { main } = useTimelineFeed(subject, options);
|
||||
|
||||
const results = useMemo(() => {
|
||||
const searchString = search.trim();
|
||||
const fuseResults = fuzzySearch.search(searchString);
|
||||
@ -66,7 +42,7 @@ export default function useProfileSearch(search: string) {
|
||||
});
|
||||
|
||||
return combinedResults.map(r => r.item);
|
||||
}, [search, main]);
|
||||
}, [search]);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
@ -1,25 +1,15 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { FormattedMessage, useIntl } from "react-intl";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
|
||||
import Timeline from "@/Components/Feed/Timeline";
|
||||
import { LocalSearch } from "@/Components/Feed/LocalSearch";
|
||||
import TabSelectors, { Tab } from "@/Components/TabSelectors/TabSelectors";
|
||||
import TrendingNotes from "@/Components/Trending/TrendingPosts";
|
||||
import TrendingUsers from "@/Components/Trending/TrendingUsers";
|
||||
import FollowListBase from "@/Components/User/FollowListBase";
|
||||
import useProfileSearch from "@/Hooks/useProfileSearch";
|
||||
import { debounce } from "@/Utils";
|
||||
|
||||
const NOTES = 0;
|
||||
const PROFILES = 1;
|
||||
|
||||
const Profiles = ({ keyword }: { keyword: string }) => {
|
||||
const results = useProfileSearch(keyword);
|
||||
const ids = useMemo(() => results.map(r => r.pubkey), [results]);
|
||||
const content = keyword ? <FollowListBase pubkeys={ids} showAbout={true} /> : <TrendingUsers />;
|
||||
return <div className="px-3">{content}</div>;
|
||||
};
|
||||
|
||||
const SearchPage = () => {
|
||||
const params = useParams();
|
||||
const { formatMessage } = useIntl();
|
||||
@ -53,18 +43,9 @@ const SearchPage = () => {
|
||||
return debounce(500, () => setKeyword(search));
|
||||
}, [search]);
|
||||
|
||||
const subject = useMemo(
|
||||
() => ({
|
||||
type: "post_keyword",
|
||||
items: [keyword + (sortPopular ? " sort:popular" : "")],
|
||||
discriminator: keyword,
|
||||
}),
|
||||
[keyword, sortPopular],
|
||||
);
|
||||
|
||||
function tabContent() {
|
||||
if (tab.value === PROFILES) {
|
||||
return <Profiles keyword={search} />;
|
||||
return <LocalSearch term={keyword} kind={0} />;
|
||||
}
|
||||
|
||||
if (!keyword) {
|
||||
@ -74,7 +55,7 @@ const SearchPage = () => {
|
||||
return (
|
||||
<>
|
||||
{sortOptions()}
|
||||
<Timeline key={keyword} subject={subject} postsOnly={false} method={"LIMIT_UNTIL"} loadMore={false} />
|
||||
<LocalSearch term={keyword} kind={1} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user