feat: add site search
This commit is contained in:
@@ -1,30 +1,16 @@
|
||||
import { Profile } from "~/lib/types"
|
||||
import { useAccountSites } from "~/queries/site"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { useAccountState } from "@crossbell/connect-kit"
|
||||
import { useState, useEffect } from "react"
|
||||
import { useCommentPage, useUpdateComment } from "~/queries/page"
|
||||
import { useRouter } from "next/router"
|
||||
import { useTranslation } from "next-i18next"
|
||||
import { cn } from "~/lib/utils"
|
||||
|
||||
export const SearchInput: React.FC<{
|
||||
characterId?: string
|
||||
value?: string
|
||||
}> = ({ characterId, value }) => {
|
||||
const account = useAccountState((s) => s.computed.account)
|
||||
const userSites = useAccountSites()
|
||||
const commentPage = useCommentPage()
|
||||
const updateComment = useUpdateComment()
|
||||
noBorder?: boolean
|
||||
onSubmit?: (value?: string) => void
|
||||
}> = ({ value, noBorder, onSubmit }) => {
|
||||
const router = useRouter()
|
||||
const [viewer, setViewer] = useState<Profile | null>(null)
|
||||
const { t } = useTranslation(["common", "site"])
|
||||
|
||||
useEffect(() => {
|
||||
if (userSites.isSuccess && userSites.data?.length) {
|
||||
setViewer(userSites.data[0])
|
||||
}
|
||||
}, [userSites, router])
|
||||
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
content: value || "",
|
||||
@@ -33,14 +19,9 @@ export const SearchInput: React.FC<{
|
||||
|
||||
const handleSubmit = form.handleSubmit(async (values) => {
|
||||
router.push(`/search?q=${values.content}`)
|
||||
onSubmit?.(values.content)
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (commentPage.isSuccess || updateComment.isSuccess) {
|
||||
form.reset()
|
||||
}
|
||||
}, [commentPage.isSuccess, updateComment.isSuccess, form])
|
||||
|
||||
return (
|
||||
<div className="xlog-comment-input flex">
|
||||
<form className="w-full relative" onSubmit={handleSubmit}>
|
||||
@@ -48,12 +29,16 @@ export const SearchInput: React.FC<{
|
||||
className="absolute left-0 top-1/2 -translate-y-1/2 text-2xl text-zinc-500 h-11 w-14 flex items-center justify-center cursor-pointer"
|
||||
onClick={handleSubmit}
|
||||
>
|
||||
<i className="i-mingcute:search-line block"></i>
|
||||
<i className="i-mingcute:search-line block" />
|
||||
</div>
|
||||
<input
|
||||
id="content"
|
||||
className="rounded-full w-full pl-12 pr-5 h-11 border outline-none hover:shadow-md focus:shadow-md transition-shadow"
|
||||
placeholder={t("Search for your interest", { ns: "site" }) || ""}
|
||||
className={cn(
|
||||
"w-full pl-12 pr-5 h-11 outline-none",
|
||||
!noBorder &&
|
||||
"border rounded-full hover:shadow-md focus:shadow-md transition-shadow",
|
||||
)}
|
||||
placeholder={t("Search for your interest") || ""}
|
||||
{...form.register("content", {})}
|
||||
/>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user