fix(search): tim input, add timeout
All checks were successful
Open PR on Staging / audit_and_check (pull_request) Successful in 54s
All checks were successful
Open PR on Staging / audit_and_check (pull_request) Successful in 54s
Fixes #308
This commit is contained in:
parent
0834e52316
commit
f7d0718b78
@ -45,7 +45,8 @@ import {
|
||||
uploadToFileStorage,
|
||||
DEFAULT_TOOLBOX,
|
||||
settleAllFullfilfedPromises,
|
||||
uploadMetaToFileStorage
|
||||
uploadMetaToFileStorage,
|
||||
timeout
|
||||
} from '../../utils'
|
||||
import { Container } from '../../components/Container'
|
||||
import fileListStyles from '../../components/FileList/style.module.scss'
|
||||
@ -79,6 +80,7 @@ import { useNDKContext } from '../../hooks/useNDKContext.ts'
|
||||
import { useNDK } from '../../hooks/useNDK.ts'
|
||||
import { useImmer } from 'use-immer'
|
||||
import { ButtonUnderline } from '../../components/ButtonUnderline/index.tsx'
|
||||
import { TimeoutError } from '../../types/errors/TimeoutError.ts'
|
||||
|
||||
type FoundUser = NostrEvent & { npub: string }
|
||||
|
||||
@ -162,8 +164,8 @@ export const CreatePage = () => {
|
||||
return pubkey
|
||||
}
|
||||
|
||||
const handleSearchUsers = async (searchValue?: string) => {
|
||||
const searchString = searchValue || userSearchInput || undefined
|
||||
const handleSearchUsers = async () => {
|
||||
const searchString = userSearchInput || undefined
|
||||
|
||||
if (!searchString) return
|
||||
|
||||
@ -171,14 +173,17 @@ export const CreatePage = () => {
|
||||
|
||||
const searchTerm = searchString.trim()
|
||||
|
||||
fetchEventsFromUserRelays(
|
||||
{
|
||||
kinds: [0],
|
||||
search: searchTerm
|
||||
},
|
||||
usersPubkey,
|
||||
UserRelaysType.Write
|
||||
)
|
||||
Promise.race([
|
||||
fetchEventsFromUserRelays(
|
||||
{
|
||||
kinds: [0],
|
||||
search: searchTerm
|
||||
},
|
||||
usersPubkey,
|
||||
UserRelaysType.Write
|
||||
),
|
||||
timeout(30000)
|
||||
])
|
||||
.then((events) => {
|
||||
const nostrEvents = events.map((event) => event.rawEvent())
|
||||
|
||||
@ -216,6 +221,9 @@ export const CreatePage = () => {
|
||||
toast.info('No user found with the provided search term')
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof TimeoutError) {
|
||||
toast.error('Search timed out. Please try again.')
|
||||
}
|
||||
console.error(error)
|
||||
})
|
||||
.finally(() => {
|
||||
@ -245,22 +253,23 @@ export const CreatePage = () => {
|
||||
|
||||
// If pasted user npub of nip05 is present, we just add the user to the counterparts list
|
||||
if (pastedUserNpubOrNip05) {
|
||||
setUserInput(pastedUserNpubOrNip05)
|
||||
setUserInput(pastedUserNpubOrNip05.trim())
|
||||
setPastedUserNpubOrNip05(undefined)
|
||||
} else {
|
||||
// Otherwize if search already provided some results, user must manually click the search button
|
||||
// Otherwise if search already provided some results, user must manually click the search button
|
||||
if (!foundUsers.length) {
|
||||
const searchTerm = userSearchInput.trim()
|
||||
// If it's NIP05 (includes @ or is a valid domain) send request to .well-known
|
||||
const domainRegex = /^[a-zA-Z0-9@.-]+\.[a-zA-Z]{2,}$/
|
||||
if (domainRegex.test(userSearchInput)) {
|
||||
if (searchTerm.startsWith('_@') || domainRegex.test(searchTerm)) {
|
||||
setSearchUsersLoading(true)
|
||||
|
||||
const pubkey = await handleSearchUserNip05(userSearchInput)
|
||||
const pubkey = await handleSearchUserNip05(searchTerm)
|
||||
|
||||
setSearchUsersLoading(false)
|
||||
|
||||
if (pubkey) {
|
||||
setUserInput(userSearchInput)
|
||||
setUserInput(searchTerm)
|
||||
} else {
|
||||
toast.error(`No user found with the NIP05: ${userSearchInput}`)
|
||||
}
|
||||
@ -411,7 +420,7 @@ export const CreatePage = () => {
|
||||
|
||||
setUserSearchInput('')
|
||||
|
||||
if (input.startsWith('npub')) {
|
||||
if (input.startsWith('npub1')) {
|
||||
return handleAddNpubUser(input)
|
||||
}
|
||||
|
||||
@ -1034,17 +1043,8 @@ export const CreatePage = () => {
|
||||
}
|
||||
|
||||
// Seems like it's npub format
|
||||
if (value.startsWith('npub')) {
|
||||
// We will try to convert npub to hex and if it's successfull that means
|
||||
// npub is valid
|
||||
const validHexPubkey = npubToHex(value)
|
||||
|
||||
if (validHexPubkey) {
|
||||
// Arm the manual user npub add after enter is hit, we don't want to trigger search
|
||||
setPastedUserNpubOrNip05(value)
|
||||
} else {
|
||||
disarmAddOnEnter()
|
||||
}
|
||||
if (value.trim().startsWith('npub1')) {
|
||||
setPastedUserNpubOrNip05(value.trim())
|
||||
} else {
|
||||
// Disarm the add user on enter hit, and trigger search after 1 second
|
||||
disarmAddOnEnter()
|
||||
@ -1204,7 +1204,7 @@ export const CreatePage = () => {
|
||||
{!pastedUserNpubOrNip05 ? (
|
||||
<Button
|
||||
disabled={!userSearchInput || searchUsersLoading}
|
||||
onClick={() => handleSearchUsers()}
|
||||
onClick={handleSearchUsers}
|
||||
variant="contained"
|
||||
aria-label="Add"
|
||||
className={styles.counterpartToggleButton}
|
||||
@ -1218,7 +1218,7 @@ export const CreatePage = () => {
|
||||
) : (
|
||||
<Button
|
||||
onClick={() => {
|
||||
setUserInput(userSearchInput)
|
||||
setUserInput(userSearchInput.trim())
|
||||
}}
|
||||
variant="contained"
|
||||
aria-label="Add"
|
||||
|
Loading…
x
Reference in New Issue
Block a user