feat: use post card in site

This commit is contained in:
DIYgod
2023-07-17 22:47:54 +01:00
parent cfa044c872
commit ab15f171b9
12 changed files with 88 additions and 190 deletions
+2 -2
View File
@@ -138,7 +138,7 @@ export default async function SitePagePage({
}
return (
<>
<div className="max-w-screen-md mx-auto">
<script
type="application/ld+json"
dangerouslySetInnerHTML={addPageJsonLd()}
@@ -185,6 +185,6 @@ export default async function SitePagePage({
{page?.metadata && <PostFooter page={page} site={site} />}
</Hydrate>
)}
</>
</div>
)
}
+2 -2
View File
@@ -194,13 +194,13 @@ export default async function SiteLayout({
<main
className={cn(
`xlog-post-id-${page?.characterId}-${page?.noteId}`,
"xlog-deprecated-class xlog-post-area max-w-screen-md mx-auto px-5 pt-12 relative",
"xlog-deprecated-class xlog-post-area max-w-screen-lg mx-auto px-5 pt-12 relative",
)}
>
{children}
</main>
{site && !onlyContent && (
<section className="xlog-blockchain-info max-w-screen-md mx-auto pt-12 pb-10">
<section className="xlog-blockchain-info max-w-screen-lg mx-auto pt-12 pb-10">
<BlockchainInfo site={site} page={page || undefined} />
</section>
)}
@@ -31,7 +31,7 @@ export default function SitePreviewPage() {
const { t } = useTranslation("site")
return (
<>
<div className="max-w-screen-md mx-auto">
<div className="fixed top-0 left-0 w-full text-center text-red-500 bg-gray-100 py-2 opacity-80 text-sm z-10">
{t(
"This address is in local editing preview mode and cannot be viewed by the public.",
@@ -55,6 +55,6 @@ export default function SitePreviewPage() {
toc={true}
></DynamicPageContent>
</article>
</>
</div>
)
}
+63 -38
View File
@@ -11,7 +11,6 @@ import { Titles } from "~/components/common/Titles"
import PostCover from "~/components/home/PostCover"
import { Avatar } from "~/components/ui/Avatar"
import { useDate } from "~/hooks/useDate"
import { useIsMobileLayout } from "~/hooks/useMobileLayout"
import { getSiteLink } from "~/lib/helpers"
import { useTranslation } from "~/lib/i18n/client"
import { ExpandedNote } from "~/lib/types"
@@ -23,28 +22,29 @@ const Card = ({
keyword,
comment,
createdAt,
isPinned,
linkPrefix,
isBlank,
}: {
character?: CharacterEntity
post: ExpandedNote
keyword?: string
comment?: string
createdAt?: string
isPinned?: boolean
linkPrefix?: string
isBlank?: boolean
}) => {
const router = useRouter()
const { t } = useTranslation("common")
const date = useDate()
const isMobileLayout = useIsMobileLayout()
return (
<Link
target={isMobileLayout ? "_blank" : undefined}
href={
isMobileLayout
? `/site/${post?.character?.handle}/${post.metadata?.content?.slug}`
: `/post/${post?.character?.handle}/${post.metadata?.content?.slug}`
}
target={isBlank ? "_blank" : undefined}
href={`${linkPrefix || ""}/${post.metadata?.content?.slug}`}
className={cn(
"xlog-post sm:hover:bg-hover transition-all rounded-2xl flex flex-col items-center hover:opacity-100 group border",
"xlog-post sm:hover:bg-hover transition-all rounded-2xl flex flex-col items-center hover:opacity-100 group border relative",
)}
>
<PostCover
@@ -92,7 +92,7 @@ const Card = ({
<div className="xlog-post-meta text-zinc-400 space-x-2 flex items-center text-[13px] h-[26px] truncate">
{!!post.metadata?.content?.tags?.[1] && (
<span
className="xlog-post-tags hover:text-zinc-600 hover:bg-zinc-200 border transition-colors text-zinc-500 inline-flex items-center bg-zinc-100 rounded-full px-2 py-[2px] truncate"
className="xlog-post-tags hover:text-zinc-600 hover:bg-zinc-200 border transition-colors text-zinc-500 inline-flex items-center bg-zinc-100 rounded-full px-2 py-[1.5px] truncate"
onClick={(e) => {
e.preventDefault()
router.push(`/tag/${post.metadata?.content?.tags?.[1]}`)
@@ -120,33 +120,37 @@ const Card = ({
)}
</div>
<div className="flex items-center space-x-1 text-xs sm:text-sm overflow-hidden">
<CharacterFloatCard siteId={character?.handle}>
<span
className="flex items-center cursor-pointer"
onClick={(e) => {
e.preventDefault()
window.open(
`${getSiteLink({
subdomain: character?.handle || "",
})}`,
)
}}
>
<span className="w-5 h-5 inline-block mr-[6px]">
<Avatar
cid={character?.characterId}
images={character?.metadata?.content?.avatars || []}
size={20}
name={character?.metadata?.content?.name}
></Avatar>
</span>
<span className="font-medium truncate text-zinc-600">
{character?.metadata?.content?.name || character?.handle}
</span>
</span>
</CharacterFloatCard>
<Titles characterId={character?.characterId} />
<span className="text-zinc-400 hidden sm:inline-block">·</span>
{character && (
<>
<CharacterFloatCard siteId={character?.handle}>
<span
className="flex items-center cursor-pointer"
onClick={(e) => {
e.preventDefault()
window.open(
`${getSiteLink({
subdomain: character?.handle || "",
})}`,
)
}}
>
<span className="w-5 h-5 inline-block mr-[6px]">
<Avatar
cid={character?.characterId}
images={character?.metadata?.content?.avatars || []}
size={20}
name={character?.metadata?.content?.name}
></Avatar>
</span>
<span className="font-medium truncate text-zinc-600">
{character?.metadata?.content?.name || character?.handle}
</span>
</span>
</CharacterFloatCard>
<Titles characterId={character?.characterId} />
<span className="text-zinc-400 hidden sm:inline-block">·</span>
</>
)}
<time
dateTime={date.formatToISO(createdAt || post.createdAt)}
className="xlog-post-date whitespace-nowrap text-zinc-400 hidden sm:inline-block"
@@ -164,11 +168,29 @@ const Card = ({
</time>
</div>
</div>
{isPinned && (
<span className="absolute top-2 right-2 text-xs border transition-colors text-zinc-500 inline-flex items-center bg-zinc-100 rounded-full px-2 py-[1.5px]">
<i className="icon-[mingcute--pin-2-fill] mr-1" />
{t("Pinned")}
</span>
)}
</Link>
)
}
const Post = ({ post, keyword }: { post: ExpandedNote; keyword?: string }) => {
const Post = ({
post,
keyword,
isPinned,
linkPrefix,
isBlank,
}: {
post: ExpandedNote
keyword?: string
isPinned?: boolean
linkPrefix?: string
isBlank?: boolean
}) => {
let isComment
if (post.metadata?.content?.tags?.includes("comment") && post.toNote) {
isComment = true
@@ -181,6 +203,9 @@ const Post = ({ post, keyword }: { post: ExpandedNote; keyword?: string }) => {
keyword={keyword}
comment={isComment ? post.metadata?.content?.summary : undefined}
createdAt={isComment ? post?.createdAt : post.toNote?.createdAt}
isPinned={isPinned}
linkPrefix={linkPrefix}
isBlank={isBlank}
/>
)
}
+9
View File
@@ -12,6 +12,7 @@ import { EmptyState } from "~/components/ui/EmptyState"
import { Skeleton } from "~/components/ui/Skeleton"
import { Tabs } from "~/components/ui/Tabs"
import { Tooltip } from "~/components/ui/Tooltip"
import { useIsMobileLayout } from "~/hooks/useMobileLayout"
import { useTranslation } from "~/lib/i18n/client"
import { getStorage, setStorage } from "~/lib/storage"
import { ExpandedNote } from "~/lib/types"
@@ -170,6 +171,8 @@ export const HomeFeed = ({ type }: { type?: FeedType }) => {
setIsMounted(true)
}, [])
const isMobileLayout = useIsMobileLayout()
return (
<>
<div className="space-y-10">
@@ -236,6 +239,12 @@ export const HomeFeed = ({ type }: { type?: FeedType }) => {
key={`${post.characterId}-${post.noteId}`}
post={post}
keyword={searchParams?.get("q") || undefined}
linkPrefix={
isMobileLayout
? `/site/${post?.character?.handle}`
: `/post/${post?.character?.handle}`
}
isBlank={isMobileLayout}
/>
)
}}
+4 -136
View File
@@ -1,19 +1,10 @@
"use client"
import Link from "next/link"
import { usePathname, useRouter } from "next/navigation"
import { memo } from "react"
import reactStringReplace from "react-string-replace"
import { UseInfiniteQueryResult } from "@tanstack/react-query"
import FadeIn from "~/components/common/FadeIn"
import PostCover from "~/components/site/PostCover"
import PostCard from "~/components/common/PostCard"
import { Button } from "~/components/ui/Button"
import { EmptyState } from "~/components/ui/EmptyState"
import { useIsClient } from "~/hooks/useClient"
import { useDate } from "~/hooks/useDate"
import { getSiteRelativeUrl } from "~/lib/helpers"
import { useTranslation } from "~/lib/i18n/client"
import { ExpandedNote } from "~/lib/types"
@@ -43,17 +34,16 @@ export default function PostList({
<>
{!posts.data.pages[0].count && <EmptyState />}
{!!posts.data.pages[0].count && (
<div className="xlog-posts space-y-8">
<div className="xlog-posts grid gap-3 sm:gap-6 grid-cols-2 sm:grid-cols-3">
{posts.data.pages.map((posts, listIndex) =>
posts.list.map((post, postIndex) => {
currentLength++
return (
<PostItem
<PostCard
key={`${post.characterId}-${post.noteId}`}
post={post}
isPinned={post.noteId === pinnedNoteId}
keyword={keyword}
key={post.noteId}
priority={listIndex === 0 && postIndex < 3}
/>
)
}),
@@ -85,125 +75,3 @@ export default function PostList({
</>
)
}
const PostItem = memo(
({
post,
isPinned,
keyword,
priority,
}: {
post: ExpandedNote
isPinned?: boolean
keyword?: string
priority?: boolean
}) => {
const { t } = useTranslation("site")
const isMounted = useIsClient()
const date = useDate()
const router = useRouter()
const pathname = usePathname()
return (
<FadeIn
key={`${post.characterId}-${post.noteId}`}
className="xlog-post -mx-5 first:-mt-5 sm:rounded-2xl bg-white"
role="article"
aria-label={post.metadata?.content?.title}
>
<Link
className="flex flex-col sm:flex-row items-center group px-5 py-7 focus-visible:outline focus-visible:outline-accent"
href={getSiteRelativeUrl(
pathname,
`/${post.metadata?.content?.slug}`,
)}
suppressHydrationWarning
>
{isPinned && (
<span className="absolute top-2 right-5 text-xs text-zinc-400 flex items-center gap-1">
<i className="icon-[mingcute--pin-2-fill]" />
{t("Pinned")}
</span>
)}
<PostCover
cover={post.metadata?.content?.cover}
priority={priority}
/>
<div className="flex-1 flex justify-center flex-col w-full min-w-0">
<h2 className="xlog-post-title text-2xl font-bold text-zinc-700 line-clamp-2">
{post.metadata?.content?.title}
</h2>
<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 || "",
)}
className="xlog-post-date whitespace-nowrap"
>
{date.formatDate(
post.metadata?.content?.date_published || "",
undefined,
isMounted ? undefined : "America/Los_Angeles",
)}
</time>
{!!post.metadata?.content?.tags?.filter(
(tag) => tag !== "post" && tag !== "page",
).length && (
<span
aria-label="tags for this post"
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.stopPropagation()
e.preventDefault()
router.push(
getSiteRelativeUrl(pathname, `/tag/${tag}`),
)
}}
>
#{tag}
</span>
))}
</span>
)}
<span
aria-label="views for this post"
className="xlog-post-views inline-flex items-center"
>
<i className="icon-[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",
}}
>
{keyword
? reactStringReplace(
post.metadata?.content?.summary || "",
keyword,
(match, i) => (
<span key={i} className="bg-yellow-200">
{match}
</span>
),
)
: post.metadata?.content?.summary}
{post.metadata?.content?.summary && "..."}
</div>
</div>
</Link>
</FadeIn>
)
},
)
PostItem.displayName = "PostItem"
+1 -1
View File
@@ -36,7 +36,7 @@ export default function PostMeta({
) : null}
<span className="xlog-post-views inline-flex items-center">
<i className="icon-[mingcute--eye-line] mr-[2px]" />
<span>{page.metadata?.content?.views}</span>
<span>{page.stat?.viewDetailCount}</span>
</span>
<UniLink
className="xlog-post-blockchain inline-flex items-center"
+1 -1
View File
@@ -31,7 +31,7 @@ export default async function SiteFooter({
return (
<>
<footer className="text-zinc-500 border-t">
<div className="max-w-screen-md mx-auto px-5 py-10 text-xs sm:flex justify-between sm:space-x-5 sm:space-y-0 space-y-5">
<div className="max-w-screen-lg mx-auto px-5 py-10 text-xs sm:flex justify-between sm:space-x-5 sm:space-y-0 space-y-5">
<div className="font-medium text-base">
<span>&copy; </span>
<UniLink href="/" className="hover:text-accent">
+1 -1
View File
@@ -67,7 +67,7 @@ export const SiteHeader = async ({
}
})()}
</div>
<div className="px-5 max-w-screen-md mx-auto h-full relative flex items-center flex-col">
<div className="px-5 max-w-screen-lg mx-auto h-full relative flex items-center flex-col">
<div className="mb-auto"></div>
<div className="flex py-12 w-full">
<div
+2 -5
View File
@@ -81,16 +81,13 @@ export const expandCrossbellNote = async ({
: 0
if (useStat) {
if ((note as any).stat?.viewDetailCount) {
expandedNote.metadata.content.views = (note as any).stat
?.viewDetailCount
} else {
if (!expandedNote.stat) {
const stat = await (
await fetch(
`https://indexer.crossbell.io/v1/stat/notes/${expandedNote.characterId}/${expandedNote.noteId}`,
)
).json()
expandedNote.metadata.content.views = stat.viewDetailCount
expandedNote.stat = stat
}
}
-1
View File
@@ -80,7 +80,6 @@ export type ExpandedNote = NoteEntity & {
cover?: string
frontMatter?: Record<string, any>
slug?: string
views?: number
audio?: string
score?: {
number?: number
+1 -1
View File
@@ -165,7 +165,7 @@ export async function getPagesBySite(input: {
await indexer.note.getMany({
characterId: input.characterId,
limit: input.limit || 10,
limit: input.limit || 12,
cursor: input.cursor,
orderBy: "publishedAt",
tags: [...(input.tags || []), input.type],