Skip to content

Commit e1d3c75

Browse files
committed
Add search query param
1 parent 7abfedb commit e1d3c75

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

src/app/page.tsx

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useState, useEffect } from "react";
44
import Image from "next/image";
55
import CopyButton from "@/components/CopyButton";
66
import Link from "next/link";
7+
import { useRouter, useSearchParams } from "next/navigation";
78

89
interface SearchResult {
910
name: string;
@@ -45,13 +46,24 @@ const exampleSelectors = [
4546
];
4647

4748
export default function Home() {
49+
const router = useRouter();
50+
const searchParams = useSearchParams();
51+
4852
const [query, setQuery] = useState("");
4953
const [results, setResults] = useState<SearchResult[]>([]);
5054
const [loading, setLoading] = useState(false);
5155
const [stats, setStats] = useState<Stats | null>(null);
52-
const [searchType, setSearchType] = useState<"search" | "lookup">("search");
5356
const [error, setError] = useState<string | null>(null);
54-
const [statsError, setStatsError] = useState<string | null>(null);
57+
58+
// Update URL with search query
59+
const updateURL = (searchQuery: string) => {
60+
const params = new URLSearchParams();
61+
if (searchQuery.trim()) {
62+
params.set("q", searchQuery.trim());
63+
}
64+
const newURL = params.toString() ? `/?${params.toString()}` : "/";
65+
router.replace(newURL);
66+
};
5567

5668
const performSearch = async (searchQuery: string) => {
5769
if (!searchQuery.trim()) return;
@@ -62,6 +74,9 @@ export default function Home() {
6274

6375
const trimmedQuery = searchQuery.trim();
6476

77+
// Update URL with search query
78+
updateURL(trimmedQuery);
79+
6580
// Check if it's a hex query (starts with 0x or is 8 hex chars)
6681
const isHexQuery =
6782
trimmedQuery.toLowerCase().startsWith("0x") || (trimmedQuery.length === 8 && /^[a-fA-F0-9]+$/.test(trimmedQuery));
@@ -89,8 +104,6 @@ export default function Home() {
89104
return;
90105
}
91106

92-
setSearchType("lookup");
93-
94107
try {
95108
// Search for all types: function, event, and error
96109
const params = new URLSearchParams();
@@ -152,7 +165,6 @@ export default function Home() {
152165
}
153166
} else {
154167
// Text search
155-
setSearchType("search");
156168

157169
try {
158170
const response = await fetch(
@@ -235,9 +247,23 @@ export default function Home() {
235247
await performSearch(example);
236248
};
237249

250+
// Load query from URL parameters on mount
238251
useEffect(() => {
252+
const urlQuery = searchParams.get("q");
253+
if (urlQuery) {
254+
setQuery(urlQuery);
255+
performSearch(urlQuery);
256+
}
239257
fetchStats();
240-
}, []);
258+
}, [searchParams]);
259+
260+
// Update query state when URL changes
261+
useEffect(() => {
262+
const urlQuery = searchParams.get("q") || "";
263+
if (urlQuery !== query) {
264+
setQuery(urlQuery);
265+
}
266+
}, [searchParams, query]);
241267

242268
return (
243269
<div className="min-h-screen py-4">
@@ -309,11 +335,14 @@ export default function Home() {
309335

310336
<div className="mt-6 text-xs md:text-sm text-gray-600 space-y-1">
311337
<div>
312-
<b>Text search:</b> Use &apos;*&apos; and &apos;?&apos; for wildcards
338+
<b>Text search:</b> Use &apos;*&apos; and &apos;?&apos; for wildcards, case insensitive.
313339
</div>
314340
<div>
315341
<b>0x hash search:</b> Start with &apos;0x&apos;. Search 4byte or full 32 byte hash.
316342
</div>
343+
<div>
344+
<b>?q=0x12345678 in URL:</b> Use the ?q=0x12345678 query parameter for a sharable link.
345+
</div>
317346
</div>
318347

319348
{/* Example Selectors */}

0 commit comments

Comments
 (0)