|
1 | 1 | import { clsx } from "clsx"; |
2 | 2 | import { Loader2 } from "lucide-react"; |
3 | 3 | import { useCallback, useEffect, useRef, useState } from "react"; |
| 4 | +import { useSearchParams } from "react-router-dom"; |
4 | 5 | import { Navbar } from "../components/Navbar"; |
5 | 6 | import { PdfViewer } from "../components/PdfViewer"; |
6 | 7 |
|
@@ -35,14 +36,17 @@ interface SpecSearchResult { |
35 | 36 | const DEFAULT_PDF_URL = "https://cdn.ooxml.dev/ecma-376/part1.pdf"; |
36 | 37 |
|
37 | 38 | export function SpecExplorer() { |
38 | | - const [search, setSearch] = useState(""); |
| 39 | + const [searchParams] = useSearchParams(); |
| 40 | + const initialQuery = searchParams.get("q") || ""; |
| 41 | + const [search, setSearch] = useState(initialQuery); |
39 | 42 | const [submittedSearch, setSubmittedSearch] = useState(""); |
40 | 43 | const [results, setResults] = useState<SpecSearchResult[]>([]); |
41 | 44 | const [isLoading, setIsLoading] = useState(false); |
42 | 45 | const [selectedIndex, setSelectedIndex] = useState(0); |
43 | 46 | const [selectedResult, setSelectedResult] = useState<SpecSearchResult | null>(null); |
44 | 47 | const resultsRef = useRef<HTMLDivElement>(null); |
45 | 48 | const inputRef = useRef<HTMLTextAreaElement>(null); |
| 49 | + const hasAutoSearched = useRef(false); |
46 | 50 |
|
47 | 51 | // Preload the default PDF |
48 | 52 | useEffect(() => { |
@@ -94,6 +98,14 @@ export function SpecExplorer() { |
94 | 98 | } |
95 | 99 | }, [search, submittedSearch]); |
96 | 100 |
|
| 101 | + // Auto-search if query param provided (e.g., ?q=w:shd) |
| 102 | + useEffect(() => { |
| 103 | + if (initialQuery && !hasAutoSearched.current) { |
| 104 | + hasAutoSearched.current = true; |
| 105 | + handleSubmit(); |
| 106 | + } |
| 107 | + }, [initialQuery, handleSubmit]); |
| 108 | + |
97 | 109 | // Handle Enter key to submit |
98 | 110 | const handleKeyDown = useCallback( |
99 | 111 | (e: React.KeyboardEvent<HTMLTextAreaElement>) => { |
|
0 commit comments