diff --git a/src/app/(home)/story/page.tsx b/src/app/(home)/story/page.tsx index 374b070..3da1c8b 100644 --- a/src/app/(home)/story/page.tsx +++ b/src/app/(home)/story/page.tsx @@ -15,7 +15,8 @@ import Link from "next/link"; import { NRenderer } from "@/components/notion/renderer"; import { Metadata, ResolvingMetadata } from "next"; -import Comments from "@/components/comments"; +import Comments, { Reactions } from "@/components/comments"; +import { Stats } from "./stats"; export const revalidate = 100; @@ -115,6 +116,20 @@ export default async function Story({ {title} +
+ {repo && repoId && category && categoryId ? ( + + ) : null} +
+
@@ -135,7 +150,7 @@ export default async function Story({ -
+
{repo && repoId && category && categoryId ? ( { + const handleMetadata = (event: any) => { + const { data } = event; + if (typeof data === "object" && data?.giscus?.discussion) { + console.log(data); + } + }; + + window.addEventListener("message", handleMetadata as any); + + return () => window.removeEventListener("message", handleMetadata as any); + }, []); + return
; +} diff --git a/src/components/comments.tsx b/src/components/comments.tsx index cd6d318..324fd44 100644 --- a/src/components/comments.tsx +++ b/src/components/comments.tsx @@ -1,6 +1,7 @@ "use client"; import Giscus from "@giscus/react"; +import { useEffect, useState } from "react"; const Comments = ({ repo, @@ -30,4 +31,90 @@ const Comments = ({ ); }; +interface Reaction { + count: number; + viewerHasReacted: boolean; +} + +interface Reactions { + CONFUSED: Reaction; + EYES: Reaction; + HEART: Reaction; + HOORAY: Reaction; + LAUGH: Reaction; + ROCKET: Reaction; + THUMBS_DOWN: Reaction; + THUMBS_UP: Reaction; + [key: string]: Reaction; // To accommodate additional reaction types not explicitly listed here +} + +interface Discussion { + id: string; + locked: boolean; + reactionCount: number; + reactions: Reactions; + repository: { + nameWithOwner: string; + }; + totalCommentCount: number; + totalReplyCount: number; + url: string; +} + +interface GiscusData { + discussion: Discussion; +} + +interface MetadataEvent { + giscus?: GiscusData; +} + +export const Reactions = ({ + repo, + repoId, + category, + categoryId, +}: { + repo: `${string}/${string}`; + repoId: string; + category: string; + categoryId: string; +}) => { + const [showReactions, setShowReactions] = useState(false); + + useEffect(() => { + const handleMetadata = (event: { data: MetadataEvent }) => { + const { data } = event; + if (typeof data === "object" && data?.giscus?.discussion) { + console.log(data.giscus.discussion); + setShowReactions( + Object.values(data.giscus.discussion).some( + (reaction) => reaction.count > 0, + ), + ); + } + }; + + window.addEventListener("message", handleMetadata as any); + + return () => window.removeEventListener("message", handleMetadata as any); + }, []); + + return ( + + ); +}; + export default Comments;