feat: comment list in dashboard

This commit is contained in:
DIYgod
2023-04-26 17:43:32 +01:00
parent de213d2fa9
commit 3351a82232
8 changed files with 295 additions and 8 deletions
+6 -1
View File
@@ -191,5 +191,10 @@
"delete_confirmation_page": "你确定要删除这个页面吗?",
"Confirm": "确认",
"Cancel": "取消",
"Published a new post on my blockchain blog: {{title}}. Check it out now!": "在我的区块链博客发布了一篇新文章《{{title}}》,快来看看吧!"
"Published a new post on my blockchain blog: {{title}}. Check it out now!": "在我的区块链博客发布了一篇新文章《{{title}}》,快来看看吧!",
"Comments": "评论",
"You can subscribe to comments through an RSS reader to receive timely reminders.": "你可以通过 RSS 阅读器订阅评论,以便及时收到提醒。",
"Subscription address:": "订阅地址:",
"comment on your {{type}} {{toTitle}}" : "评论了你的{{type}}「{{toTitle}}」",
"{{newCount}} new comments" : "{{newCount}} 条新评论"
}
+2 -2
View File
@@ -40,7 +40,7 @@ export const Comment: React.FC<{
<CommentInput pageId={page?.id} />
<Virtuoso
className="xlog-comment-list mt-6"
className="xlog-comment-list"
useWindowScroll
data={comments.data?.pages}
endReached={() => comments.fetchNextPage()}
@@ -50,7 +50,7 @@ export const Comment: React.FC<{
itemContent={(_, p) =>
p?.list?.map((comment, idx) => (
<CommentItem
className={idx === 0 ? "" : "mt-6"}
className="mt-6"
originalId={page?.id}
comment={comment}
key={comment.transactionHash}
+11 -5
View File
@@ -116,11 +116,10 @@ export function DashboardLayout({
text: "Pages",
},
{
href: `/dashboard/${subdomain}/import`,
isActive: ({ pathname }) =>
pathname.startsWith(`/dashboard/${subdomain}/import`),
icon: "i-mingcute:file-import-line",
text: "Import",
href: `/dashboard/${subdomain}/comments`,
isActive: ({ href, pathname }) => href === pathname,
icon: "i-mingcute-comment-line",
text: "Comments",
},
{
onClick: showNotificationModal,
@@ -130,6 +129,13 @@ export function DashboardLayout({
: "i-mingcute:notification-fill",
text: isAllRead ? "Notifications" : "Unread notifications",
},
{
href: `/dashboard/${subdomain}/import`,
isActive: ({ pathname }) =>
pathname.startsWith(`/dashboard/${subdomain}/import`),
icon: "i-mingcute:file-import-line",
text: "Import",
},
{
href: `/dashboard/${subdomain}/events`,
isActive: ({ href, pathname }) => href === pathname,
+18
View File
@@ -403,6 +403,24 @@ export async function subscribeToSites(
}
}
export async function getCommentsBySite(input: {
characterId: string
cursor?: string
}) {
const notes = await indexer.getNotes({
toCharacterId: input.characterId,
limit: 7,
includeCharacter: true,
cursor: input.cursor,
})
notes.list = notes.list.filter((item) =>
item.toNote?.metadata?.content?.sources?.includes("xlog"),
)
return notes
}
const xLogOperatorPermissions: CharacterOperatorPermission[] = [
CharacterOperatorPermission.SET_NOTE_URI,
CharacterOperatorPermission.DELETE_NOTE,
+80
View File
@@ -0,0 +1,80 @@
import { GetServerSideProps } from "next"
import { QueryClient } from "@tanstack/react-query"
import { getSiteLink } from "~/lib/helpers"
import { renderPageContent } from "~/markdown"
import { fetchGetComments, fetchGetSite } from "~/queries/site.server"
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const queryClient = new QueryClient()
ctx.res.setHeader("Content-Type", "application/feed+json; charset=utf-8")
ctx.res.setHeader("Access-Control-Allow-Methods", "GET")
ctx.res.setHeader("Access-Control-Allow-Origin", "*")
const domainOrSubdomain = ctx.params!.site as string
const site = await fetchGetSite(domainOrSubdomain, queryClient)
const comments = await fetchGetComments(
{
characterId: site?.metadata?.proof,
},
queryClient,
)
const link = getSiteLink({
subdomain: site.username || "",
})
ctx.res.write(
JSON.stringify({
version: "https://jsonfeed.org/version/1.1",
title: "Comments on " + site.name,
description: site.description,
icon: site.avatars?.[0],
home_page_url: link,
feed_url: `${link}/feed/notifications`,
items: comments?.list?.map((comment) => {
const type = comment.toNote?.metadata?.content?.tags?.[0]
let toTitle
if (type === "post" || type === "page") {
toTitle = comment.toNote?.metadata?.content?.title
} else {
if ((comment.toNote?.metadata?.content?.content?.length || 0) > 30) {
toTitle =
comment.toNote?.metadata?.content?.content?.slice(0, 30) + "..."
} else {
toTitle = comment.toNote?.metadata?.content?.content
}
}
const name =
comment?.character?.metadata?.content?.name ||
`@${comment?.character?.handle}`
return {
id: comment.characterId + "-" + comment.noteId,
title: `${name}: ${comment.metadata?.content?.content}`,
content_html: `${name} commented on ${type} ${toTitle}: ${
renderPageContent(comment.metadata?.content?.content || "")
.contentHTML
}`,
url: `${link}/${
comment.toNote?.metadata?.content?.attributes?.find(
(attribute: any) => attribute.trait_type === "xlog_slug",
)?.value ||
comment.toNote?.characterId + "-" + comment.toNote?.noteId
}`,
date_published: comment.createdAt,
date_modified: comment.updatedAt,
}
}),
}),
)
ctx.res.end()
return {
props: {},
}
}
const SiteFeed: React.FC = () => null
export default SiteFeed
@@ -0,0 +1,137 @@
import { GetServerSideProps } from "next"
import { useTranslation } from "next-i18next"
import { useRouter } from "next/router"
import type { ReactElement } from "react"
import { Virtuoso } from "react-virtuoso"
import { CommentItem } from "~/components/common/CommentItem"
import { DashboardLayout } from "~/components/dashboard/DashboardLayout"
import { getServerSideProps as getLayoutServerSideProps } from "~/components/dashboard/DashboardLayout.server"
import { DashboardMain } from "~/components/dashboard/DashboardMain"
import { UniLink } from "~/components/ui/UniLink"
import { getSiteLink } from "~/lib/helpers"
import { serverSidePropsHandler } from "~/lib/server-side-props"
import { useGetCommentsBySite, useGetSite } from "~/queries/site"
export const getServerSideProps: GetServerSideProps = serverSidePropsHandler(
async (ctx) => {
const { props: layoutProps } = await getLayoutServerSideProps(ctx)
return {
props: {
...layoutProps,
},
}
},
)
export default function CommentsPage() {
const router = useRouter()
const subdomain = router.query.subdomain as string
const { t } = useTranslation(["dashboard", "common"])
const site = useGetSite(subdomain)
const comments = useGetCommentsBySite({
characterId: site.data?.metadata?.proof,
})
const feedUrl =
getSiteLink({
subdomain: subdomain,
}) + "/feed/comments"
return (
<DashboardMain title="Comments">
<div className="min-w-[270px] max-w-screen-lg">
<div className="text-sm text-zinc-500 leading-relaxed">
<p>
{t(
"You can subscribe to comments through an RSS reader to receive timely reminders.",
)}
</p>
<p>
{t("Subscription address:")}{" "}
<UniLink className="underline" href={feedUrl}>
{feedUrl}
</UniLink>
</p>
</div>
<div className="xlog-comment">
<div className="prose space-y-4 pt-4">
<Virtuoso
className="xlog-comment-list"
useWindowScroll
data={comments.data?.pages}
endReached={() => comments.fetchNextPage()}
components={{
Footer: comments.isLoading ? Loader : undefined,
}}
itemContent={(_, p) =>
p?.list?.map((comment, idx) => {
const type = comment.toNote?.metadata?.content?.tags?.[0]
let toTitle
if (type === "post" || type === "page") {
toTitle = comment.toNote?.metadata?.content?.title
} else {
if (
(comment.toNote?.metadata?.content?.content?.length ||
0) > 30
) {
toTitle =
comment.toNote?.metadata?.content?.content?.slice(
0,
30,
) + "..."
} else {
toTitle = comment.toNote?.metadata?.content?.content
}
}
const name =
comment?.character?.metadata?.content?.name ||
`@${comment?.character?.handle}`
return (
<div key={comment.transactionHash} className="mt-6">
<div>
{name}{" "}
{t("comment on your {{type}} {{toTitle}}", {
type: t(type || "", {
ns: "common",
}),
toTitle: toTitle,
})}
:
</div>
<CommentItem
className="mt-6"
comment={comment}
depth={0}
/>
</div>
)
})
}
></Virtuoso>
</div>
</div>
</div>
</DashboardMain>
)
}
CommentsPage.getLayout = (page: ReactElement) => {
return <DashboardLayout title="Comments">{page}</DashboardLayout>
}
const Loader = () => {
const { t } = useTranslation("common")
return (
<div
className="relative mt-4 w-full text-sm text-center py-4"
key={"loading"}
>
{t("Loading")}...
</div>
)
}
+19
View File
@@ -79,3 +79,22 @@ export const prefetchGetSites = async (queryClient: QueryClient) => {
})
})
}
export const fetchGetComments = async (
data: Parameters<typeof siteModel.getCommentsBySite>[0],
queryClient: QueryClient,
) => {
const key = ["getComments", data]
if (!data.characterId) {
return null
}
return await queryClient.fetchQuery(key, async () => {
return cacheGet({
key,
getValueFun: () =>
siteModel.getCommentsBySite({
characterId: data.characterId,
}),
}) as Promise<ReturnType<typeof siteModel.getCommentsBySite>>
})
}
+22
View File
@@ -229,6 +229,28 @@ export function useUnsubscribeFromSite() {
})
}
export const useGetCommentsBySite = (
data: Partial<Parameters<typeof siteModel.getCommentsBySite>[0]>,
) => {
return useInfiniteQuery({
queryKey: ["getCommentsBySite", data],
queryFn: async ({ pageParam }) => {
if (!data.characterId) {
return {
count: 0,
list: [],
cursor: undefined,
}
}
return siteModel.getCommentsBySite({
characterId: data.characterId,
cursor: pageParam,
})
},
getNextPageParam: (lastPage) => lastPage.cursor || undefined,
})
}
export const useGetOperators = (
data: Parameters<typeof siteModel.getOperators>[0],
) => {