You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

55 lines
1.5 KiB

import { SEARCHABLE_RELAY_URLS } from '@/constants'
import client from '@/services/client.service'
import { TProfile } from '@/types'
import { useEffect, useState } from 'react'
export function useSearchProfiles(search: string, limit: number) {
const [isFetching, setIsFetching] = useState(true)
const [error, setError] = useState<Error | null>(null)
const [profiles, setProfiles] = useState<TProfile[]>([])
useEffect(() => {
const fetchProfiles = async () => {
if (!search) {
setProfiles([])
return
}
setIsFetching(true)
setProfiles([])
try {
const profiles = await client.searchProfilesFromLocal(search, limit)
setProfiles(profiles)
if (profiles.length >= limit) {
return
}
const existingPubkeys = new Set(profiles.map((profile) => profile.pubkey))
const fetchedProfiles = await client.searchProfiles(
SEARCHABLE_RELAY_URLS,
{
search,
limit
}
)
if (fetchedProfiles.length) {
fetchedProfiles.forEach((profile) => {
if (existingPubkeys.has(profile.pubkey)) {
return
}
existingPubkeys.add(profile.pubkey)
profiles.push(profile)
})
setProfiles([...profiles])
}
} catch (err) {
setError(err as Error)
} finally {
setIsFetching(false)
}
}
fetchProfiles()
}, [search, limit])
return { isFetching, error, profiles }
}