import React, { FormEvent, useCallback, useEffect, useState } from "react"; import { useLocation, useNavigate } from "@remix-run/react"; import { Select, SelectContent, SelectTrigger, SelectItem, SelectValue, } from "./ui/select"; import { SitesCombobox } from "./SitesCombobox"; import { FILTER_TIMES } from "@/utils"; import { SiteList } from "@/types.js"; type Props = { value: string; placeholder?: string; className?: string; filters?: { sites: { value: string; label: string }[]; }; sites: SiteList; }; const SimplifiedSearchBar = ({ value: initialValue, placeholder, filters, className, sites, }: Props) => { let navigate = useNavigate(); let location = useLocation(); const [value, setValue] = useState(initialValue); const [selectedSite, setSelectedSite] = useState(null); const [selectedTime, setSelectedTime] = useState(null); const handleSearch = useCallback( (event: FormEvent) => { event.preventDefault(); doSearch(); }, [value] ); function doSearch() { const newSearchParams = new URLSearchParams(location.search); if (value) { newSearchParams.set("q", value); } else { newSearchParams.delete("q"); return; } if (selectedSite) { newSearchParams.set("site", selectedSite); } if (selectedTime) { const timestampInMilliseconds = Date.parse(selectedTime); // Date.parse returns the timestamp in milliseconds const timestamp = timestampInMilliseconds / 1000; newSearchParams.set("time", timestamp.toString()); } navigate(`${location.pathname}?${newSearchParams}`); } useEffect(() => { doSearch(); }, [selectedSite, selectedTime]); return (
results:
setValue(e.target.value)} size={value.length} />
{/* Dropdown component should be here */} {/* Dropdown component should be here */}
); }; export default SimplifiedSearchBar;