feat: improve search results for profiles
This commit is contained in:
@@ -8,7 +8,7 @@ import MentionList, { MentionListHandle, MentionListProps } from './MentionList'
|
|||||||
|
|
||||||
const suggestion = {
|
const suggestion = {
|
||||||
items: async ({ query }: { query: string }) => {
|
items: async ({ query }: { query: string }) => {
|
||||||
return await client.searchNpubs(query, 20)
|
return await client.searchNpubsFromCache(query, 20)
|
||||||
},
|
},
|
||||||
|
|
||||||
render: () => {
|
render: () => {
|
||||||
|
|||||||
@@ -22,14 +22,27 @@ export function useSearchProfiles(search: string, limit: number) {
|
|||||||
setIsFetching(true)
|
setIsFetching(true)
|
||||||
setProfiles([])
|
setProfiles([])
|
||||||
try {
|
try {
|
||||||
const profiles = await client.searchProfiles(
|
const profiles = await client.searchProfilesFromCache(search, limit)
|
||||||
|
setProfiles(profiles)
|
||||||
|
if (profiles.length >= limit) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const existingPubkeys = new Set(profiles.map((profile) => profile.pubkey))
|
||||||
|
const fetchedProfiles = await client.searchProfiles(
|
||||||
searchableRelayUrls.concat(SEARCHABLE_RELAY_URLS).slice(0, 4),
|
searchableRelayUrls.concat(SEARCHABLE_RELAY_URLS).slice(0, 4),
|
||||||
{
|
{
|
||||||
search,
|
search,
|
||||||
limit
|
limit
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
if (profiles) {
|
if (fetchedProfiles.length) {
|
||||||
|
fetchedProfiles.forEach((profile) => {
|
||||||
|
if (existingPubkeys.has(profile.pubkey)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
existingPubkeys.add(profile.pubkey)
|
||||||
|
profiles.push(profile)
|
||||||
|
})
|
||||||
setProfiles(profiles)
|
setProfiles(profiles)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -833,11 +833,17 @@ class ClientService extends EventTarget {
|
|||||||
this.relayListEventDataLoader.prime(event.pubkey, Promise.resolve(event))
|
this.relayListEventDataLoader.prime(event.pubkey, Promise.resolve(event))
|
||||||
}
|
}
|
||||||
|
|
||||||
async searchNpubs(query: string, limit: number = 100) {
|
async searchNpubsFromCache(query: string, limit: number = 100) {
|
||||||
const result = await this.userIndex.searchAsync(query, { limit })
|
const result = await this.userIndex.searchAsync(query, { limit })
|
||||||
return result.map((pubkey) => pubkeyToNpub(pubkey as string)).filter(Boolean) as string[]
|
return result.map((pubkey) => pubkeyToNpub(pubkey as string)).filter(Boolean) as string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async searchProfilesFromCache(query: string, limit: number = 100) {
|
||||||
|
const npubs = await this.searchNpubsFromCache(query, limit)
|
||||||
|
const profiles = await Promise.all(npubs.map((npub) => this.fetchProfile(npub)))
|
||||||
|
return profiles.filter((profile) => !!profile) as TProfile[]
|
||||||
|
}
|
||||||
|
|
||||||
async initUserIndexFromFollowings(pubkey: string, signal: AbortSignal) {
|
async initUserIndexFromFollowings(pubkey: string, signal: AbortSignal) {
|
||||||
const followings = await this.fetchFollowings(pubkey, true)
|
const followings = await this.fetchFollowings(pubkey, true)
|
||||||
for (let i = 0; i * 20 < followings.length; i++) {
|
for (let i = 0; i * 20 < followings.length; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user