Skip to content

Commit

Permalink
Merge pull request #1587 from oasisprotocol/lw/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
lukaw3d authored Oct 24, 2024
2 parents 1022072 + 004ee4e commit aa6e673
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 30 deletions.
1 change: 1 addition & 0 deletions .changelog/1587.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cleanup
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const config = {
parser: '@typescript-eslint/parser',

settings: {},
reportUnusedDisableDirectives: true,

rules: {
'no-restricted-imports': [
'error',
Expand Down
1 change: 0 additions & 1 deletion internals/jest/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import 'dotenv/config'
import 'react-app-polyfill/stable'

Expand Down
21 changes: 7 additions & 14 deletions src/app/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { SearchSuggestionsButtons } from './SearchSuggestionsButtons'
import { formHelperTextClasses } from '@mui/material/FormHelperText'
import { outlinedInputClasses } from '@mui/material/OutlinedInput'
import { SearchScope } from '../../../types/searchScope'
import { textSearchMininumLength } from './search-utils'
import { textSearchMinimumLength } from './search-utils'
import Typography from '@mui/material/Typography'
import { isValidBlockHeight } from '../../utils/helpers'
import { typingDelay } from '../../../styles/theme'
Expand Down Expand Up @@ -115,20 +115,13 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
const valueInSearchParams = useSearchParams()[0].get('q') ?? ''
const [isProblemFresh, setIsProblemFresh] = useState(false)

const wordsOfPower = t('search.wordsOfPower')
const hasWordsOfPower = value.trim().toLowerCase().startsWith(wordsOfPower.toLowerCase())
const valueWithoutPrefix = hasWordsOfPower
? value.trim().substring(wordsOfPower.length).trim()
: value.trim()
const trimmedValue = value.trim()

const isTooShort =
!!value && valueWithoutPrefix.length < textSearchMininumLength && !isValidBlockHeight(valueWithoutPrefix)

const hasPrivacyProblem = !hasWordsOfPower && isValidMnemonic(valueWithoutPrefix)
!!value && trimmedValue.length < textSearchMinimumLength && !isValidBlockHeight(trimmedValue)

useEffect(() => {
setValue(hasWordsOfPower ? `${wordsOfPower} ${valueInSearchParams}` : valueInSearchParams)
// eslint-disable-next-line react-hooks/exhaustive-deps
setValue(valueInSearchParams)
}, [valueInSearchParams]) // We only want to update the value from code when the URL changes

const onChange = (newValue: string) => {
Expand All @@ -143,7 +136,7 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
const onFormSubmit = (e?: FormEvent) => {
e?.preventDefault()
if (value && value !== valueInSearchParams) {
navigate(RouteUtils.getSearchRoute(scope, valueWithoutPrefix))
navigate(RouteUtils.getSearchRoute(scope, trimmedValue))
}
}

Expand All @@ -166,8 +159,8 @@ const SearchCmp: FC<SearchProps> = ({ scope, variant, disabled, onFocusChange: o
const errorMessage = isTooShort ? t('search.error.tooShort') : undefined
const hasError = !!errorMessage

const warningMessage = hasPrivacyProblem
? t('search.error.privacy', { appName: getAppTitle(), wordsOfPower })
const warningMessage = isValidMnemonic(trimmedValue)
? t('search.error.privacy', { appName: getAppTitle() })
: undefined
const hasWarning = !!warningMessage

Expand Down
16 changes: 4 additions & 12 deletions src/app/components/Search/search-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
isValidTxHash,
isValidOasisAddress,
isValidEthAddress,
getEvmBech32Address,
} from '../../utils/helpers'
import { RouteUtils, SpecifiedPerEnabledLayer } from '../../utils/route-utils'
import { AppError, AppErrors } from '../../../types/errors'
Expand Down Expand Up @@ -67,7 +66,7 @@ export const searchSuggestionTerms = {
},
} satisfies SpecifiedPerEnabledLayer<LayerSuggestions>

export const textSearchMininumLength = 3
export const textSearchMinimumLength = 3

export const validateAndNormalize = {
blockHeight: (searchTerm: string) => {
Expand Down Expand Up @@ -109,25 +108,18 @@ export const validateAndNormalize = {
return searchTerm.toLowerCase()
}
},
evmBech32Account: (searchTerm: string): string | undefined => {
const evmAccount = validateAndNormalize.evmAccount(searchTerm)
if (evmAccount) {
// TODO: remove conversion when API supports querying by EVM address
return getEvmBech32Address(evmAccount)
}
},
evmTokenNameFragment: (searchTerm: string) => {
if (searchTerm?.length >= textSearchMininumLength) {
if (searchTerm?.length >= textSearchMinimumLength) {
return searchTerm.toLowerCase()
}
},
networkProposalNameFragment: (searchTerm: string) => {
if (searchTerm?.length >= textSearchMininumLength) {
if (searchTerm?.length >= textSearchMinimumLength) {
return searchTerm.toLowerCase()
}
},
accountNameFragment: (searchTerm: string) => {
if (searchTerm?.length >= textSearchMininumLength) {
if (searchTerm?.length >= textSearchMinimumLength) {
return searchTerm.toLowerCase()
}
},
Expand Down
5 changes: 2 additions & 3 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@
"placeholder": "Address, Block, Contract, Transaction hash, Token name, etc.",
"error": {
"tooShort": "Please enter either at least 3 characters or a number in order to perform a search.",
"privacy": "It seems like you might accidentally entered a keyphrase for a wallet. Please note that your mnemonic is a secret key that should never be shared, even not with our {{ appName }}.\nExecuting this search is highly unlikely to return any results. If you want to proceed nonetheless, please add “{{ wordsOfPower }}” in front of your search query to perform the search at your own risk."
"privacy": "It seems like you might accidentally entered a keyphrase for a wallet. Please note that your mnemonic is a secret key that should never be shared, even not with our {{ appName }}.\nExecuting this search is highly unlikely to return any results."
},
"mobilePlaceholder": "Search Address, Block, Txn, Token, etc",
"noResults": {
Expand Down Expand Up @@ -633,8 +633,7 @@
},
"searchBtnText": "Search",
"searchSuggestions": "Not sure what to look for? Try out a search: <OptionalBreak><BlockLink><BlockIcon/> Block</BlockLink>, <TransactionLink><TransactionIcon/> Transaction</TransactionLink>, <AccountLink><AccountIcon/> Address</AccountLink>, <TokenLink><TokenIcon/> Token</TokenLink> </OptionalBreak>.",
"searchSuggestionsForNoResults": "Alternatively, you can view a random <BlockLink><BlockIcon/> Block</BlockLink>, <OptionalBreak><TransactionLink><TransactionIcon/> Transaction</TransactionLink>, <AccountLink><AccountIcon/> Address</AccountLink> or <TokenLink><TokenIcon/> Token</TokenLink></OptionalBreak> <OptionalBreak>to discover our Explorer.</OptionalBreak>",
"wordsOfPower": "I COMMAND THEE TO SEARCH FOR"
"searchSuggestionsForNoResults": "Alternatively, you can view a random <BlockLink><BlockIcon/> Block</BlockLink>, <OptionalBreak><TransactionLink><TransactionIcon/> Transaction</TransactionLink>, <AccountLink><AccountIcon/> Address</AccountLink> or <TokenLink><TokenIcon/> Token</TokenLink></OptionalBreak> <OptionalBreak>to discover our Explorer.</OptionalBreak>"
},
"tableSearch": {
"error": {
Expand Down

0 comments on commit aa6e673

Please sign in to comment.