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>
|
||||
|
||||
@@ -20,6 +20,8 @@ import { Menu } from "~/components/ui/Menu"
|
||||
import { useTranslation } from "next-i18next"
|
||||
import { Tooltip } from "~/components/ui/Tooltip"
|
||||
import { PatronButton } from "~/components/common/PatronButton"
|
||||
import { Modal } from "~/components/ui/Modal"
|
||||
import { SearchInput } from "~/components/common/SearchInput"
|
||||
|
||||
type HeaderLinkType = {
|
||||
icon?: React.ReactNode
|
||||
@@ -113,8 +115,20 @@ export const SiteHeader: React.FC<{
|
||||
url: `/feed/xml`,
|
||||
out: true,
|
||||
},
|
||||
{
|
||||
text: "Search on this site",
|
||||
icon: (
|
||||
<span className="text-stone-400">
|
||||
<i className="i-mingcute:search-line block text-2xl" />
|
||||
</span>
|
||||
),
|
||||
onClick: () => setSearchOpen(true),
|
||||
out: true,
|
||||
},
|
||||
]
|
||||
|
||||
const [searchOpen, setSearchOpen] = useState(false)
|
||||
|
||||
const avatarRef = useRef<HTMLImageElement>(null)
|
||||
const bannerRef = useRef<HTMLImageElement | HTMLVideoElement>(null)
|
||||
|
||||
@@ -235,48 +249,55 @@ export const SiteHeader: React.FC<{
|
||||
dropdown={
|
||||
<div className="text-gray-600 bg-white rounded-lg ring-1 ring-zinc-100 shadow-md py-2 text-sm">
|
||||
{moreMenuItems.map((item) => {
|
||||
return (
|
||||
<UniLink
|
||||
key={item.text}
|
||||
href={item.url}
|
||||
className="h-10 flex w-full space-x-2 items-center px-3 hover:bg-hover"
|
||||
>
|
||||
<span className="fill-gray-500 flex w-4 h-4">
|
||||
{item.icon}
|
||||
</span>
|
||||
<span>{t(item.text)}</span>
|
||||
</UniLink>
|
||||
)
|
||||
if (!item.out) {
|
||||
return (
|
||||
<UniLink
|
||||
key={item.text}
|
||||
href={item.url}
|
||||
className="h-10 flex w-full space-x-2 items-center px-3 hover:bg-hover"
|
||||
>
|
||||
<span className="fill-gray-500 flex w-4 h-4">
|
||||
{item.icon}
|
||||
</span>
|
||||
<span>{t(item.text)}</span>
|
||||
</UniLink>
|
||||
)
|
||||
}
|
||||
})}
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<div className="xlog-site-more-out hidden sm:block">
|
||||
{moreMenuItems.map((item) => {
|
||||
if (item.out) {
|
||||
return (
|
||||
<Tooltip
|
||||
label={t(item.text)}
|
||||
key={item.text}
|
||||
placement="bottom"
|
||||
>
|
||||
<Button
|
||||
variant="text"
|
||||
aria-label={item.text}
|
||||
className="-mx-2"
|
||||
onClick={() => window.open(item.url, "_blank")}
|
||||
<div className="-mx-2 flex">
|
||||
{moreMenuItems.map((item) => {
|
||||
if (item.out) {
|
||||
return (
|
||||
<Tooltip
|
||||
label={t(item.text)}
|
||||
key={item.text}
|
||||
placement="bottom"
|
||||
>
|
||||
<span className="fill-gray-500 flex w-6 h-6">
|
||||
{item.icon}
|
||||
</span>
|
||||
</Button>
|
||||
</Tooltip>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})}
|
||||
<Button
|
||||
variant="text"
|
||||
aria-label={item.text}
|
||||
onClick={() =>
|
||||
item.url
|
||||
? window.open(item.url, "_blank")
|
||||
: item.onClick?.()
|
||||
}
|
||||
>
|
||||
<span className="fill-gray-500 flex w-6 h-6">
|
||||
{item.icon}
|
||||
</span>
|
||||
</Button>
|
||||
</Tooltip>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div className="xlog-site-follow-button">
|
||||
<FollowingButton site={site} />
|
||||
@@ -311,6 +332,11 @@ export const SiteHeader: React.FC<{
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Modal open={searchOpen} setOpen={setSearchOpen}>
|
||||
<div className="p-3">
|
||||
<SearchInput noBorder={true} onSubmit={() => setSearchOpen(false)} />
|
||||
</div>
|
||||
</Modal>
|
||||
</header>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
import Link from "next/link"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
import { ExpandedNote, Notes } from "~/lib/types"
|
||||
import { EmptyState } from "../ui/EmptyState"
|
||||
import { useRouter } from "next/router"
|
||||
import { Image } from "~/components/ui/Image"
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import { useTranslation } from "next-i18next"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export const SiteSearch: React.FC<{
|
||||
postPages?: {
|
||||
list: ExpandedNote[]
|
||||
count: number
|
||||
cursor: string | null
|
||||
}[]
|
||||
fetchNextPage: () => void
|
||||
hasNextPage?: boolean
|
||||
isFetchingNextPage?: boolean
|
||||
}> = ({ postPages, fetchNextPage, hasNextPage, isFetchingNextPage }) => {
|
||||
const router = useRouter()
|
||||
const { t } = useTranslation(["common", "site"])
|
||||
const date = useDate()
|
||||
|
||||
const [isMounted, setIsMounted] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
setIsMounted(true)
|
||||
}, [])
|
||||
|
||||
if (!postPages?.length) return null
|
||||
|
||||
let currentLength = 0
|
||||
|
||||
return (
|
||||
<>
|
||||
{!postPages[0].count && <EmptyState />}
|
||||
{!!postPages[0].count && (
|
||||
<div className="xlog-posts space-y-8">
|
||||
{postPages.map((posts) =>
|
||||
posts.list.map((post) => {
|
||||
currentLength++
|
||||
return (
|
||||
<Link
|
||||
key={post.noteId}
|
||||
href={`/${post.metadata.content.slug}`}
|
||||
className="xlog-post sm:hover:bg-hover bg-white transition-all px-5 py-7 -mx-5 first:-mt-5 sm:rounded-xl flex flex-col sm:flex-row items-center"
|
||||
>
|
||||
<div className="flex-1 flex justify-center flex-col w-full min-w-0">
|
||||
<h3 className="xlog-post-title text-2xl font-bold text-zinc-700">
|
||||
{post.metadata.content.title}
|
||||
</h3>
|
||||
<div className="xlog-post-meta text-sm text-zinc-400 mt-1 space-x-4 flex items-center mr-8">
|
||||
<time
|
||||
dateTime={date.formatToISO(
|
||||
post.metadata.content.date_published ||
|
||||
post.createdAt,
|
||||
)}
|
||||
className="xlog-post-date whitespace-nowrap"
|
||||
>
|
||||
{date.formatDate(
|
||||
post.metadata.content.date_published ||
|
||||
post.createdAt,
|
||||
undefined,
|
||||
isMounted ? undefined : "America/Los_Angeles",
|
||||
)}
|
||||
</time>
|
||||
{!!post.metadata.content.tags?.filter(
|
||||
(tag) => tag !== "post" && tag !== "page",
|
||||
).length && (
|
||||
<span className="xlog-post-tags space-x-1 truncate min-w-0">
|
||||
{post.metadata.content.tags
|
||||
?.filter((tag) => tag !== "post" && tag !== "page")
|
||||
.map((tag) => (
|
||||
<span
|
||||
className="hover:text-zinc-600"
|
||||
key={tag}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
router.push(`/tag/${tag}`)
|
||||
}}
|
||||
>
|
||||
#{tag}
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
)}
|
||||
<span className="xlog-post-views inline-flex items-center">
|
||||
<i className="i-mingcute:eye-line mr-[2px]" />
|
||||
<span>{post.metadata.content.views}</span>
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
className="xlog-post-excerpt mt-3 text-zinc-500 line-clamp-2"
|
||||
style={{
|
||||
wordBreak: "break-word",
|
||||
}}
|
||||
>
|
||||
{post.metadata.content.summary}
|
||||
{post.metadata.content.summary && "..."}
|
||||
</div>
|
||||
</div>
|
||||
{post.metadata.content.cover && (
|
||||
<div className="xlog-post-cover flex items-center relative w-full sm:w-24 h-40 sm:h-24 mt-2 sm:ml-4 sm:mt-0">
|
||||
<Image
|
||||
className="object-cover rounded"
|
||||
alt="cover"
|
||||
fill={true}
|
||||
src={post.metadata.content.cover}
|
||||
></Image>
|
||||
</div>
|
||||
)}
|
||||
</Link>
|
||||
)
|
||||
}),
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{hasNextPage && (
|
||||
<Button
|
||||
className="mt-8 w-full bg-zinc-50 text-sm"
|
||||
variant="text"
|
||||
onClick={fetchNextPage}
|
||||
isLoading={isFetchingNextPage}
|
||||
aria-label="load more"
|
||||
>
|
||||
{t("load more", {
|
||||
ns: "site",
|
||||
name: t(
|
||||
"post" + (postPages[0].total - currentLength > 1 ? "s" : ""),
|
||||
),
|
||||
count: postPages[0].total - currentLength,
|
||||
})}
|
||||
</Button>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user