11import React , { useState } from 'react' ;
22import { Search } from 'lucide-react' ;
33import { useNavigate } from 'react-router-dom' ;
4- import { isBitcoinAddress , isTransactionId , isBlockHash , isBlockHeight } from '../utils/validation' ;
4+ import { isBitcoinAddress , isTransactionId , isBlockHash , isBlockHeight } from '../utils/validation' ; // Make sure to import the updated validation functions
55import { Notification } from './Notification' ;
66
77export const SearchBar : React . FC = ( ) => {
@@ -15,72 +15,72 @@ export const SearchBar: React.FC = () => {
1515 return 'Please enter a search term' ;
1616 }
1717
18+ const trimmedInput = input . trim ( ) ; // Trim input once to avoid repetition
19+
20+ // Check if it's a Bitcoin address (FIRST, because addresses are shorter and have specific prefixes)
21+ if ( isBitcoinAddress ( trimmedInput ) ) {
22+ return null ; // Valid Bitcoin address
23+ }
24+
1825 // Check if it's a block height (numeric)
19- if ( / ^ \d + $ / . test ( input ) ) {
20- if ( ! isBlockHeight ( parseInt ( input , 10 ) ) ) {
26+ if ( / ^ \d + $ / . test ( trimmedInput ) ) {
27+ if ( ! isBlockHeight ( parseInt ( trimmedInput , 10 ) ) ) {
2128 return 'Invalid block height' ;
2229 }
23- return null ;
30+ return null ; // Valid block height
2431 }
2532
26- // Check if it's a block hash (64 hex chars)
27- if ( input . length === 64 && / ^ [ a - f A - F 0 - 9 ] + $ / . test ( input ) ) {
28- if ( ! isBlockHash ( input ) ) {
29- return 'Invalid block hash format' ;
33+ // Check if it's a block hash (64 hex chars and starts with zeros - heuristic )
34+ if ( trimmedInput . length === 64 && / ^ [ a - f A - F 0 - 9 ] + $ / . test ( trimmedInput ) ) {
35+ if ( isBlockHash ( trimmedInput ) ) { // Use the updated isBlockHash function
36+ return null ; // Valid Block Hash (heuristic)
3037 }
31- return null ;
32- }
33-
34- // Check if it's a transaction ID (64 hex chars)
35- if ( input . length === 64 ) {
36- if ( ! isTransactionId ( input ) ) {
37- return 'Invalid transaction ID format' ;
38+ // If not likely a block hash, check if it's a Transaction ID
39+ if ( isTransactionId ( trimmedInput ) ) { // Use the updated isTransactionId function
40+ return null ; // Valid Transaction ID (heuristic)
3841 }
39- return null ;
42+ return 'Invalid Block Hash or Transaction ID format' ; // Neither Block Hash nor Transaction ID (heuristic)
4043 }
4144
42- // Check if it's a Bitcoin address
43- if ( ! isBitcoinAddress ( input ) ) {
44- return 'Invalid Bitcoin address format' ;
45- }
4645
47- return null ;
46+ // If none of the above, and not a valid address in the first check, then it's an invalid address format
47+ return 'Invalid Bitcoin address format' ; // Fallback to invalid address format if not caught earlier
4848 } ;
4949
5050 const handleSearch = ( e : React . FormEvent ) => {
5151 e . preventDefault ( ) ;
52-
52+
5353 const trimmedQuery = query . trim ( ) ;
5454 const validationError = validateInput ( trimmedQuery ) ;
55-
55+
5656 if ( validationError ) {
5757 setError ( validationError ) ;
5858 setShowNotification ( true ) ;
5959 return ;
6060 }
6161
62- // Check if it's a block height (numeric)
62+ // Check if it's a block height (numeric) - Order is important, check block height first for numbers
6363 if ( / ^ \d + $ / . test ( trimmedQuery ) ) {
6464 navigate ( `/block/${ trimmedQuery } ` ) ;
6565 setQuery ( '' ) ;
6666 return ;
6767 }
6868
69- // Check if it's a block hash (64 hex chars)
70- if ( trimmedQuery . length === 64 && isBlockHash ( trimmedQuery ) ) {
69+ // Check if it's a block hash (64 hex chars and starts with zeros - heuristic )
70+ if ( trimmedQuery . length === 64 && isBlockHash ( trimmedQuery ) ) { // Use updated isBlockHash
7171 navigate ( `/block/${ trimmedQuery } ` ) ;
7272 setQuery ( '' ) ;
7373 return ;
7474 }
7575
76- // Check if it's a transaction ID (64 hex chars)
77- if ( trimmedQuery . length === 64 ) {
76+ // Check if it's a transaction ID (64 hex chars and NOT starting with many zeros - heuristic )
77+ if ( trimmedQuery . length === 64 && isTransactionId ( trimmedQuery ) ) { // Use updated isTransactionId
7878 navigate ( `/tx/${ trimmedQuery } ` ) ;
7979 setQuery ( '' ) ;
8080 return ;
8181 }
8282
83- // Must be a Bitcoin address
83+ // Must be a Bitcoin address (if it passed validation and is not block height/hash/txid)
8484 navigate ( `/address/${ trimmedQuery } ` ) ;
8585 setQuery ( '' ) ;
8686 } ;
0 commit comments