explorer: fix search box (preventDefault + redirect no longer swallowed by try/catch)
This commit is contained in:
@@ -1,22 +1,15 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import { isBlockHash, isNumeric, isAddress } from '$lib/utils';
|
|
||||||
|
|
||||||
let query = $state('');
|
let query = $state('');
|
||||||
|
|
||||||
function handleSearch() {
|
function handleSearch(e: Event) {
|
||||||
|
e.preventDefault();
|
||||||
const q = query.trim();
|
const q = query.trim();
|
||||||
if (!q) return;
|
if (!q) return;
|
||||||
|
// Delegate disambiguation (height / block hash / txid / address) to the
|
||||||
if (isNumeric(q)) {
|
// /search server loader, which redirects to the correct page.
|
||||||
goto(`/blocks/${q}`);
|
goto(`/search?q=${encodeURIComponent(q)}`);
|
||||||
} else if (isBlockHash(q)) {
|
|
||||||
goto(`/search?q=${q}`);
|
|
||||||
} else if (isAddress(q)) {
|
|
||||||
goto(`/address/${q}`);
|
|
||||||
} else {
|
|
||||||
goto(`/search?q=${q}`);
|
|
||||||
}
|
|
||||||
query = '';
|
query = '';
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -7,32 +7,43 @@ export const load: PageServerLoad = async ({ url }) => {
|
|||||||
const q = url.searchParams.get('q')?.trim();
|
const q = url.searchParams.get('q')?.trim();
|
||||||
if (!q) error(400, 'No search query provided');
|
if (!q) error(400, 'No search query provided');
|
||||||
|
|
||||||
// Numeric: treat as block height
|
// Numeric: treat as block height.
|
||||||
|
// NOTE: redirect()/error() throw, so they must live OUTSIDE the try/catch —
|
||||||
|
// otherwise the thrown redirect is swallowed and turned into a 404.
|
||||||
if (isNumeric(q)) {
|
if (isNumeric(q)) {
|
||||||
|
let blockhash: string | undefined;
|
||||||
try {
|
try {
|
||||||
const { blockhash } = await getBlockHashByHeight(parseInt(q));
|
({ blockhash } = await getBlockHashByHeight(parseInt(q)));
|
||||||
redirect(302, `/block/${blockhash}`);
|
|
||||||
} catch {
|
} catch {
|
||||||
error(404, `Block at height ${q} not found`);
|
blockhash = undefined;
|
||||||
}
|
}
|
||||||
|
if (!blockhash) error(404, `Block at height ${q} not found`);
|
||||||
|
redirect(302, `/block/${blockhash}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 64-char hex: try block hash, then txid
|
// 64-char hex: try block hash, then txid.
|
||||||
if (isBlockHash(q)) {
|
if (isBlockHash(q)) {
|
||||||
|
let isBlock = false;
|
||||||
try {
|
try {
|
||||||
await getBlock(q);
|
await getBlock(q);
|
||||||
redirect(302, `/block/${q}`);
|
isBlock = true;
|
||||||
} catch {
|
} catch {
|
||||||
// Not a block, try as txid
|
isBlock = false;
|
||||||
}
|
}
|
||||||
|
if (isBlock) redirect(302, `/block/${q}`);
|
||||||
|
|
||||||
|
let isTx = false;
|
||||||
try {
|
try {
|
||||||
await getTransaction(q);
|
await getTransaction(q);
|
||||||
redirect(302, `/tx/${q}`);
|
isTx = true;
|
||||||
} catch {
|
} catch {
|
||||||
error(404, `No block or transaction found for ${q}`);
|
isTx = false;
|
||||||
}
|
}
|
||||||
|
if (isTx) redirect(302, `/tx/${q}`);
|
||||||
|
|
||||||
|
error(404, `No block or transaction found for ${q}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise: try as address
|
// Otherwise: treat as an address and go straight to its page.
|
||||||
redirect(302, `/address/${q}`);
|
redirect(302, `/address/${q}`);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user