From 3351a8223245e3b09e53ff48014e82e278767df7 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 26 Apr 2023 17:43:32 +0100 Subject: [PATCH] feat: comment list in dashboard --- public/locales/zh/dashboard.json | 7 +- src/components/common/Comment.tsx | 4 +- src/components/dashboard/DashboardLayout.tsx | 16 ++- src/models/site.model.ts | 18 +++ src/pages/_site/[site]/feed/comments.tsx | 80 +++++++++++ src/pages/dashboard/[subdomain]/comments.tsx | 137 +++++++++++++++++++ src/queries/site.server.ts | 19 +++ src/queries/site.ts | 22 +++ 8 files changed, 295 insertions(+), 8 deletions(-) create mode 100644 src/pages/_site/[site]/feed/comments.tsx create mode 100644 src/pages/dashboard/[subdomain]/comments.tsx diff --git a/public/locales/zh/dashboard.json b/public/locales/zh/dashboard.json index cb4d31c6..2540b2e4 100644 --- a/public/locales/zh/dashboard.json +++ b/public/locales/zh/dashboard.json @@ -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}} 条新评论" } \ No newline at end of file diff --git a/src/components/common/Comment.tsx b/src/components/common/Comment.tsx index 8d749b57..7a28f204 100644 --- a/src/components/common/Comment.tsx +++ b/src/components/common/Comment.tsx @@ -40,7 +40,7 @@ export const Comment: React.FC<{ comments.fetchNextPage()} @@ -50,7 +50,7 @@ export const Comment: React.FC<{ itemContent={(_, p) => p?.list?.map((comment, idx) => ( - 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, diff --git a/src/models/site.model.ts b/src/models/site.model.ts index fde75aaf..b746a7e3 100644 --- a/src/models/site.model.ts +++ b/src/models/site.model.ts @@ -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, diff --git a/src/pages/_site/[site]/feed/comments.tsx b/src/pages/_site/[site]/feed/comments.tsx new file mode 100644 index 00000000..3c5e06d7 --- /dev/null +++ b/src/pages/_site/[site]/feed/comments.tsx @@ -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 diff --git a/src/pages/dashboard/[subdomain]/comments.tsx b/src/pages/dashboard/[subdomain]/comments.tsx new file mode 100644 index 00000000..b7a3a2f3 --- /dev/null +++ b/src/pages/dashboard/[subdomain]/comments.tsx @@ -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 ( + +
+
+

+ {t( + "You can subscribe to comments through an RSS reader to receive timely reminders.", + )} +

+

+ {t("Subscription address:")}{" "} + + {feedUrl} + +

+
+
+
+ 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 ( +
+
+ {name}{" "} + {t("comment on your {{type}} {{toTitle}}", { + type: t(type || "", { + ns: "common", + }), + toTitle: toTitle, + })} + : +
+ +
+ ) + }) + } + >
+
+
+
+
+ ) +} + +CommentsPage.getLayout = (page: ReactElement) => { + return {page} +} + +const Loader = () => { + const { t } = useTranslation("common") + return ( +
+ {t("Loading")}... +
+ ) +} diff --git a/src/queries/site.server.ts b/src/queries/site.server.ts index b35a8f1d..bc4b2fc5 100644 --- a/src/queries/site.server.ts +++ b/src/queries/site.server.ts @@ -79,3 +79,22 @@ export const prefetchGetSites = async (queryClient: QueryClient) => { }) }) } + +export const fetchGetComments = async ( + data: Parameters[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> + }) +} diff --git a/src/queries/site.ts b/src/queries/site.ts index a8c70199..ca9838de 100644 --- a/src/queries/site.ts +++ b/src/queries/site.ts @@ -229,6 +229,28 @@ export function useUnsubscribeFromSite() { }) } +export const useGetCommentsBySite = ( + data: Partial[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[0], ) => {