import { useNavigate } from "@tanstack/react-router"; import { Search, X } from "lucide-react"; import { type ChangeEvent, type FormEvent, useCallback, useRef, useState } from "react"; import { type BrowseQuery, browseHref, DEFAULT_WINDOW } from "./links"; const ICON_CLASS = "h-3.5 w-3.5 text-extra-muted-foreground"; /** * Search box for the plugin pages. Typing replaces the URL with the browse page for the term, * keeping `scope`'s category and sort. Without JavaScript the form submits the same URL. */ export function PluginSearch({ scope, className }: { scope: BrowseQuery; className?: string }) { const navigate = useNavigate(); const input = useRef(null); const [term, setTerm] = useState(scope.q ?? ""); const search = useCallback( (next: string) => { setTerm(next); void navigate({ href: browseHref({ ...scope, q: next.trim() ? next : undefined }), replace: true, }); }, [navigate, scope], ); const handleChange = useCallback( (event: ChangeEvent) => search(event.target.value), [search], ); const handleClear = useCallback(() => { search(""); input.current?.focus(); }, [search]); const handleSubmit = useCallback((event: FormEvent) => event.preventDefault(), []); return (
{scope.sort === "new" && } {scope.sort === "installs" && scope.window !== DEFAULT_WINDOW && ( )}
{term && ( )}
); }