Skip to content

Commit

Permalink
feat(comments): add giscus for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FjellOverflow committed Jan 23, 2024
1 parent 503341b commit 8293f39
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 4 deletions.
71 changes: 67 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@astrojs/check": "^0.4.1",
"@astrojs/rss": "^4.0.1",
"@giscus/react": "^2.4.0",
"@resvg/resvg-js": "^2.6.0",
"astro": "^4.1.1",
"fuse.js": "^7.0.0",
Expand Down
38 changes: 38 additions & 0 deletions src/components/Comments.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Giscus, { type GiscusProps, type Theme } from "@giscus/react";
import { GISCUS } from "@config";
import { useState } from "react";

interface Props {
lightTheme?: Theme;
darkTheme?: Theme;
}

export default function Comments({
lightTheme = "light",
darkTheme = "dark",
}: Props) {
const currentTheme = localStorage.getItem("theme");
const browserTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";

const [theme, setTheme] = useState(currentTheme || browserTheme);

// manual theme toggle
document.querySelector("#theme-btn")?.addEventListener("click", () => {
theme === "dark" ? setTheme("light") : setTheme("dark");
});

// browser preference
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", ({ matches: isDark }) => {
isDark ? setTheme("dark") : setTheme("light");
});

return (
<div className="mt-8">
<Giscus theme={theme === "light" ? lightTheme : darkTheme} {...GISCUS} />
</div>
);
}
15 changes: 15 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { GiscusProps } from "@giscus/react";
import type { Site, SocialObjects } from "./types";

export const SITE: Site = {
Expand All @@ -23,6 +24,20 @@ export const LOGO_IMAGE = {
width: 480,
};

export const GISCUS: GiscusProps = {
id: "comments",
repo: "FjellOverflow/blog",
repoId: "R_kgDOLFslYw",
category: "Giscus comments",
categoryId: "DIC_kwDOLFslY84Ccm0m",
mapping: "pathname",
reactionsEnabled: "0",
emitMetadata: "0",
inputPosition: "bottom",
lang: "en",
loading: "lazy",
};

export const SOCIALS: SocialObjects = [
{
name: "Github",
Expand Down
3 changes: 3 additions & 0 deletions src/layouts/PostDetails.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Datetime from "@components/Datetime";
import type { CollectionEntry } from "astro:content";
import { slugifyStr } from "@utils/slugify";
import ShareLinks from "@components/ShareLinks.astro";
import Comments from "@components/Comments";
export interface Props {
post: CollectionEntry<"blog">;
Expand Down Expand Up @@ -92,6 +93,8 @@ const layoutProps = {

<ShareLinks />
</div>

<Comments client:only />
</main>
<Footer />
</Layout>
Expand Down

0 comments on commit 8293f39

Please sign in to comment.