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