1- import React , { Component } from 'react'
2- import PropTypes from 'prop-types'
3- import { withTranslation } from 'react-i18next'
4- import { Redirect } from 'react-router-dom'
1+ import React , { KeyboardEventHandler , useContext } from 'react'
2+ import { useTranslation } from 'react-i18next'
3+ import { useHistory } from 'react-router-dom'
4+ import { XrplClient } from 'xrpl-client'
5+
56import {
67 isValidClassicAddress ,
78 isValidXAddress ,
89 classicAddressToXAddress ,
910} from 'ripple-address-codec'
1011import { isValidPayId as isValidPayString } from 'payid-lib'
12+ import SocketContext from '../shared/SocketContext'
1113import {
1214 analytics ,
1315 ANALYTIC_TYPES ,
@@ -19,9 +21,8 @@ import {
1921} from '../shared/utils'
2022import './search.scss'
2123import { getTransaction } from '../../rippled/lib/rippled'
22- import SocketContext from '../shared/SocketContext'
2324
24- const determineHashType = async ( id , rippledContext ) => {
25+ const determineHashType = async ( id : string , rippledContext : XrplClient ) => {
2526 try {
2627 await getTransaction ( rippledContext , id )
2728 return 'transactions'
@@ -30,7 +31,7 @@ const determineHashType = async (id, rippledContext) => {
3031 }
3132}
3233
33- const getIdType = async ( id , rippledContext ) => {
34+ const getIdType = async ( id : string , rippledContext : XrplClient ) => {
3435 if ( DECIMAL_REGEX . test ( id ) ) {
3536 return 'ledgers'
3637 }
@@ -63,7 +64,7 @@ const getIdType = async (id, rippledContext) => {
6364
6465// normalize classicAddress:tag to X-address
6566// TODO: Take network into account (!)
66- const normalize = ( id , type ) => {
67+ const normalize = ( id : string , type : string ) => {
6768 if ( type === 'transactions' ) {
6869 return id . toUpperCase ( )
6970 }
@@ -75,7 +76,7 @@ const normalize = (id, type) => {
7576 components [ 0 ] ,
7677 components [ 1 ] === undefined || components [ 1 ] === 'false'
7778 ? false
78- : components [ 1 ] ,
79+ : Number ( components [ 1 ] ) ,
7980 false ,
8081 ) // TODO: Take network into account (!)
8182 return xAddress
@@ -93,73 +94,47 @@ const normalize = (id, type) => {
9394 return id
9495}
9596
96- class Search extends Component {
97- constructor ( props ) {
98- super ( props )
99- this . state = { redirect : '' }
100- this . onKeyDown = this . onKeyDown . bind ( this )
101- }
97+ export interface SearchProps {
98+ mobile : boolean
99+ callback : Function
100+ }
102101
103- // eslint-disable-next-line camelcase
104- UNSAFE_componentWillReceiveProps ( nextProps ) {
105- this . setState ( { redirect : '' } )
106- }
102+ export const Search = ( {
103+ callback = ( ) => { } ,
104+ mobile = false ,
105+ } : SearchProps ) => {
106+ const { t } = useTranslation ( )
107+ const socket = useContext ( SocketContext )
108+ const history = useHistory ( )
107109
108- async handleSearch ( id ) {
109- const { callback } = this . props
110- const type = await getIdType ( id , this . context )
110+ const handleSearch = async ( id : string ) => {
111+ const type = await getIdType ( id , socket )
111112
112113 analytics ( ANALYTIC_TYPES . event , {
113114 eventCategory : 'globalSearch' ,
114115 eventAction : type ,
115116 eventLabel : id ,
116117 } )
117118
118- this . setState (
119- {
120- redirect :
121- type === 'invalid'
122- ? `/search/${ id } `
123- : `/${ type } /${ normalize ( id , type ) } ` ,
124- } ,
125- callback ,
119+ history . push (
120+ type === 'invalid' ? `/search/${ id } ` : `/${ type } /${ normalize ( id , type ) } ` ,
126121 )
122+ callback ( )
127123 }
128124
129- onKeyDown ( event ) {
125+ const onKeyDown : KeyboardEventHandler < HTMLInputElement > = ( event ) => {
130126 if ( event . key === 'Enter' ) {
131- this . handleSearch ( event . currentTarget . value . trim ( ) )
127+ handleSearch ( event . currentTarget ? .value ? .trim ( ) )
132128 }
133129 }
134130
135- render ( ) {
136- const { t, mobile } = this . props
137- const { redirect } = this . state
138- return redirect ? (
139- < Redirect push to = { redirect } />
140- ) : (
141- < div className = { mobile ? 'search' : 'search in-header' } >
142- < input
143- type = "text"
144- placeholder = { t ( 'header.search.placeholder' ) }
145- onKeyDown = { this . onKeyDown }
146- />
147- </ div >
148- )
149- }
150- }
151-
152- Search . contextType = SocketContext
153-
154- Search . propTypes = {
155- t : PropTypes . func . isRequired ,
156- mobile : PropTypes . bool ,
157- callback : PropTypes . func ,
131+ return (
132+ < div className = { mobile ? 'search' : 'search in-header' } >
133+ < input
134+ type = "text"
135+ placeholder = { t ( 'header.search.placeholder' ) }
136+ onKeyDown = { onKeyDown }
137+ />
138+ </ div >
139+ )
158140}
159-
160- Search . defaultProps = {
161- mobile : false ,
162- callback : ( ) => { } ,
163- }
164-
165- export default withTranslation ( ) ( Search )
0 commit comments