feat: make PostMeta a server component
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
|
||||
export const Time: React.FC<{
|
||||
isoString?: string
|
||||
}> = ({ isoString }) => {
|
||||
const date = useDate()
|
||||
const [isMounted, setIsMounted] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
setIsMounted(true)
|
||||
}, [])
|
||||
|
||||
if (!isoString) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<time
|
||||
dateTime={date.formatToISO(isoString || "")}
|
||||
className="xlog-post-date whitespace-nowrap"
|
||||
>
|
||||
{date.formatDate(
|
||||
isoString || "",
|
||||
undefined,
|
||||
isMounted ? undefined : "America/Los_Angeles",
|
||||
)}
|
||||
</time>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
import { UniLink } from "~/components/ui/UniLink"
|
||||
import { useUserRole } from "~/hooks/useUserRole"
|
||||
import { SITE_URL } from "~/lib/env"
|
||||
import { useTranslation } from "~/lib/i18n/client"
|
||||
|
||||
export const EditButton: React.FC<{
|
||||
handle?: string
|
||||
noteId?: number
|
||||
isPost?: boolean
|
||||
}> = ({ handle, noteId, isPost }) => {
|
||||
const { t } = useTranslation("common")
|
||||
const [showEdit, setShowEdit] = useState(false)
|
||||
const userRole = useUserRole(handle)
|
||||
useEffect(() => {
|
||||
if (userRole.isSuccess && userRole.data) {
|
||||
setShowEdit(true)
|
||||
}
|
||||
}, [userRole.isSuccess, userRole.data])
|
||||
|
||||
return (
|
||||
<>
|
||||
{showEdit && (
|
||||
<UniLink
|
||||
className="xlog-post-editor inline-flex items-center"
|
||||
href={`${SITE_URL}/dashboard/${handle}/editor?id=${noteId}&type=${
|
||||
isPost ? "post" : "page"
|
||||
}`}
|
||||
>
|
||||
<i className="icon-[mingcute--edit-line] mx-1" /> {t("Edit")}
|
||||
</UniLink>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -1,58 +1,31 @@
|
||||
"use client"
|
||||
|
||||
// TODO
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
import { Time } from "~/components/common/Time"
|
||||
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
|
||||
import { EditButton } from "~/components/site/EditButton"
|
||||
import { UniLink } from "~/components/ui/UniLink"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
import { useUserRole } from "~/hooks/useUserRole"
|
||||
import { CSB_SCAN, SITE_URL } from "~/lib/env"
|
||||
import { useTranslation } from "~/lib/i18n/client"
|
||||
import { CSB_SCAN } from "~/lib/env"
|
||||
import { useTranslation } from "~/lib/i18n"
|
||||
import { toCid } from "~/lib/ipfs-parser"
|
||||
import { ExpandedCharacter, ExpandedNote } from "~/lib/types"
|
||||
import { useGetSummary } from "~/queries/page"
|
||||
import { getSummary } from "~/queries/page.server"
|
||||
|
||||
export const PostMeta: React.FC<{
|
||||
export default async function PostMeta({
|
||||
page,
|
||||
site,
|
||||
}: {
|
||||
page: ExpandedNote
|
||||
site?: ExpandedCharacter
|
||||
}> = ({ page, site }) => {
|
||||
const { t } = useTranslation("common")
|
||||
const date = useDate()
|
||||
const [isMounted, setIsMounted] = useState(false)
|
||||
const { i18n } = useTranslation()
|
||||
const summary = useGetSummary({
|
||||
}) {
|
||||
const { t } = await useTranslation("common")
|
||||
const { i18n } = await useTranslation()
|
||||
const summary = await getSummary({
|
||||
cid: toCid(page.metadata?.uri || ""),
|
||||
lang: i18n.resolvedLanguage,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
setIsMounted(true)
|
||||
}, [])
|
||||
|
||||
const [showEdit, setShowEdit] = useState(false)
|
||||
const userRole = useUserRole(site?.handle)
|
||||
useEffect(() => {
|
||||
if (userRole.isSuccess && userRole.data) {
|
||||
setShowEdit(true)
|
||||
}
|
||||
}, [userRole.isSuccess, userRole.data])
|
||||
|
||||
return (
|
||||
<div className="xlog-post-meta">
|
||||
<div className="text-zinc-400 mt-4 space-x-5 flex items-center">
|
||||
<time
|
||||
dateTime={date.formatToISO(
|
||||
page?.metadata?.content?.date_published || "",
|
||||
)}
|
||||
className="xlog-post-date whitespace-nowrap"
|
||||
>
|
||||
{date.formatDate(
|
||||
page.metadata?.content?.date_published || "",
|
||||
undefined,
|
||||
isMounted ? undefined : "America/Los_Angeles",
|
||||
)}
|
||||
</time>
|
||||
<Time isoString={page?.metadata?.content?.date_published} />
|
||||
{page.metadata?.content?.tags?.filter(
|
||||
(tag) => tag !== "post" && tag !== "page",
|
||||
).length ? (
|
||||
@@ -82,28 +55,19 @@ export const PostMeta: React.FC<{
|
||||
>
|
||||
<BlockchainIcon className="fill-zinc-500 ml-1" />
|
||||
</UniLink>
|
||||
{showEdit && (
|
||||
<UniLink
|
||||
className="xlog-post-editor inline-flex items-center"
|
||||
href={`${SITE_URL}/dashboard/${site?.handle}/editor?id=${
|
||||
page.noteId
|
||||
}&type=${
|
||||
page.metadata?.content?.tags?.includes("post") ? "post" : "page"
|
||||
}`}
|
||||
>
|
||||
<i className="icon-[mingcute--edit-line] mx-1" /> {t("Edit")}
|
||||
</UniLink>
|
||||
)}
|
||||
<EditButton
|
||||
handle={site?.handle}
|
||||
noteId={page.noteId}
|
||||
isPost={page.metadata?.content?.tags?.includes("post")}
|
||||
/>
|
||||
</div>
|
||||
{(summary.isLoading || summary.data) && (
|
||||
{summary && (
|
||||
<div className="xlog-post-summary border rounded-xl mt-4 p-4 space-y-2">
|
||||
<div className="font-bold text-zinc-700 flex items-center">
|
||||
<i className="icon-[mingcute--android-2-line] mr-2 text-lg" />
|
||||
{t("AI-generated summary")}
|
||||
</div>
|
||||
<div className="text-zinc-500 leading-loose text-sm">
|
||||
{summary.data || `${t("Generating")}...`}
|
||||
</div>
|
||||
<div className="text-zinc-500 leading-loose text-sm">{summary}</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -3,7 +3,7 @@ import serialize from "serialize-javascript"
|
||||
|
||||
import { PageContent } from "~/components/common/PageContent"
|
||||
import { PostFooter } from "~/components/site/PostFooter"
|
||||
import { PostMeta } from "~/components/site/PostMeta"
|
||||
import PostMeta from "~/components/site/PostMeta"
|
||||
import { getSiteLink } from "~/lib/helpers"
|
||||
import { ExpandedCharacter, ExpandedNote } from "~/lib/types"
|
||||
|
||||
@@ -67,6 +67,7 @@ export function SitePage({
|
||||
</h2>
|
||||
)}
|
||||
{page?.metadata?.content?.tags?.includes("post") && !preview && (
|
||||
/* @ts-expect-error Async Server Component */
|
||||
<PostMeta page={page} site={site} />
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user