From d6ff039021567284ca49fe2058e1bf2bf07d1afe Mon Sep 17 00:00:00 2001 From: hitchhooker Date: Sat, 23 Nov 2024 19:42:17 +0700 Subject: [PATCH] add basic validator identity search adds minimal identity search support to validator search. skips queries until 3+ chars and limits results to 20 validators for efficiency. preserves existing address validation as fallback with no extra overhead when searching by address. --- src/components/SearchSmall.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/components/SearchSmall.js b/src/components/SearchSmall.js index f38409c..5086d0e 100644 --- a/src/components/SearchSmall.js +++ b/src/components/SearchSmall.js @@ -16,6 +16,7 @@ import { import { pageChanged } from '../features/layout/layoutSlice'; +import { useGetValidatorsQuery } from '../features/api/validatorsSlice'; export default function SearchSmall({width = 512}) { // const theme = useTheme(); @@ -25,6 +26,13 @@ export default function SearchSmall({width = 512}) { const currentSelected = useSelector(selectAddress); const [address, setAddress] = React.useState(""); let [searchParams, setSearchParams] = useSearchParams(); + + const { data: validators } = useGetValidatorsQuery({ + show_profile: true, + size: 20 + }, { + skip: !address || address.length < 3 + }); const handleChange = (event) => { setAddress(event.target.value); @@ -32,7 +40,17 @@ export default function SearchSmall({width = 512}) { const handleSubmit = (event) => { event.preventDefault() - if (isValidAddress(address)) { + const validatorMatch = validators?.data?.find(v => + v.identity?.display?.toLowerCase() === address.toLowerCase() + ); + + if (validatorMatch) { + const defaultSS58 = addressSS58(validatorMatch.address); + dispatch(addressChanged(defaultSS58)); + dispatch(pageChanged(`validator/${defaultSS58}`)); + navigate(`/validator/${validatorMatch.address}`) + setAddress(""); + } else if (isValidAddress(address)) { const defaultSS58 = addressSS58(address) dispatch(addressChanged(defaultSS58)); dispatch(pageChanged(`validator/${defaultSS58}`)); @@ -52,7 +70,7 @@ export default function SearchSmall({width = 512}) { width, }} variant="outlined" - placeholder="Search by validator stash address" + placeholder="Search by identity or stash address" color="primary" value={address} onChange={handleChange} @@ -84,4 +102,4 @@ export default function SearchSmall({width = 512}) { /> ) -} \ No newline at end of file +}