From 5ed8e70bd84653c40a3f17d40fe02f10954ffe24 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 8 Aug 2022 23:14:18 +0100 Subject: [PATCH] feat: comment component --- src/components/common/BlockchainInfo.tsx | 20 +-- src/components/common/Comment.tsx | 162 +++++++++++++++++++++++ src/components/icons/BlockchainIcon.tsx | 9 +- src/components/site/PostFooter.tsx | 66 ++++----- src/components/site/PostMeta.tsx | 22 ++- src/components/site/SitePage.tsx | 4 +- src/css/tailwind.css | 7 + src/lib/types.ts | 1 + src/models/page.model.ts | 59 ++++++++- src/queries/page.ts | 25 ++++ 10 files changed, 320 insertions(+), 55 deletions(-) create mode 100644 src/components/common/Comment.tsx diff --git a/src/components/common/BlockchainInfo.tsx b/src/components/common/BlockchainInfo.tsx index dd4fb24e..d33aac6d 100644 --- a/src/components/common/BlockchainInfo.tsx +++ b/src/components/common/BlockchainInfo.tsx @@ -3,6 +3,7 @@ import { UniLink } from "../ui/UniLink" import { Profile, Note } from "~/lib/types" import { Disclosure } from "@headlessui/react" import { ChevronUpIcon } from "@heroicons/react/solid" +import { BlockchainIcon } from "~/components/icons/BlockchainIcon" export const BlockchainInfo: React.FC<{ site?: Profile @@ -15,14 +16,17 @@ export const BlockchainInfo: React.FC<{ <> - 🔔 This{" "} - {page - ? page.tags?.includes("post") - ? "post" - : "page" - : "blog"}{" "} - has been permanently stored on the blockchain and signed by its - creator. + + + This{" "} + {page + ? page.tags?.includes("post") + ? "post" + : "page" + : "blog"}{" "} + has been permanently stored on the blockchain and signed by + its creator. + = ({ page, className }) => { + const { address } = useAccount() + const userSites = useGetUserSites(address) + const { openConnectModal } = useConnectModal() + const commentPage = useCommentPage() + const router = useRouter() + const [viewer, setViewer] = useState(null) + const [addressIn, setAddressIn] = useState("") + const comments = useGetComments({ + pageId: page?.id, + }) + + useEffect(() => { + setAddressIn(address || "") + }, [address]) + + useEffect(() => { + if (userSites.isSuccess && userSites.data?.length) { + setViewer(userSites.data[0]) + } + }, [userSites, router]) + + const form = useForm({ + defaultValues: { + content: "", + }, + }) + + const handleSubmit = form.handleSubmit(async (values) => { + if (!address) { + openConnectModal?.() + } else if (userSites.isSuccess && !userSites.data?.length) { + router.push(`/dashboard/new-site`) + } else { + commentPage.mutate({ + address, + pageId: page!.id, + content: values.content, + }) + } + }) + + useEffect(() => { + if (commentPage.isSuccess) { + form.reset() + } + }, [commentPage.isSuccess, form]) + + return ( +
+
+ + {comments.data?.length || "0"} Comment + {comments.data?.length && comments.data.length > 1 ? "s" : ""} + +
+
+ +
+
+ +
+
+ +
+
+
+
+ {comments.data?.map((comment) => ( +
+ + + +
+
+ + {comment?.character?.metadata?.content?.name} + {" "} + ·{" "} + {dayjs + .duration( + dayjs(comment?.createdAt).diff(dayjs(), "minute"), + "minute", + ) + .humanize()}{" "} + ago ·{" "} + + + +
+
{comment.metadata?.content?.content}
+
+
+ ))} +
+
+ ) +} diff --git a/src/components/icons/BlockchainIcon.tsx b/src/components/icons/BlockchainIcon.tsx index a5da9f60..997dd566 100644 --- a/src/components/icons/BlockchainIcon.tsx +++ b/src/components/icons/BlockchainIcon.tsx @@ -12,14 +12,7 @@ export const BlockchainIcon: React.FC> = ( version="1.1" xmlns="http://www.w3.org/2000/svg" > - - + ) } diff --git a/src/components/site/PostFooter.tsx b/src/components/site/PostFooter.tsx index d5a729bb..3e30999f 100644 --- a/src/components/site/PostFooter.tsx +++ b/src/components/site/PostFooter.tsx @@ -17,6 +17,7 @@ import { Button } from "../ui/Button" import { useRouter } from "next/router" import { SITE_URL } from "~/lib/env" import { DuplicateIcon } from "@heroicons/react/solid" +import { Comment } from "~/components/common/Comment" export const PostFooter: React.FC<{ page?: Note @@ -137,36 +138,39 @@ export const PostFooter: React.FC<{ ]) return ( -
- - -
+ <> +
+ + +
+ + ) } diff --git a/src/components/site/PostMeta.tsx b/src/components/site/PostMeta.tsx index 611b3663..f3a68dc6 100644 --- a/src/components/site/PostMeta.tsx +++ b/src/components/site/PostMeta.tsx @@ -1,11 +1,25 @@ import { formatDate } from "~/lib/date" +import { BlockchainIcon } from "~/components/icons/BlockchainIcon" +import { UniLink } from "~/components/ui/UniLink" +import { Note } from "~/lib/types" export const PostMeta: React.FC<{ - publishedAt: string -}> = ({ publishedAt }) => { + page: Note +}> = ({ page }) => { return ( -
- {formatDate(publishedAt)} +
+ {formatDate(page.date_published)} + · + + url.startsWith("https://scan.crossbell.io/tx/"), + )?.[0] + } + > + +
) } diff --git a/src/components/site/SitePage.tsx b/src/components/site/SitePage.tsx index 22d4654d..376d8933 100644 --- a/src/components/site/SitePage.tsx +++ b/src/components/site/SitePage.tsx @@ -17,9 +17,7 @@ export const SitePage: React.FC<{ ) : (

{page?.title}

)} - {page?.tags?.includes("post") && ( - - )} + {page?.tags?.includes("post") && }
diff --git a/src/css/tailwind.css b/src/css/tailwind.css index 69dd4d19..ea1d5bd6 100644 --- a/src/css/tailwind.css +++ b/src/css/tailwind.css @@ -5,6 +5,7 @@ @layer base { html { @apply text-zinc-800; + @apply fill-zinc-800; } } @@ -112,6 +113,12 @@ body { fill: #ffcf55; } +.button.is-crossbell { + background-color: #fff5e1; + fill: #e5b65a; + color: #e5b65a; +} + .button.is-green { --shadow-color-button: theme("colors.green.700"); @apply bg-green-500 hover:bg-green-600 focus:bg-green-700; diff --git a/src/lib/types.ts b/src/lib/types.ts index df66541c..61c46195 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -73,6 +73,7 @@ export type SiteNavigationItem = { export type Note = UniNote & { slug?: string + character?: Profile } export type Notes = { diff --git a/src/models/page.model.ts b/src/models/page.model.ts index d6b4d825..57beccce 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -9,11 +9,12 @@ import { import { type Gate } from "~/lib/gate.server" import { Rendered, renderPageContent } from "~/markdown" import { notFound } from "~/lib/server-side-props" -import { PageVisibilityEnum, Notes } from "~/lib/types" +import { PageVisibilityEnum, Notes, Note } from "~/lib/types" import { isUUID } from "~/lib/uuid" import { stripHTML } from "~/lib/utils" import unidata from "~/lib/unidata" import { indexer, getContract } from "~/lib/crossbell" +import { NoteEntity, CharacterEntity } from "crossbell.js" const checkPageSlug = async ({ slug, @@ -401,3 +402,59 @@ export async function checkMint({ noteId: pageId.split("-")[1], }) } + +export async function commentPage({ + address, + pageId, + content, +}: { + address: string + pageId: string + content: string +}) { + const characterId = await getPrimaryCharacter(address) + if (!characterId) { + throw notFound(`character not found`) + } else { + console.log( + characterId, + { + content, + sources: ["xlog"], + tags: ["comment"], + }, + pageId.split("-")[0], + pageId.split("-")[1], + ) + return getContract()?.postNoteForNote( + characterId, + { + content, + sources: ["xlog"], + tags: ["comment"], + }, + pageId.split("-")[0], + pageId.split("-")[1], + ) + } +} + +export async function getComments({ pageId }: { pageId: string }) { + const list: (NoteEntity & { + character?: CharacterEntity | null + })[] = ( + await indexer.getNotes({ + toCharacterId: pageId.split("-")[0], + toNoteId: pageId.split("-")[1], + }) + ).list + + await Promise.all( + list.map(async (item) => { + const characterId = item.characterId + item.character = await indexer.getCharacter(characterId) + }), + ) + + return list +} diff --git a/src/queries/page.ts b/src/queries/page.ts index 723f46ff..1353a4f1 100644 --- a/src/queries/page.ts +++ b/src/queries/page.ts @@ -161,3 +161,28 @@ export function useMintPage() { }, ) } + +export function useCommentPage() { + const queryClient = useQueryClient() + return useMutation( + async (input: Parameters[0]) => { + return pageModel.commentPage(input) + }, + { + onSuccess: (data, variables) => { + queryClient.invalidateQueries(["getComments", variables.pageId]) + }, + }, + ) +} + +export function useGetComments(input: { pageId?: string }) { + return useQuery(["getComments", input.pageId], async () => { + if (!input.pageId) { + return + } + return pageModel.getComments({ + pageId: input.pageId, + }) + }) +}