2023-11-14 15:52:42 +00:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
import { usePathname, useRouter, useSearchParams } from "next/navigation"
|
|
|
|
import React, { FormEvent, useState } from "react"
|
2023-12-09 15:26:32 +00:00
|
|
|
import { Select, SelectContent, SelectTrigger, SelectItem, SelectValue } from "./ui/select"
|
|
|
|
import { SitesCombobox } from "./SitesCombobox"
|
|
|
|
import { FILTER_TIMES } from "@/utils"
|
2023-11-14 15:52:42 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
value: string
|
|
|
|
placeholder?: string
|
|
|
|
className?: string
|
2023-12-05 14:03:21 +00:00
|
|
|
filters?: {
|
2023-12-09 15:26:32 +00:00
|
|
|
sites: { value: string; label: string }[]
|
2023-12-05 14:03:21 +00:00
|
|
|
}
|
2023-11-14 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SimplifiedSearchBar = ({
|
|
|
|
value: initialValue,
|
|
|
|
placeholder,
|
2023-12-05 14:03:21 +00:00
|
|
|
filters,
|
2023-11-14 15:52:42 +00:00
|
|
|
className
|
|
|
|
}: Props) => {
|
2023-12-09 15:26:32 +00:00
|
|
|
const searchParams = useSearchParams()
|
|
|
|
const pathname = usePathname()
|
|
|
|
const router = useRouter()
|
2023-11-14 15:52:42 +00:00
|
|
|
const [value, setValue] = useState<string>(initialValue)
|
|
|
|
const handleSearch = (event: FormEvent<HTMLFormElement>) => {
|
2023-12-09 15:26:32 +00:00
|
|
|
event.preventDefault()
|
|
|
|
const newSearchParams = new URLSearchParams(searchParams ?? undefined)
|
2023-11-14 15:52:42 +00:00
|
|
|
|
2023-12-09 15:26:32 +00:00
|
|
|
if (value) {
|
|
|
|
newSearchParams.set("q", value)
|
2023-11-14 15:52:42 +00:00
|
|
|
} else {
|
2023-12-09 15:26:32 +00:00
|
|
|
newSearchParams.delete("q")
|
2023-11-14 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
router.push(`${pathname}?${newSearchParams}`)
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
className={`flex items-center text-lg border-b border-primary pb-2`}
|
|
|
|
onSubmit={handleSearch}
|
|
|
|
>
|
|
|
|
<div className="flex-1 flex flex-row max-w-full">
|
|
|
|
<span className="text-white mr-2">results: </span>
|
|
|
|
<fieldset
|
2023-12-09 15:26:32 +00:00
|
|
|
className={`${
|
|
|
|
value && value !== ""
|
|
|
|
? `after:content-['"'] before:content-['"'] relative after:absolute after:-right-1`
|
|
|
|
: "w-full"
|
|
|
|
} text-primary ${className}`}
|
2023-11-14 15:52:42 +00:00
|
|
|
>
|
|
|
|
<input
|
2023-12-09 15:26:32 +00:00
|
|
|
className={`flex-grow inline bg-transparent placeholder-gray-400 outline-none ring-none text-primary ${
|
|
|
|
value && value !== "" ? "" : "w-full"
|
|
|
|
}`}
|
2023-11-14 15:52:42 +00:00
|
|
|
placeholder={placeholder}
|
|
|
|
value={value}
|
|
|
|
onChange={(e) => setValue(e.target.value)}
|
|
|
|
size={value.length}
|
|
|
|
/>
|
|
|
|
</fieldset>
|
|
|
|
</div>
|
2023-12-09 15:26:32 +00:00
|
|
|
<div className="w-56 flex gap-2">
|
|
|
|
{/* Dropdown component should be here */}
|
|
|
|
<SitesCombobox />
|
|
|
|
{/* Dropdown component should be here */}
|
|
|
|
<Select defaultValue={"0"}>
|
|
|
|
<SelectTrigger className="hover:bg-muted w-auto">
|
|
|
|
<SelectValue placeholder="Time ago" />
|
|
|
|
</SelectTrigger>
|
|
|
|
<SelectContent>
|
|
|
|
{FILTER_TIMES.map((v) => (
|
|
|
|
<SelectItem
|
|
|
|
value={String(v.value)}
|
|
|
|
key={`FilteTimeSelectItem_${v.value}`}
|
|
|
|
>
|
|
|
|
{v.label}
|
|
|
|
</SelectItem>
|
|
|
|
))}
|
|
|
|
</SelectContent>
|
|
|
|
</Select>
|
|
|
|
</div>
|
2023-11-14 15:52:42 +00:00
|
|
|
</form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SimplifiedSearchBar
|