diff --git a/src/components/site/SiteHome.tsx b/src/components/site/SiteHome.tsx index a2cf748d..aab583a0 100644 --- a/src/components/site/SiteHome.tsx +++ b/src/components/site/SiteHome.tsx @@ -1,21 +1,11 @@ "use client" -import Link from "next/link" -import { useRouter } from "next/navigation" -import { useEffect, useState } from "react" - -import PostCover from "~/components/site/PostCover" -import { Button } from "~/components/ui/Button" -import { EmptyState } from "~/components/ui/EmptyState" -import { useDate } from "~/hooks/useDate" -import { getSlugUrl } from "~/lib/helpers" -import { useTranslation } from "~/lib/i18n/client" +import SitePostList from "~/components/site/SitePostList" import { PageVisibilityEnum } from "~/lib/types" import { useGetPagesBySiteLite } from "~/queries/page" import { useGetSite } from "~/queries/site" export default function SiteHome({ handle }: { handle: string }) { - const router = useRouter() const site = useGetSite(handle) const posts = useGetPagesBySiteLite({ characterId: site.data?.characterId, @@ -24,120 +14,11 @@ export default function SiteHome({ handle }: { handle: string }) { useStat: true, }) - const { t } = useTranslation("site") - const date = useDate() - - const [isMounted, setIsMounted] = useState(false) - - useEffect(() => { - setIsMounted(true) - }, []) - if (!posts.data?.pages?.length) return null - let currentLength = 0 - return ( <> - {!posts.data.pages[0].count && } - {!!posts.data.pages[0].count && ( -
- {posts.data.pages.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.stopPropagation() - e.preventDefault() - router.push(`/tag/${tag}`) - }} - > - #{tag} - - ))} - - )} - - - {post.metadata?.content?.views} - -
-
- {post.metadata?.content?.summary} - {post.metadata?.content?.summary && "..."} -
-
- - ) - }), - )} -
- )} - {posts.hasNextPage && ( - - )} + ) } diff --git a/src/components/site/SitePostList.tsx b/src/components/site/SitePostList.tsx new file mode 100644 index 00000000..4510abb2 --- /dev/null +++ b/src/components/site/SitePostList.tsx @@ -0,0 +1,160 @@ +"use client" + +import Link from "next/link" +import { useRouter } from "next/navigation" +import { useEffect, useState } from "react" +import reactStringReplace from "react-string-replace" + +import { UseInfiniteQueryResult } from "@tanstack/react-query" + +import PostCover from "~/components/site/PostCover" +import { Button } from "~/components/ui/Button" +import { EmptyState } from "~/components/ui/EmptyState" +import { useDate } from "~/hooks/useDate" +import { getSlugUrl } from "~/lib/helpers" +import { useTranslation } from "~/lib/i18n/client" +import { ExpandedNote } from "~/lib/types" + +export default function SitePostList({ + posts, + keyword, +}: { + posts: UseInfiniteQueryResult< + { + list: ExpandedNote[] + count: number + cursor: string | null + }, + unknown + > + keyword?: string +}) { + const router = useRouter() + + const { t } = useTranslation("site") + const date = useDate() + + const [isMounted, setIsMounted] = useState(false) + + useEffect(() => { + setIsMounted(true) + }, []) + + if (!posts.data?.pages?.length) return null + + let currentLength = 0 + + return ( + <> + {!posts.data.pages[0].count && } + {!!posts.data.pages[0].count && ( +
+ {posts.data.pages.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.stopPropagation() + e.preventDefault() + router.push(`/tag/${tag}`) + }} + > + #{tag} + + ))} + + )} + + + {post.metadata?.content?.views} + +
+
+ {keyword + ? reactStringReplace( + post.metadata?.content?.summary || "", + keyword, + (match, i) => ( + + {match} + + ), + ) + : post.metadata?.content?.summary} + {post.metadata?.content?.summary && "..."} +
+
+ + ) + }), + )} +
+ )} + {posts.hasNextPage && ( + + )} + + ) +} diff --git a/src/components/site/SiteSearch.tsx b/src/components/site/SiteSearch.tsx index 535566b1..cc12e7cc 100644 --- a/src/components/site/SiteSearch.tsx +++ b/src/components/site/SiteSearch.tsx @@ -1,23 +1,14 @@ "use client" -import Link from "next/link" -import { useParams, useRouter, useSearchParams } from "next/navigation" -import { useEffect, useState } from "react" -import reactStringReplace from "react-string-replace" +import { useParams, useSearchParams } from "next/navigation" -import PostCover from "~/components/site/PostCover" -import { Button } from "~/components/ui/Button" -import { useDate } from "~/hooks/useDate" +import SitePostList from "~/components/site/SitePostList" import { useTranslation } from "~/lib/i18n/client" import { useGetSearchPagesBySite } from "~/queries/page" import { useGetSite } from "~/queries/site" -import { EmptyState } from "../ui/EmptyState" - export const SiteSearch = () => { - const router = useRouter() const { t } = useTranslation("site") - const date = useDate() const searchParams = useSearchParams() const params = useParams() @@ -28,120 +19,13 @@ export const SiteSearch = () => { keyword, }) - const [isMounted, setIsMounted] = useState(false) - - useEffect(() => { - setIsMounted(true) - }, []) - - let currentLength = 0 - return ( <>

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

{posts.isLoading && <>{t("Loading")}...} - {!posts.data?.pages?.[0].count && } - {!!posts.data?.pages?.[0].count && ( -
- {posts.data?.pages.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} - -
-
- {keyword - ? reactStringReplace( - post.metadata?.content?.summary || "", - keyword, - (match, i) => ( - - {match} - - ), - ) - : post.metadata?.content?.summary} - {post.metadata.content.summary && "..."} -
-
- - ) - }), - )} -
- )} - {posts.hasNextPage && posts.data?.pages[0].count && ( - - )} + ) }