From f7d0718b7820ebc66310f572b0ceb6e91f5393bd Mon Sep 17 00:00:00 2001 From: en Date: Fri, 7 Feb 2025 19:14:25 +0100 Subject: [PATCH 1/2] fix(search): tim input, add timeout Fixes #308 --- src/pages/create/index.tsx | 60 +++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/pages/create/index.tsx b/src/pages/create/index.tsx index d5424a9..2d476ae 100644 --- a/src/pages/create/index.tsx +++ b/src/pages/create/index.tsx @@ -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 ? (