diff --git a/public/locales/zh/common.json b/public/locales/zh/common.json index 84b5c9cc..694d47aa 100644 --- a/public/locales/zh/common.json +++ b/public/locales/zh/common.json @@ -75,5 +75,7 @@ "Successfully followed": "嘿!你已经准备好<2>在这里跟上你关注的博主的最新动态了。", "Enable AI Filtering": "启用 AI 过滤", "Filter out possible low-quality content based on AI ratings.": "基于 AI 评分过滤可能质量不佳的内容。", - "Refreshing": "刷新中" + "Refreshing": "刷新中", + "Search for your interest": "搜索你感兴趣的内容", + "results": "条结果" } \ No newline at end of file diff --git a/public/locales/zh/site.json b/public/locales/zh/site.json index cd8009a1..76841b73 100644 --- a/public/locales/zh/site.json +++ b/public/locales/zh/site.json @@ -14,5 +14,6 @@ "View on Hoot It": "在 Hoot It 上查看", "View on Crossbell Scan": "在 Crossbell Scan 上查看", "Subscribe to JSON Feed": "订阅 JSON Feed", - "Subscribe to RSS": "订阅 RSS" + "Subscribe to RSS": "订阅 RSS", + "Search on this site": "在本站搜索" } \ No newline at end of file diff --git a/src/components/common/SearchInput.tsx b/src/components/common/SearchInput.tsx index 8995dcab..a2a2b8c5 100644 --- a/src/components/common/SearchInput.tsx +++ b/src/components/common/SearchInput.tsx @@ -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(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 (
@@ -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} > - +
diff --git a/src/components/site/SiteHeader.tsx b/src/components/site/SiteHeader.tsx index 1b1acd24..4bf2487c 100644 --- a/src/components/site/SiteHeader.tsx +++ b/src/components/site/SiteHeader.tsx @@ -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: ( + + + + ), + onClick: () => setSearchOpen(true), + out: true, + }, ] + const [searchOpen, setSearchOpen] = useState(false) + const avatarRef = useRef(null) const bannerRef = useRef(null) @@ -235,48 +249,55 @@ export const SiteHeader: React.FC<{ dropdown={
{moreMenuItems.map((item) => { - return ( - - - {item.icon} - - {t(item.text)} - - ) + if (!item.out) { + return ( + + + {item.icon} + + {t(item.text)} + + ) + } })}
} />
- {moreMenuItems.map((item) => { - if (item.out) { - return ( - - - - ) - } else { - return null - } - })} + + + ) + } else { + return null + } + })} +
@@ -311,6 +332,11 @@ export const SiteHeader: React.FC<{
+ +
+ setSearchOpen(false)} /> +
+
) } diff --git a/src/components/site/SiteSearch.tsx b/src/components/site/SiteSearch.tsx new file mode 100644 index 00000000..1ba9a906 --- /dev/null +++ b/src/components/site/SiteSearch.tsx @@ -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 && } + {!!postPages[0].count && ( +
+ {postPages.map((posts) => + posts.list.map((post) => { + currentLength++ + return ( + +
+

+ {post.metadata.content.title} +

+
+ + {!!post.metadata.content.tags?.filter( + (tag) => tag !== "post" && tag !== "page", + ).length && ( + + {post.metadata.content.tags + ?.filter((tag) => tag !== "post" && tag !== "page") + .map((tag) => ( + { + e.preventDefault() + router.push(`/tag/${tag}`) + }} + > + #{tag} + + ))} + + )} + + + {post.metadata.content.views} + +
+
+ {post.metadata.content.summary} + {post.metadata.content.summary && "..."} +
+
+ {post.metadata.content.cover && ( +
+ cover +
+ )} + + ) + }), + )} +
+ )} + {hasNextPage && ( + + )} + + ) +} diff --git a/src/lib/slug-reserved-words.ts b/src/lib/slug-reserved-words.ts index d72de4bf..a066700c 100644 --- a/src/lib/slug-reserved-words.ts +++ b/src/lib/slug-reserved-words.ts @@ -12,6 +12,7 @@ const slugReservedWords = [ "sw.js", "manifest.json", "atom.xml", + "search", ] export const checkSlugReservedWords = (word: string) => { diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 090ebae0..4f4cff25 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -1,5 +1,5 @@ import { notFound } from "~/lib/server-side-props" -import { PageVisibilityEnum, Notes, Note } from "~/lib/types" +import { PageVisibilityEnum, Notes, Note, ExpandedNote } from "~/lib/types" import unidata from "~/queries/unidata.server" import { indexer } from "@crossbell/indexer" import { NoteMetadata } from "crossbell.js" @@ -9,7 +9,7 @@ import type Unidata from "unidata.js" import type { Contract } from "crossbell.js" import { checkSlugReservedWords } from "~/lib/slug-reserved-words" import { GeneralAccount } from "@crossbell/connect-kit" -import { expandUnidataNote } from "~/lib/expand-unit" +import { expandUnidataNote, expandCrossbellNote } from "~/lib/expand-unit" export async function checkPageSlug( input: { @@ -296,6 +296,31 @@ export async function getPagesBySite( return pages } +export async function getSearchPagesBySite(input: { + characterId?: string + keyword?: string + cursor?: string +}) { + const result = await indexer.searchNotes(input.keyword || "", { + cursor: input.cursor, + characterId: input.characterId, + tags: ["post"], + sources: ["xlog"], + }) + + const list: ExpandedNote[] = await Promise.all( + result.list.map(async (page: any) => { + return await expandCrossbellNote(page, true) + }), + ) + + return { + list, + cursor: result.cursor, + count: result.count, + } +} + export async function getPage( input: { /** page slug or id, `site` is needed when `page` is a slug */ diff --git a/src/pages/_site/[site]/search.tsx b/src/pages/_site/[site]/search.tsx new file mode 100644 index 00000000..0651d039 --- /dev/null +++ b/src/pages/_site/[site]/search.tsx @@ -0,0 +1,73 @@ +import { GetServerSideProps } from "next" +import { SiteSearch } from "~/components/site/SiteSearch" +import { SiteLayout } from "~/components/site/SiteLayout" +import { getServerSideProps as getLayoutServerSideProps } from "~/components/site/SiteLayout.server" +import { QueryClient } from "@tanstack/react-query" +import { useGetSearchPagesBySite } from "~/queries/page" +import type { ReactElement } from "react" +import { useGetSite } from "~/queries/site" +import { useRouter } from "next/router" +import { SearchInput } from "~/components/common/SearchInput" +import { useTranslation } from "next-i18next" + +export const getServerSideProps: GetServerSideProps = async (ctx) => { + const queryClient = new QueryClient() + const domainOrSubdomain = ctx.params!.site as string + const { props: layoutProps } = await getLayoutServerSideProps( + ctx, + queryClient, + { + skipPages: true, + }, + ) + + return { + props: { + ...layoutProps, + domainOrSubdomain, + }, + } +} + +function SiteSearchPage({ domainOrSubdomain }: { domainOrSubdomain: string }) { + const site = useGetSite(domainOrSubdomain) + const router = useRouter() + const keyword = router.query.q as string + const { t } = useTranslation(["common"]) + + const posts = useGetSearchPagesBySite({ + characterId: site.data?.metadata?.proof, + keyword, + }) + + return ( + <> +
+ +
+

+ {posts.data?.pages?.[0].count || "0"} {t("results")} +

+ {posts.isLoading ? ( + <>{t("Loading")}... + ) : ( + + )} + + ) +} + +SiteSearchPage.getLayout = (page: ReactElement) => { + return ( + + {page} + + ) +} + +export default SiteSearchPage diff --git a/src/queries/page.ts b/src/queries/page.ts index 5347d9fe..b8230619 100644 --- a/src/queries/page.ts +++ b/src/queries/page.ts @@ -62,6 +62,21 @@ export const useGetPagesBySite = ( }) } +export const useGetSearchPagesBySite = ( + input: Parameters[0], +) => { + return useInfiniteQuery({ + queryKey: ["getSearchPagesBySite", input.characterId, input], + queryFn: async ({ pageParam }) => { + return pageModel.getSearchPagesBySite({ + ...input, + cursor: pageParam, + }) + }, + getNextPageParam: (lastPage) => lastPage.cursor || undefined, + }) +} + export const useGetPage = (input: Parameters[0]) => { const unidata = useUnidata() return useQuery(["getPage", input.page || input.pageId, input], async () => {