42 lines
986 B
TypeScript
42 lines
986 B
TypeScript
|
|
export type DirectSearchTarget =
|
||
|
|
| { kind: 'address'; href: string; label: string }
|
||
|
|
| { kind: 'transaction'; href: string; label: string }
|
||
|
|
| { kind: 'block'; href: string; label: string }
|
||
|
|
|
||
|
|
const addressPattern = /^0x[a-f0-9]{40}$/i
|
||
|
|
const transactionHashPattern = /^0x[a-f0-9]{64}$/i
|
||
|
|
const blockNumberPattern = /^\d+$/
|
||
|
|
|
||
|
|
export function inferDirectSearchTarget(query: string): DirectSearchTarget | null {
|
||
|
|
const trimmed = query.trim()
|
||
|
|
if (!trimmed) {
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
|
||
|
|
if (addressPattern.test(trimmed)) {
|
||
|
|
return {
|
||
|
|
kind: 'address',
|
||
|
|
href: `/addresses/0x${trimmed.slice(2)}`,
|
||
|
|
label: 'Open address',
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (transactionHashPattern.test(trimmed)) {
|
||
|
|
return {
|
||
|
|
kind: 'transaction',
|
||
|
|
href: `/transactions/0x${trimmed.slice(2)}`,
|
||
|
|
label: 'Open transaction',
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (blockNumberPattern.test(trimmed)) {
|
||
|
|
return {
|
||
|
|
kind: 'block',
|
||
|
|
href: `/blocks/${trimmed}`,
|
||
|
|
label: 'Open block',
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return null
|
||
|
|
}
|