feat: comment component

This commit is contained in:
DIYgod
2022-08-08 23:14:18 +01:00
parent 48e0d6fe84
commit 5ed8e70bd8
10 changed files with 320 additions and 55 deletions
+12 -8
View File
@@ -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<{
<>
<Disclosure.Button className="flex w-full justify-between rounded-lg px-4 py-2 text-left text-gray-900 hover:bg-zinc-100 transition-colors md:rounded-xl">
<span>
🔔 This{" "}
{page
? page.tags?.includes("post")
? "post"
: "page"
: "blog"}{" "}
has been permanently stored on the blockchain and signed by its
creator.
<BlockchainIcon className="w-4 h-4 inline-block align-middle mr-2" />
<span className="align-middle">
This{" "}
{page
? page.tags?.includes("post")
? "post"
: "page"
: "blog"}{" "}
has been permanently stored on the blockchain and signed by
its creator.
</span>
</span>
<ChevronUpIcon
className={`${
+162
View File
@@ -0,0 +1,162 @@
import clsx from "clsx"
import { Note, Profile } from "~/lib/types"
import { useAccount } from "wagmi"
import { useGetUserSites } from "~/queries/site"
import { Avatar } from "~/components/ui/Avatar"
import { Input } from "~/components/ui/Input"
import { Button } from "~/components/ui/Button"
import { useForm } from "react-hook-form"
import { useConnectModal } from "@rainbow-me/rainbowkit"
import { useState, useEffect } from "react"
import { useCommentPage, useGetComments } from "~/queries/page"
import { useRouter } from "next/router"
import dayjs from "dayjs"
import duration from "dayjs/plugin/duration"
import relativeTime from "dayjs/plugin/relativeTime"
import { UniLink } from "~/components/ui/UniLink"
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
dayjs.extend(duration)
dayjs.extend(relativeTime)
export const Comment: React.FC<{
page?: Note
className?: string
}> = ({ page, className }) => {
const { address } = useAccount()
const userSites = useGetUserSites(address)
const { openConnectModal } = useConnectModal()
const commentPage = useCommentPage()
const router = useRouter()
const [viewer, setViewer] = useState<Profile | null>(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 (
<div className={clsx("comment", className)}>
<div className="border-b pb-2 mb-6">
<span>
{comments.data?.length || "0"} Comment
{comments.data?.length && comments.data.length > 1 ? "s" : ""}
</span>
</div>
<div className="flex">
<Avatar
className="align-middle mr-3"
images={viewer?.avatars || []}
name={viewer?.name}
size={45}
/>
<form className="w-full" onSubmit={handleSubmit}>
<div>
<Input
id="content"
isBlock
required={
!!addressIn && userSites.isSuccess && !!userSites.data?.length
}
disabled={
!addressIn || !userSites.isSuccess || !userSites.data?.length
}
multiline
maxLength={30}
className="mb-4"
placeholder="Write a comment on the blockchain"
{...form.register("content", {})}
/>
</div>
<div className="text-right">
<Button
type="submit"
isLoading={userSites.isLoading || commentPage.isLoading}
>
{addressIn
? userSites.isSuccess && !userSites.data?.length
? "Create Character"
: "Submit"
: "Connect Wallet"}
</Button>
</div>
</form>
</div>
<div>
{comments.data?.map((comment) => (
<div key={comment.transactionHash} className="flex mt-5">
<UniLink
href={`https://crossbell.kindjeff.com/@${comment?.character?.handle}`}
className="align-middle mr-3"
>
<Avatar
images={comment?.character?.metadata?.content?.avatars || []}
name={comment?.character?.metadata?.content?.name}
size={45}
/>
</UniLink>
<div className="flex-1 flex flex-col rounded-lg px-4 py-3 bg-zinc-50">
<div className="mb-2 text-sm">
<UniLink
href={`https://crossbell.kindjeff.com/@${comment?.character?.handle}`}
className="font-medium text-indigo-600"
>
{comment?.character?.metadata?.content?.name}
</UniLink>{" "}
·{" "}
{dayjs
.duration(
dayjs(comment?.createdAt).diff(dayjs(), "minute"),
"minute",
)
.humanize()}{" "}
ago ·{" "}
<UniLink
href={`https://scan.crossbell.io/tx/${comment.transactionHash}`}
>
<BlockchainIcon className="w-3 h-3 inline-block" />
</UniLink>
</div>
<div>{comment.metadata?.content?.content}</div>
</div>
</div>
))}
</div>
</div>
)
}
+1 -8
View File
@@ -12,14 +12,7 @@ export const BlockchainIcon: React.FC<React.ComponentPropsWithoutRef<"svg">> = (
version="1.1"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M128 320v286.208a106.666667 106.666667 0 1 0 106.965333 182.912l256.128 144.085333a42.666667 42.666667 0 0 0 41.813334 0l256.128-144.085333A106.666667 106.666667 0 1 0 896 606.208V320a42.666667 42.666667 0 0 0-21.76-37.205333l-256.085333-144.042667a106.666667 106.666667 0 0 0-212.309334 0L149.76 282.794667A42.666667 42.666667 0 0 0 128 320z m362.666667-170.666667a21.333333 21.333333 0 1 1 42.666666 0 21.333333 21.333333 0 0 1-42.666666 0zM469.333333 247.125333v91.050667l-106.837333 62.293333A42.666667 42.666667 0 0 0 341.333333 437.333333v113.92l-111.744 63.829334c-5.12-3.413333-10.538667-6.4-16.256-8.874667V344.96l219.904-123.733333c10.026667 11.008 22.314667 19.882667 36.096 25.898666z m341.333334 359.082667a106.325333 106.325333 0 0 0-16.256 8.874667L682.666667 551.253333v-113.92a42.666667 42.666667 0 0 0-21.162667-36.864L554.666667 338.176V247.125333c13.781333-6.016 26.069333-14.933333 36.096-25.856L810.666667 344.96v261.248z m-62.634667 80.64a107.946667 107.946667 0 0 0-0.853333 27.904L512 847.018667l-235.221333-132.266667a107.946667 107.946667 0 0 0-0.853334-27.861333l98.602667-56.32 115.968 67.626666a42.666667 42.666667 0 0 0 43.008 0l115.968-67.669333 98.56 56.32zM597.333333 461.866667v100.352l-85.333333 49.749333-85.333333-49.749333v-100.352l85.333333-49.749334 85.333333 49.749334zM170.666667 682.666667a21.333333 21.333333 0 1 0 0 42.666666 21.333333 21.333333 0 0 0 0-42.666666z m661.333333 21.333333a21.333333 21.333333 0 1 1 42.666667 0 21.333333 21.333333 0 0 1-42.666667 0z"
p-id="1166"
></path>
<path
d="M512 128a21.333333 21.333333 0 1 0 0 42.666667 21.333333 21.333333 0 0 0 0-42.666667zM597.333333 562.176v-100.352l-85.333333-49.749333-85.333333 49.749333v100.352l85.333333 49.749333 85.333333-49.749333zM149.333333 704a21.333333 21.333333 0 1 1 42.666667 0 21.333333 21.333333 0 0 1-42.666667 0zM853.333333 682.666667a21.333333 21.333333 0 1 0 0 42.666666 21.333333 21.333333 0 0 0 0-42.666666z"
p-id="1167"
></path>
<path d="M912.290909 0c-61.607564 0-111.709091 50.101527-111.709091 111.709091 0 19.269818 4.896582 37.403927 13.5168 53.248l-182.998109 182.998109a147.884218 147.884218 0 0 0-182.365091-0.018618l-142.782836-142.782837A73.485964 73.485964 0 0 0 316.509091 167.563636c0-41.071709-33.401018-74.472727-74.472727-74.472727S167.563636 126.491927 167.563636 167.563636s33.401018 74.472727 74.472728 74.472728c13.777455 0 26.512291-4.002909 37.590109-10.556509l142.782836 142.782836a148.0704 148.0704 0 0 0 0 182.365091L186.628655 792.427055A111.169164 111.169164 0 0 0 111.709091 763.345455c-61.607564 0-111.709091 50.101527-111.709091 111.70909s50.101527 111.709091 111.709091 111.709091 111.709091-50.101527 111.709091-111.709091a111.076073 111.076073 0 0 0-13.5168-53.248l238.852654-238.852654a148.0704 148.0704 0 0 0 72.573673 30.142836v189.160728c-52.745309 8.899491-93.090909 54.774691-93.090909 110.033454 0 61.607564 50.101527 111.709091 111.709091 111.709091s111.709091-50.101527 111.709091-111.709091c0-55.240145-40.3456-101.115345-93.090909-110.033454V613.096727a148.0704 148.0704 0 0 0 72.573672-30.142836l142.782837 142.782836A73.448727 73.448727 0 0 0 763.345455 763.345455c0 41.071709 33.401018 74.472727 74.472727 74.472727s74.472727-33.401018 74.472727-74.472727-33.401018-74.472727-74.472727-74.472728c-13.777455 0-26.512291 4.002909-37.590109 10.556509l-142.782837-142.782836a148.0704 148.0704 0 0 0 0-182.365091l179.944728-179.944727A111.094691 111.094691 0 0 0 912.290909 223.418182c61.607564 0 111.709091-50.101527 111.709091-111.709091s-50.101527-111.709091-111.709091-111.709091zM204.8 167.563636c0-20.535855 16.700509-37.236364 37.236364-37.236363s37.236364 16.700509 37.236363 37.236363-16.700509 37.236364-37.236363 37.236364-37.236364-16.700509-37.236364-37.236364zM111.709091 949.527273c-41.071709 0-74.472727-33.401018-74.472727-74.472728s33.401018-74.472727 74.472727-74.472727 74.472727 33.401018 74.472727 74.472727-33.401018 74.472727-74.472727 74.472728z m502.690909-37.236364c0 41.071709-33.401018 74.472727-74.472727 74.472727s-74.472727-33.401018-74.472728-74.472727 33.401018-74.472727 74.472728-74.472727 74.472727 33.401018 74.472727 74.472727z m-74.472727-335.127273c-61.607564 0-111.709091-50.101527-111.709091-111.709091s50.101527-111.709091 111.709091-111.70909 111.709091 50.101527 111.709091 111.70909-50.101527 111.709091-111.709091 111.709091z m335.127272 186.181819c0 20.535855-16.700509 37.236364-37.236363 37.236363s-37.236364-16.700509-37.236364-37.236363 16.700509-37.236364 37.236364-37.236364 37.236364 16.700509 37.236363 37.236364z m37.236364-577.163637c-41.071709 0-74.472727-33.401018-74.472727-74.472727s33.401018-74.472727 74.472727-74.472727 74.472727 33.401018 74.472727 74.472727-33.401018 74.472727-74.472727 74.472727z"></path>
</svg>
)
}
+35 -31
View File
@@ -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 (
<div className="flex fill-gray-400 text-gray-500 mt-14">
<Button
variant="like"
className={`flex items-center mr-10 ${
isLike.isSuccess && isLike.data.count && "active"
}`}
isAutoWidth={true}
onClick={like}
isLoading={
userSite.isLoading ||
likePage.isLoading ||
unlikePage.isLoading ||
likeProgress
}
>
<LikeIcon className="mr-2 w-10 h-10" />
<span>{likes.data?.count || 0}</span>
</Button>
<Button
variant="collect"
className={`flex items-center mr-10 ${
isMint.isSuccess && isMint.data.count && "active"
}`}
isAutoWidth={true}
onClick={mint}
isLoading={mintPage.isLoading || likeProgress}
>
<DuplicateIcon className="mr-2 w-10 h-10" />
<span>{mints.data?.count || 0}</span>
</Button>
</div>
<>
<div className="flex fill-gray-400 text-gray-500 mt-14 mb-12">
<Button
variant="like"
className={`flex items-center mr-10 ${
isLike.isSuccess && isLike.data.count && "active"
}`}
isAutoWidth={true}
onClick={like}
isLoading={
userSite.isLoading ||
likePage.isLoading ||
unlikePage.isLoading ||
likeProgress
}
>
<LikeIcon className="mr-2 w-10 h-10" />
<span>{likes.data?.count || 0}</span>
</Button>
<Button
variant="collect"
className={`flex items-center mr-10 ${
isMint.isSuccess && isMint.data.count && "active"
}`}
isAutoWidth={true}
onClick={mint}
isLoading={mintPage.isLoading || likeProgress}
>
<DuplicateIcon className="mr-2 w-10 h-10" />
<span>{mints.data?.count || 0}</span>
</Button>
</div>
<Comment page={page} />
</>
)
}
+18 -4
View File
@@ -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 (
<div className="text-zinc-400 mt-2 flex items-center space-x-5">
<span>{formatDate(publishedAt)}</span>
<div className="text-zinc-400 mt-2">
<span className="align-middle">{formatDate(page.date_published)}</span>
<span className="align-middle mx-1"> · </span>
<UniLink
className="align-middle inline-block"
href={
page.related_urls?.filter((url) =>
url.startsWith("https://scan.crossbell.io/tx/"),
)?.[0]
}
>
<BlockchainIcon className="fill-zinc-500" />
</UniLink>
</div>
)
}
+1 -3
View File
@@ -17,9 +17,7 @@ export const SitePage: React.FC<{
) : (
<h2 className="text-xl font-bold page-title">{page?.title}</h2>
)}
{page?.tags?.includes("post") && (
<PostMeta publishedAt={page.date_published} />
)}
{page?.tags?.includes("post") && <PostMeta page={page} />}
</div>
<PageContent contentHTML={page?.body?.content || ""} className="mt-10" />
<PostFooter page={page} />
+7
View File
@@ -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;
+1
View File
@@ -73,6 +73,7 @@ export type SiteNavigationItem = {
export type Note = UniNote & {
slug?: string
character?: Profile
}
export type Notes = {
+58 -1
View File
@@ -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
}
+25
View File
@@ -161,3 +161,28 @@ export function useMintPage() {
},
)
}
export function useCommentPage() {
const queryClient = useQueryClient()
return useMutation(
async (input: Parameters<typeof pageModel.commentPage>[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,
})
})
}